新增 微信第三方平台 小程序/公众号授权事件回调接口

This commit is contained in:
Lemon
2023-07-28 17:03:10 +08:00
parent a5fcc876a0
commit e9c8624e1f
3 changed files with 75 additions and 13 deletions

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
@@ -152,7 +204,7 @@ public class AgentDevService {
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;
}