This commit is contained in:
2023-07-28 17:04:59 +08:00
4 changed files with 86 additions and 19 deletions

View File

@@ -28,8 +28,7 @@ public class AgentDevController extends BaseController {
private AgentDevService agentDevService;
/**
* 授权事件接收URL,验证票据
* 微信平台 10 分钟推送一次
* 授权事件接收URL,验证票据
*
* @param timestamp
* @param nonce
@@ -44,19 +43,20 @@ public class AgentDevController extends BaseController {
@RequestBody String postData) {
AuthorizationEventResult<String> resultBean = new AuthorizationEventResult<>();
ResponseEntity<AuthorizationEventResult> responseEntity;
logger.info("授权事件接收URL,验证票据");
logger.info("第三方平台授权事件接收URL params:{}", postData);
try {
resultBean.setData(agentDevService.parseRequest(timestamp, nonce, msgSignature, postData));
responseEntity = new ResponseEntity<>(resultBean, HttpStatus.OK);
logger.info("第三方平台授权事件接收URL,验证票据成功");
logger.info("第三方平台授权事件接收URL success");
} catch (Exception e) {
logger.error("第三方平台授权事件接收URL,验证票据异常", e.getMessage(), e);
logger.error("第三方平台授权事件接收URL error:{}", e.getMessage(), e);
AuthorizationEventResult<String> errorResultBean = new AuthorizationEventResult<>();
errorResultBean.setMsg("第三方平台授权事件接收URL,验证票据异常");
errorResultBean.setMsg("第三方平台授权事件接收URL error");
errorResultBean.setErrorMsg(e.getMessage());
errorResultBean.setCode(422);
responseEntity = new ResponseEntity<>(errorResultBean, HttpStatus.UNPROCESSABLE_ENTITY);
}
logger.info("第三方平台授权事件接收URL result:{}", resultBean);
return responseEntity;
}
@@ -68,7 +68,7 @@ public class AgentDevController extends BaseController {
* @return
*/
@PostMapping("/getComponentToken")
public RestApiResponse<?> getComponentToken(GetComponentTokenDTO dto) {
public RestApiResponse<?> getComponentToken(@RequestBody GetComponentTokenDTO dto) {
logger.info("获取第三方平台令牌 params:{}", JSONObject.toJSONString(dto));
RestApiResponse<?> response = null;
try {

View File

@@ -75,8 +75,14 @@ public class AgentDevService {
String infoType = MapUtils.getString(result, "InfoType");
if (StringUtils.equals("component_verify_ticket", infoType)) {
// 授权票据
VerifyTicket(result);
// 授权票据事件
verifyTicket(result);
} else if (StringUtils.equals("authorized", infoType)
|| StringUtils.equals("updateauthorized", infoType)
|| StringUtils.equals("unauthorized", infoType)) {
// 授权成功通知 authorized、授权更新通知 updateauthorized、 取消授权通知 unauthorized
// 小程序/公众号授权事件
authorizedInform(result);
}
} catch (AesException e) {
@@ -92,10 +98,11 @@ public class AgentDevService {
* @param map 微信第三方平台推送消息解析后的 map
* @return
*/
private String VerifyTicket(Map<String, String> map) {
private String verifyTicket(Map<String, String> map) {
// 获取验证票据
String componentVerifyTicket = MapUtils.getString(map, "ComponentVerifyTicket");
if (StringUtils.isEmpty(componentVerifyTicket)) {
throw new RuntimeException("微信开放平台,第三方平台获取【验证票据】失败");
throw new RuntimeException("微信开放平台,第三方平台获取【验证票据】为空");
}
String redisKey = CacheConstants.COMPONENT_VERIFY_TICKET + PLATFORM_APP_ID;
// 查缓存,看是否已经过期
@@ -104,8 +111,8 @@ public class AgentDevService {
// 先删除旧缓存
redisCache.deleteObject(redisKey);
}
// 存入Redis 过期时间 10 分钟
redisCache.setCacheObject(redisKey, componentVerifyTicket, 60 * 10, TimeUnit.SECONDS);
// 存入Redis 过期时间: 官方12小时但十分钟推送一次, 因此可设置 20 分钟过期
redisCache.setCacheObject(redisKey, componentVerifyTicket, 60 * 20, TimeUnit.SECONDS);
verifyTicket = redisCache.getCacheObject(redisKey);
// 存储平台授权票据,保存ticket
@@ -115,6 +122,51 @@ public class AgentDevService {
return verifyTicket;
}
/**
* 小程序/公众号授权事件
* @param map
*/
private void authorizedInform(Map<String, String> map) {
// 获取通知类型
String infoType = MapUtils.getString(map, "InfoType");
// 小程序appid
String authorizerAppid = MapUtils.getString(map, "AuthorizerAppid");
if (StringUtils.equals("unauthorized", infoType)) {
logger.info("微信开放平台,第三方平台,用户:{} 取消授权", authorizerAppid);
return;
}
logger.info("微信开放平台,第三方平台小程序/公众号授权事件 params:{}", map);
// 用户授权码 redisKey
String authCodeRedisKey = CacheConstants.COMPONENT_AUTHORIZATION_CODE + authorizerAppid;
// 用户预授权码 redisKey 过期时间 1800 秒
String preAuthCodeRedisKey = CacheConstants.COMPONENT_PRE_AUTHORIZATION_CODE + authorizerAppid;
// 如果此用户之前有缓存,需把原来的删除
if (StringUtils.isNotBlank(authCodeRedisKey)) {
redisCache.deleteObject(authCodeRedisKey);
}
if (StringUtils.isNotBlank(preAuthCodeRedisKey)) {
redisCache.deleteObject(preAuthCodeRedisKey);
}
// 获取授权码
String authorizationCode = MapUtils.getString(map, "AuthorizationCode");
// 预授权码
String preAuthCode = MapUtils.getString(map, "PreAuthCode");
// 过期时间 (授权码用)
int expiredTime = (Integer) MapUtils.getObject(map, "AuthorizationCodeExpiredTime");
if (StringUtils.isBlank(authorizationCode)
|| StringUtils.isBlank(authorizerAppid)) {
throw new RuntimeException("微信开放平台,第三方平台获取【授权码】为空");
}
redisCache.setCacheObject(authCodeRedisKey, authorizationCode, expiredTime, TimeUnit.SECONDS);
redisCache.setCacheObject(preAuthCodeRedisKey, preAuthCode, 60 * 30, TimeUnit.SECONDS);
logger.info("微信开放平台,第三方平台小程序/公众号授权事件 success");
}
/**
* 获取第三方平台 token
@@ -124,7 +176,7 @@ public class AgentDevService {
*/
public String getComponentToken(GetComponentTokenDTO dto) {
String verifyTicket = dto.getVerifyTicket();
String redisKey = CacheConstants.COMPONENT_ACCESS_TOKEN + PLATFORM_APP_ID;
String redisKey = CacheConstants.COMPONENT_ACCESS_TOKEN + dto.getAppId();
// 先判断缓存中是否有 token
String token = redisCache.getCacheObject(redisKey);
if (StringUtils.isNotBlank(token)) {
@@ -133,9 +185,10 @@ public class AgentDevService {
}
// 为空再去请求获取新令牌
String url = "https://api.weixin.qq.com/cgi-bin/component/api_component_token";
if (verifyTicket == null) {
if (StringUtils.isBlank(verifyTicket)) {
// 获取缓存中的票据
verifyTicket = redisCache.getCacheObject(CacheConstants.COMPONENT_VERIFY_TICKET + PLATFORM_APP_ID);
verifyTicket = redisCache.getCacheObject(CacheConstants.COMPONENT_VERIFY_TICKET + dto.getAppId());
logger.info("获取第三方平台缓存中票据:{}", verifyTicket);
}
JSONObject jsonObject = new JSONObject();
jsonObject.put("component_appid", dto.getAppId());
@@ -146,12 +199,12 @@ public class AgentDevService {
JSONObject jsonResult = JSONObject.parseObject(result);
logger.info("获取第三方平台 token 请求结果:{}", JSONObject.toJSONString(jsonResult));
// 获取返回值中的 token
token = jsonObject.getString("component_access_token");
token = jsonResult.getString("component_access_token");
if (token == null) {
throw new RuntimeException("获取第三方平台 token 异常!");
}
logger.info("获取第三方平台 token component_access_token:{}", token);
// 存入redis, 有效期 1小时50分
// 存入redis, 有效期 1小时50分, 官方 2 小时
redisCache.setCacheObject(redisKey, token, 60 * 110, TimeUnit.SECONDS);
return token;
}

View File

@@ -325,7 +325,11 @@ public class MemberService {
// PageHelper.startPage(pageNum, pageSize);
List<MemberWalletLogVO> list = memberBasicInfoService.getMemberWalletDetail(dto);
// 总支出
BigDecimal totalConsumption = list.stream().map(MemberWalletLogVO::getOrderAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal totalConsumption = list.stream()
.map(MemberWalletLogVO::getOrderAmount)
.filter(Objects::nonNull)
.reduce(BigDecimal.ZERO, BigDecimal::add);
vo.setTotalConsumption(totalConsumption);
// PageInfo<MemberWalletLogVO> pageInfo = new PageInfo<>(list);

View File

@@ -110,6 +110,16 @@ public class CacheConstants {
*/
public static final String COMPONENT_VERIFY_TICKET = "component_verify_ticket:";
/**
* 微信第三方平台 用户授权码
*/
public static final String COMPONENT_AUTHORIZATION_CODE = "component_authorization_code";
/**
* 微信第三方平台 用户预授权码
*/
public static final String COMPONENT_PRE_AUTHORIZATION_CODE = "component_pre_authorization_code";
/**
* 微信第三方平台 token
*/