新增 微信第三方平台 授权回调接口

This commit is contained in:
Lemon
2023-08-02 10:13:48 +08:00
parent 8bedb86374
commit 1065a81db7
4 changed files with 90 additions and 19 deletions

View File

@@ -64,6 +64,30 @@ public class AgentDevController extends BaseController {
}
/**
* 授权回调 URI
*
* @param authCode
* @param expiresIn
* @return
*/
@GetMapping("/getAuthCallback")
public RestApiResponse<?> getAuthCallback(@RequestParam("auth_code") String authCode,
@RequestParam("expires_in") String expiresIn) {
logger.info("授权回调 URI, auth_code:{}, expires_in:{}", authCode, expiresIn);
RestApiResponse<?> response = null;
try {
agentDevService.getAuthCallback(authCode, expiresIn);
response = new RestApiResponse<>();
} catch (Exception e) {
logger.error("授权回调 URI error, ", e);
response = new RestApiResponse<>(e);
}
logger.info("授权回调 URI result:{}", response);
return response;
}
/**
* 获取微信第三方平台令牌
*
@@ -235,6 +259,7 @@ public class AgentDevController extends BaseController {
/**
* 发布已通过审核的小程序
*
* @param authorizerAppid
* @return
*/
@@ -245,7 +270,7 @@ public class AgentDevController extends BaseController {
try {
String result = agentDevService.releaseProcedure(authorizerAppid);
response = new RestApiResponse<>(result);
} catch (Exception e){
} catch (Exception e) {
logger.error("发布已通过审核的小程序 error, ", e);
response = new RestApiResponse<>(e);
}

View File

@@ -26,6 +26,7 @@ import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
/**
@@ -149,32 +150,24 @@ public class AgentDevService {
// 用户授权码 redisKey
String authCodeRedisKey = CacheConstants.COMPONENT_AUTHORIZATION_CODE + authorizerAppid;
// 用户预授权码 redisKey 过期时间 1800 秒
// String preAuthCodeRedisKey = CacheConstants.COMPONENT_PRE_AUTHORIZATION_CODE + authorizerAppid;
// 查缓存
String authorizationCode = redisCache.getCacheObject(authCodeRedisKey);
// 如果此用户之前有缓存,需把原来的删除
if (StringUtils.isNotBlank(authCodeRedisKey)) {
if (StringUtils.isNotBlank(authorizationCode)) {
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");
authorizationCode = MapUtils.getString(map, "AuthorizationCode");
logger.info("微信开放平台,第三方平台小程序/公众号授权事件 用户授权码:{}", authorizationCode);
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);
redisCache.setCacheObject(authCodeRedisKey, authorizationCode, 60 * 60, TimeUnit.SECONDS);
logger.info("微信开放平台,第三方平台小程序/公众号授权事件 success");
logger.info("微信开放平台,第三方平台小程序/公众号授权事件 success, authorizationCode:{}", authorizationCode);
}
@@ -242,10 +235,18 @@ public class AgentDevService {
String result = HttpUtils.sendPost(url, JSONObject.toJSONString(jsonObject));
logger.info("获取第三方平台 使用授权码获取授权信息 请求参数:{}, 请求结果:{}", JSONObject.toJSONString(jsonObject), result);
AuthInfoVO authInfoVO = JSONObject.parseObject(result, AuthInfoVO.class);
// 将结果转换成json对象
JSONObject resultJson = JSONObject.parseObject(result);
if (Objects.isNull(resultJson)) {
throw new RuntimeException("获取第三方平台 使用授权码获取授权信息 请求结果 为空");
}
// AuthInfoVO authInfoVO = (AuthInfoVO) resultJson.get("authorization_info");
AuthInfoVO authInfoVO = JSONObject.parseObject(JSONObject.toJSONString(resultJson.get("authorization_info")), AuthInfoVO.class);
if (authInfoVO == null) {
throw new RuntimeException("获取第三方平台 使用授权码获取授权信息 error");
}
logger.info("获取第三方平台 使用授权码获取授权信息 authInfoVO:{}", authInfoVO);
String authorizerAccessToken = authInfoVO.getAuthorizerAccessToken(); // 接口调用令牌, 默认有效期 7200 秒
String authorizerRefreshToken = authInfoVO.getAuthorizerRefreshToken(); // 刷新令牌 永久保存
String authorizerAppid = authInfoVO.getAuthorizerAppid(); // 授权方 appid
@@ -496,4 +497,26 @@ public class AgentDevService {
List<String> list = array.toList(String.class);
System.out.println(list);
}
/**
* 授权回调 处理方法
*/
public void getAuthCallback(String authCode, String expiresIn) {
logger.info("授权回调 处理方法 authCode:{}, expiresIn:{}", authCode, expiresIn);
AuthInfoVO authInfo = getAuthInfoByAuthCode(authCode);
if (authInfo == null) {
return;
}
String authorizerAppid = authInfo.getAuthorizerAppid();
// 先查询 redis 看有没有
String redisKey = CacheConstants.COMPONENT_AUTHORIZATION_CODE + authorizerAppid;
String redisAuthCode = redisCache.getCacheObject(redisKey);
if (StringUtils.isNotBlank(redisAuthCode)) {
// 有,先删除
redisCache.deleteObject(redisKey);
}
// 将 authCode 存入缓存
redisCache.setCacheObject(redisKey, authCode, Integer.parseInt(expiresIn), TimeUnit.SECONDS);
}
}

View File

@@ -0,0 +1,22 @@
package com.jsowell.pile.domain.agentDev;
import lombok.Data;
/**
* ext.json配置文件对象
*
* @author Lemon
* @Date 2023/8/2 10:09
*/
@Data
public class ExtJson {
/**
* 配置 ext.json 是否生效
*/
private boolean extEnable;
/**
* 配置 extAppid
*/
private String extAppid;
}

View File

@@ -1,6 +1,7 @@
package com.jsowell.pile.dto.agentDev;
import com.alibaba.fastjson2.annotation.JSONField;
import com.jsowell.pile.domain.agentDev.ExtJson;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
@@ -27,13 +28,13 @@ public class CommitCodeDTO {
* 代码库中的代码模板 ID可通过getTemplateList接口获取代码模板template_id。
*/
// @JSONField(name = "template_id")
private String templateId;
private long templateId;
/**
* 为了方便第三方平台的开发者引入 extAppid 的开发调试工作引入ext.json配置文件概念该参数则是用于控制ext.json配置文件的内容。
*/
// @JSONField(name = "ext_json")
private String extJson;
private ExtJson extJson;
/**
* 代码版本号,开发者可自定义(长度不要超过 64 个字符)