update 获取预授权码

This commit is contained in:
Lemon
2023-07-31 13:58:14 +08:00
parent abb3d5f570
commit f079eea109
2 changed files with 23 additions and 9 deletions

View File

@@ -131,7 +131,7 @@ public class AgentDevService {
/**
* 小程序/公众号授权事件
*
* https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Before_Develop/authorize_event.html
* @param map
*/
private void authorizedInform(Map<String, String> map) {
@@ -148,19 +148,19 @@ public class AgentDevService {
// 用户授权码 redisKey
String authCodeRedisKey = CacheConstants.COMPONENT_AUTHORIZATION_CODE + authorizerAppid;
// 用户预授权码 redisKey 过期时间 1800 秒
String preAuthCodeRedisKey = CacheConstants.COMPONENT_PRE_AUTHORIZATION_CODE + authorizerAppid;
// String preAuthCodeRedisKey = CacheConstants.COMPONENT_PRE_AUTHORIZATION_CODE + authorizerAppid;
// 如果此用户之前有缓存,需把原来的删除
if (StringUtils.isNotBlank(authCodeRedisKey)) {
redisCache.deleteObject(authCodeRedisKey);
}
if (StringUtils.isNotBlank(preAuthCodeRedisKey)) {
redisCache.deleteObject(preAuthCodeRedisKey);
}
// if (StringUtils.isNotBlank(preAuthCodeRedisKey)) {
// redisCache.deleteObject(preAuthCodeRedisKey);
// }
// 获取授权码
String authorizationCode = MapUtils.getString(map, "AuthorizationCode");
// 预授权码
String preAuthCode = MapUtils.getString(map, "PreAuthCode");
// String preAuthCode = MapUtils.getString(map, "PreAuthCode");
// 过期时间 (授权码用)
int expiredTime = (Integer) MapUtils.getObject(map, "AuthorizationCodeExpiredTime");
@@ -170,7 +170,7 @@ public class AgentDevService {
}
redisCache.setCacheObject(authCodeRedisKey, authorizationCode, expiredTime, TimeUnit.SECONDS);
redisCache.setCacheObject(preAuthCodeRedisKey, preAuthCode, 60 * 30, TimeUnit.SECONDS);
// redisCache.setCacheObject(preAuthCodeRedisKey, preAuthCode, 60 * 30, TimeUnit.SECONDS);
logger.info("微信开放平台,第三方平台小程序/公众号授权事件 success");
}
@@ -365,6 +365,12 @@ public class AgentDevService {
* @return
*/
public String getPreAuthorizerCode() {
String redisKey = CacheConstants.PLATFORM_PRE_AUTH_CODE + PLATFORM_APP_ID;
// 先查缓存
String preAuthCode = redisCache.getCacheObject(redisKey);
if (StringUtils.isNotBlank(preAuthCode)) {
return preAuthCode;
}
// 获取平台令牌
GetComponentTokenDTO dto = GetComponentTokenDTO.builder()
.appId(PLATFORM_APP_ID)
@@ -382,11 +388,14 @@ public class AgentDevService {
logger.info("微信第三方平台 获取预授权码请求 params:{}, result:{}", JSONObject.toJSONString(jsonObject), result);
// 将返回结果转成json对象
JSONObject resultJson = JSONObject.parseObject(result);
String preAuthCode = resultJson.getString("pre_auth_code");
preAuthCode = resultJson.getString("pre_auth_code");
if (StringUtils.isBlank(preAuthCode)) {
logger.error("微信第三方平台 获取预授权码为空");
return null;
}
// 存入缓存 有效期 1800 秒
redisCache.setCacheObject(redisKey, preAuthCode, 60 * 30, TimeUnit.SECONDS);
return preAuthCode;
}