This commit is contained in:
2023-08-02 13:31:01 +08:00
3 changed files with 108 additions and 34 deletions

View File

@@ -260,6 +260,62 @@ public class AgentDevService {
return authInfoVO;
}
/**
* 获取用户接口令牌
* @param authAppId
* @return
*/
public String getAuthAccessToken(String authAppId) {
// 先查缓存是否有值
String redisKey = CacheConstants.AUTHORIZER_ACCESS_TOKEN + authAppId;
String authAccessToken = redisCache.getCacheObject(redisKey);
if (StringUtils.isNotBlank(authAccessToken)) {
return authAccessToken;
}
// 如果没有,就通过刷新令牌去重新获取
String refreshTokenKey = CacheConstants.AUTHORIZER_REFRESH_TOKEN + authAppId;
String refreshToken = redisCache.getCacheObject(refreshTokenKey);
return getAuthTokenByRefreshToken(authAppId, refreshToken);
}
/**
* 通过刷新令牌获取接口令牌
* @param authAppId
* @param refreshToken
* @return
*/
public String getAuthTokenByRefreshToken(String authAppId, String refreshToken) {
GetComponentTokenDTO dto = GetComponentTokenDTO.builder()
.appId(PLATFORM_APP_ID)
.appSecret(PLATFORM_APP_SECRET)
.build();
String componentToken = getComponentToken(dto);
String url = "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=" + componentToken;
// 拼接参数
JSONObject jsonObject = new JSONObject();
jsonObject.put("component_appid", PLATFORM_APP_ID);
jsonObject.put("authorizer_appid", authAppId);
jsonObject.put("authorizer_refresh_token", refreshToken);
// 发送请求
String result = HttpUtils.sendPost(url, JSONObject.toJSONString(jsonObject));
logger.info("微信第三方平台 通过刷新令牌获取接口令牌 请求参数:{}\n, 请求结果:{}", JSONObject.toJSONString(jsonObject), result);
// 将返回结果转成 json 对象
JSONObject resultJson = JSONObject.parseObject(result);
String authAccessToken = resultJson.getString("authorizer_access_token"); // 用户接口令牌
int expiresTime = resultJson.getInteger("expires_in"); // 接口令牌过期时间
String authorizerRefreshToken = resultJson.getString("authorizer_refresh_token"); // 用户刷新令牌
// 先将刷新令牌删除
String refreshTokenKey = CacheConstants.AUTHORIZER_REFRESH_TOKEN + authAppId;
redisCache.deleteObject(refreshTokenKey);
// 分别存入缓存
String authTokenKey = CacheConstants.AUTHORIZER_ACCESS_TOKEN + authAppId;
redisCache.setCacheObject(authTokenKey, authAccessToken, expiresTime, TimeUnit.SECONDS);
redisCache.setCacheObject(refreshTokenKey, authorizerRefreshToken);
return redisCache.getCacheObject(authTokenKey);
}
/**
* 提交代码并生成体验版小程序
@@ -268,21 +324,22 @@ public class AgentDevService {
* @return
*/
public String commitCode(CommitCodeDTO dto) {
String redisKey = CacheConstants.AUTHORIZER_ACCESS_TOKEN + dto.getAuthorizerAppid();
String authAccessToken = redisCache.getCacheObject(redisKey);
if (StringUtils.isBlank(authAccessToken)) {
throw new RuntimeException("微信第三方平台 提交代码 error: authAccessToken为空");
}
// String redisKey = CacheConstants.AUTHORIZER_ACCESS_TOKEN + dto.getAuthorizerAppid();
// String authAccessToken = redisCache.getCacheObject(redisKey);
// if (StringUtils.isBlank(authAccessToken)) {
// throw new RuntimeException("微信第三方平台 提交代码 error: authAccessToken为空");
// }
String authAccessToken = getAuthAccessToken(dto.getAuthorizerAppid());
// 提交代码 url
String url = "https://api.weixin.qq.com/wxa/commit?access_token=" + authAccessToken;
JSONObject jsonObject = new JSONObject();
jsonObject.put("template_id", dto.getTemplateId());
jsonObject.put("ext_json", dto.getExtJson());
jsonObject.put("ext_json", JSONObject.toJSONString(dto.getExtJson()));
jsonObject.put("user_version", dto.getUserVersion());
jsonObject.put("user_desc", dto.getUserDesc());
String result = HttpUtils.sendPost(url, JSONObject.toJSONString(jsonObject));
logger.info("获取第三方平台 使用授权码获取授权信息 请求参数:{}, 请求结果:{}", JSONObject.toJSONString(jsonObject), result);
logger.info("获取第三方平台 提交代码 请求参数:{}, 请求结果:{}", JSONObject.toJSONString(jsonObject), result);
// 将返回结果转为json对象
JSONObject resultJson = JSONObject.parseObject(result);
@@ -297,11 +354,12 @@ public class AgentDevService {
* @return
*/
public List<CategoryInfo> getAllCategoryName(String authorizerAppid) {
String redisKey = CacheConstants.AUTHORIZER_ACCESS_TOKEN + authorizerAppid;
String authAccessToken = redisCache.getCacheObject(redisKey);
if (StringUtils.isBlank(authAccessToken)) {
throw new RuntimeException("微信第三方平台 获取类目名称信息 error: authAccessToken为空");
}
// String redisKey = CacheConstants.AUTHORIZER_ACCESS_TOKEN + authorizerAppid;
// String authAccessToken = redisCache.getCacheObject(redisKey);
// if (StringUtils.isBlank(authAccessToken)) {
// throw new RuntimeException("微信第三方平台 获取类目名称信息 error: authAccessToken为空");
// }
String authAccessToken = getAuthAccessToken(authorizerAppid);
String url = "https://api.weixin.qq.com/wxa/get_category?access_token=" + authAccessToken;
String result = HttpUtils.sendGet(url);
logger.info("获取第三方平台 获取类目名称信息 请求结果:{}", result);
@@ -332,11 +390,12 @@ public class AgentDevService {
* @return
*/
public String submitAudit(SubmitAuditDTO dto) {
String redisKey = CacheConstants.AUTHORIZER_ACCESS_TOKEN + dto.getAuthorizerAppid();
String authAccessToken = redisCache.getCacheObject(redisKey);
if (StringUtils.isBlank(authAccessToken)) {
throw new RuntimeException("微信第三方平台 提交代码审核 error: authAccessToken为空");
}
// String redisKey = CacheConstants.AUTHORIZER_ACCESS_TOKEN + dto.getAuthorizerAppid();
// String authAccessToken = redisCache.getCacheObject(redisKey);
// if (StringUtils.isBlank(authAccessToken)) {
// throw new RuntimeException("微信第三方平台 提交代码审核 error: authAccessToken为空");
// }
String authAccessToken = getAuthAccessToken(dto.getAuthorizerAppid());
List<AuditItem> itemList = dto.getAuditItems();
String url = "https://api.weixin.qq.com/wxa/submit_audit?access_token=" + authAccessToken;
@@ -373,11 +432,12 @@ public class AgentDevService {
throw new RuntimeException("微信第三方平台 查询审核单状态 error:审核id为空");
}
// 获取用户接口调用令牌
String redisKey = CacheConstants.AUTHORIZER_ACCESS_TOKEN + dto.getAuthorizerAppid();
String authAccessToken = redisCache.getCacheObject(redisKey);
if (StringUtils.isBlank(authAccessToken)) {
throw new RuntimeException("微信第三方平台 查询审核单状态 error: authAccessToken为空");
}
String authAccessToken = getAuthAccessToken(dto.getAuthorizerAppid());
// String redisKey = CacheConstants.AUTHORIZER_ACCESS_TOKEN + dto.getAuthorizerAppid();
// String authAccessToken = redisCache.getCacheObject(redisKey);
// if (StringUtils.isBlank(authAccessToken)) {
// throw new RuntimeException("微信第三方平台 查询审核单状态 error: authAccessToken为空");
// }
String url = "https://api.weixin.qq.com/wxa/get_auditstatus?access_token=" + authAccessToken;
// 拼接参数
JSONObject jsonObject = new JSONObject();
@@ -473,11 +533,12 @@ public class AgentDevService {
*/
public String releaseProcedure(String authorizerAppid){
// 获取用户接口调用令牌
String redisKey = CacheConstants.AUTHORIZER_ACCESS_TOKEN + authorizerAppid;
String authAccessToken = redisCache.getCacheObject(redisKey);
if (StringUtils.isBlank(authAccessToken)) {
throw new RuntimeException("微信第三方平台 发布已通过审核的小程序 error: authAccessToken为空");
}
String authAccessToken = getAuthAccessToken(authorizerAppid);
// String redisKey = CacheConstants.AUTHORIZER_ACCESS_TOKEN + authorizerAppid;
// String authAccessToken = redisCache.getCacheObject(redisKey);
// if (StringUtils.isBlank(authAccessToken)) {
// throw new RuntimeException("微信第三方平台 发布已通过审核的小程序 error: authAccessToken为空");
// }
String url = "https://api.weixin.qq.com/wxa/release?access_token=" + authAccessToken;
JSONObject jsonObject = new JSONObject();
String result = HttpUtils.sendPost(url, JSONObject.toJSONString(jsonObject));