Commit c55716fa by 吕明尚

session自动任务

parent 14ab6ca7
package share.web.controller.system;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.injector.methods.Insert;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import share.common.core.domain.R;
import share.common.enums.ReceiptRdeisEnum;
import share.system.service.QPService;
......@@ -17,6 +21,7 @@ import share.system.service.QPService;
public class QPController {
@Autowired
private QPService qpService;
RedisTemplate redisTemplate = new RedisTemplate();
//通过authCode,获取授权的session
@GetMapping("/oauthToken")
......@@ -24,4 +29,12 @@ public class QPController {
return R.ok(qpService.oauthToken(authCode));
}
@GetMapping("/queryKey")
public R<String> queryKey() {
JSONObject o = (JSONObject) redisTemplate.opsForValue().get(ReceiptRdeisEnum.MT_SESSION.getValue());
Long expiresIn = (Long) o.get("expires_in");
Integer remainRefreshCount = (Integer) o.get("remain_refresh_count");
return R.ok(new JSONObject().set("expires_in", expiresIn).set("remain_refresh_count", remainRefreshCount).toString());
}
}
\ No newline at end of file
......@@ -2,7 +2,10 @@ package share.common.enums;
public enum ReceiptRdeisEnum {
//1:验劵准备 2:session换取
PREPARE(1, "tuangou.receipt.prepare"), SESSION(2, "oauth.token");
PREPARE(1, "TUANGOU.RECEIPT.PREPARE."),
SESSION(2, "MT_SESSION_KEY"),
MT_SESSION(3, "MT_SESSION_OBJECT_KEY");
private Integer code;
private String value;
......
package share.quartz.task;
import cn.hutool.json.JSONObject;
import com.dianping.openapi.sdk.api.oauth.entity.CustomerRefreshTokenResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import share.common.enums.ReceiptRdeisEnum;
import share.system.service.ISConsumerCouponService;
import share.system.service.QPService;
import java.util.Date;
import java.util.Set;
......@@ -13,11 +16,13 @@ import java.util.Set;
public class RedisTask {
@Autowired
private ISConsumerCouponService isConsumerCouponService;
@Autowired
private QPService qpService;
RedisTemplate redisTemplate = new RedisTemplate();
public void AuToReceiptCode() {
//获取redis中所有以tuangou.receipt.prepare开头的key
Set keys = redisTemplate.keys("tuangou.receipt.prepare.*");
Set keys = redisTemplate.keys(ReceiptRdeisEnum.PREPARE.getValue() + "*");
//遍历key
keys.stream().forEach(o -> {
//获取key对应的value
......@@ -36,4 +41,15 @@ public class RedisTask {
isConsumerCouponService.deleteSConsumerCouponById(consumerCouponId);
});
}
public void AutomaticMtSessionKey() {
Boolean b = redisTemplate.hasKey(ReceiptRdeisEnum.SESSION.getValue());
if (!b) {
JSONObject o = (JSONObject) redisTemplate.opsForValue().get(ReceiptRdeisEnum.MT_SESSION.getValue());
String refreshToken = (String) o.get("refresh_token");
CustomerRefreshTokenResponse customerRefreshTokenResponse = qpService.refreshToken(refreshToken);
redisTemplate.opsForValue().set(ReceiptRdeisEnum.MT_SESSION.getValue(), new JSONObject(customerRefreshTokenResponse));
redisTemplate.opsForValue().set(ReceiptRdeisEnum.SESSION.getValue(), new JSONObject(customerRefreshTokenResponse), customerRefreshTokenResponse.getExpires_in() - 60 * 30);
}
}
}
package share.system.service;
import com.dianping.openapi.sdk.api.oauth.entity.CustomerRefreshTokenResponse;
import com.dianping.openapi.sdk.api.tuangou.entity.*;
import share.system.domain.vo.TuangouReceiptPrepareResponseEntityVo;
......@@ -21,4 +22,6 @@ public interface QPService {
String oauthToken(String authCode);
List<TuangouDealQueryShopDealResponseEntity> queryshopdeal(String openShopUuid);
CustomerRefreshTokenResponse refreshToken(String newToken);
}
......@@ -79,7 +79,7 @@ public class QPServiceImpl implements QPService {
//领取删除原来的
isConsumerCouponService.deleteSConsumerCouponById(one.getId());
//判断redis键是否存在
if (Boolean.TRUE.equals(redisTemplate.hasKey(ReceiptRdeisEnum.PREPARE.getValue() + "." + prepare.getReceipt_code()))) {
if (Boolean.TRUE.equals(redisTemplate.hasKey(ReceiptRdeisEnum.PREPARE.getValue() + prepare.getReceipt_code()))) {
//获取redis中的值
JSONObject jsonObject = (JSONObject) redisTemplate.opsForValue().get(prepare.getReceipt_code());
//获取过期时间
......@@ -91,7 +91,7 @@ public class QPServiceImpl implements QPService {
//获取redis中的券码id
Long consumerCouponId = jsonObject.getLong("consumerCouponId");
//删除redis中的值
redisTemplate.delete(ReceiptRdeisEnum.PREPARE.getValue() + "." + prepare.getReceipt_code());
redisTemplate.delete(ReceiptRdeisEnum.PREPARE.getValue() + prepare.getReceipt_code());
//删除数据库中的值
isConsumerCouponService.deleteSConsumerCouponById(consumerCouponId);
}
......@@ -151,7 +151,7 @@ public class QPServiceImpl implements QPService {
//设置30分钟后的时间
Date date = DateUtils.addMinutes(new Date(), 30);
map.put("expirationTime", String.valueOf(date));
redisTemplate.opsForValue().set(ReceiptRdeisEnum.PREPARE.getValue() + "." + prepare.getReceipt_code(), new JSONObject(map));
redisTemplate.opsForValue().set(ReceiptRdeisEnum.PREPARE.getValue() + prepare.getReceipt_code(), new JSONObject(map));
SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String output1 = format2.format(prepare.getReceiptEndDate());
BeanUtils.copyProperties(prepare, response);
......@@ -188,7 +188,9 @@ public class QPServiceImpl implements QPService {
*/
@Override
public TuangouReceiptPrepareResponseEntity prepare(String code, String openShopUuid) {
TuangouReceiptPrepareRequest request = new TuangouReceiptPrepareRequest(APP_KEY, APP_SECRET, "5bf98bb4c6c0d8ecef94f0882b40e00c5a248abc", code, "", openShopUuid);
JSONObject sessionKey = (JSONObject) redisTemplate.opsForValue().get(ReceiptRdeisEnum.MT_SESSION.getValue());
String session = (String) sessionKey.get("access_token");
TuangouReceiptPrepareRequest request = new TuangouReceiptPrepareRequest(APP_KEY, APP_SECRET, session, code, "", openShopUuid);
TuangouReceiptPrepare tuangouReceiptPrepare = new TuangouReceiptPrepare(request);
DefaultOpenAPIClient client = new DefaultOpenAPIClient();
TuangouReceiptPrepareResponse invoke = client.invoke(tuangouReceiptPrepare);
......@@ -204,9 +206,11 @@ public class QPServiceImpl implements QPService {
*/
@Override
public List<TuangouReceiptConsumeResponseEntity> consume(String code, int count, String openShopUuid) {
JSONObject sessionKey = (JSONObject) redisTemplate.opsForValue().get(ReceiptRdeisEnum.MT_SESSION.getValue());
String session = (String) sessionKey.get("access_token");
SConsumer user = FrontTokenComponent.getWxSConsumerEntry();
String requestid = UUID.randomUUID().toString();
TuangouReceiptConsumeRequest request = new TuangouReceiptConsumeRequest(APP_KEY, APP_SECRET, "5bf98bb4c6c0d8ecef94f0882b40e00c5a248abc",
TuangouReceiptConsumeRequest request = new TuangouReceiptConsumeRequest(APP_KEY, APP_SECRET, session,
requestid, code, count, openShopUuid,
user.getAccount(), user.getNickName());
DefaultOpenAPIClient openAPIClient = new DefaultOpenAPIClient();
......@@ -224,8 +228,10 @@ public class QPServiceImpl implements QPService {
*/
@Override
public List<TuangouReceiptReverseConsumeResponseEntity> reverseconsume(String code, Long appDealId, String openShopUuid) {
JSONObject sessionKey = (JSONObject) redisTemplate.opsForValue().get(ReceiptRdeisEnum.MT_SESSION.getValue());
String session = (String) sessionKey.get("access_token");
SConsumer user = FrontTokenComponent.getWxSConsumerEntry();
TuangouReceiptReverseConsumeRequest tuangouReceiptReverseConsumeRequest = new TuangouReceiptReverseConsumeRequest(APP_KEY, APP_SECRET, "5bf98bb4c6c0d8ecef94f0882b40e00c5a248abc",
TuangouReceiptReverseConsumeRequest tuangouReceiptReverseConsumeRequest = new TuangouReceiptReverseConsumeRequest(APP_KEY, APP_SECRET, session,
String.valueOf(appDealId), code, openShopUuid,
user.getAccount(), user.getNickName());
DefaultOpenAPIClient openAPIClient = new DefaultOpenAPIClient();
......@@ -243,9 +249,11 @@ public class QPServiceImpl implements QPService {
*/
@Override
public TuangouReceiptGetConsumedReponseEntity getconsumed(String code, String openShopUuid) {
JSONObject sessionKey = (JSONObject) redisTemplate.opsForValue().get(ReceiptRdeisEnum.MT_SESSION.getValue());
String session = (String) sessionKey.get("access_token");
DefaultOpenAPIClient openAPIClient = new DefaultOpenAPIClient();
TuangouReceiptGetConsumedRequest request = new TuangouReceiptGetConsumedRequest(APP_KEY, APP_SECRET,
"5bf98bb4c6c0d8ecef94f0882b40e00c5a248abc", code, openShopUuid);
session, code, openShopUuid);
TuangouReceiptGetConsumed tuangouReceiptGetConsumed = new TuangouReceiptGetConsumed(request);
TuangouReceiptGetConsumedReponse response = openAPIClient.invoke(tuangouReceiptGetConsumed);
if (response.getCode() != 200) {
......@@ -261,9 +269,8 @@ public class QPServiceImpl implements QPService {
APP_SECRET, GrantType.AUTHORIZATION_CODE.getValue(), authCode);
DynamicToken dynamicToken = new DynamicToken(request);
DynamicTokenResponse response = openAPIClient.invoke(dynamicToken);
if (response.getRemain_refresh_count() == 0) {
return refreshToken(response.getAccess_token());
}
redisTemplate.opsForValue().set(ReceiptRdeisEnum.MT_SESSION.getValue(), new JSONObject(response));
redisTemplate.opsForValue().set(ReceiptRdeisEnum.SESSION.getValue(), new JSONObject(response), response.getExpires_in() - 60 * 30);
return response.getAccess_token();
}
......@@ -276,7 +283,9 @@ public class QPServiceImpl implements QPService {
* @param app_shop_id
*/
DefaultOpenAPIClient openAPIClient = new DefaultOpenAPIClient();
TuangouDealQueryShopDealRequest request = new TuangouDealQueryShopDealRequest(APP_KEY, APP_SECRET, "5bf98bb4c6c0d8ecef94f0882b40e00c5a248abc", "", 1, 100, openShopUuid);
JSONObject sessionKey = (JSONObject) redisTemplate.opsForValue().get(ReceiptRdeisEnum.MT_SESSION.getValue());
String session = (String) sessionKey.get("access_token");
TuangouDealQueryShopDealRequest request = new TuangouDealQueryShopDealRequest(APP_KEY, APP_SECRET, session, "", 1, 100, openShopUuid);
TuangouDealQueryShopDeal tuangouDealQueryShopDeal = new TuangouDealQueryShopDeal(request);
TuangouDealQueryShopDealResponse response = openAPIClient.invoke(tuangouDealQueryShopDeal);
if (response.getCode() != 200) {
......@@ -285,12 +294,12 @@ public class QPServiceImpl implements QPService {
return response.getData();
}
public String refreshToken(String newToken) {
public CustomerRefreshTokenResponse refreshToken(String newToken) {
DefaultOpenAPIClient openAPIClient = new DefaultOpenAPIClient();
RefreshTokenRequest request = new RefreshTokenRequest(APP_KEY, APP_SECRET, newToken);
CustomerRefreshToken refreshToken = new CustomerRefreshToken(request);
CustomerRefreshTokenResponse response = openAPIClient.invoke(refreshToken);
return response.getAccess_token();
return response;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment