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

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

@@ -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;
}

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;
}

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
*/