新增 微信第三方平台 通过刷新令牌获取接口令牌接口

This commit is contained in:
Lemon
2023-08-02 11:50:03 +08:00
parent 17d18fafa0
commit b9d9ac18e8
2 changed files with 100 additions and 32 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,11 +324,12 @@ 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();
@@ -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));

View File

@@ -1,5 +1,6 @@
package com.jsowell.pile.domain.agentDev;
import com.alibaba.fastjson2.annotation.JSONField;
import lombok.Data;
/**
@@ -26,19 +27,22 @@ public class AuditItem {
* 一级类目名称 Y
* 可通过"getAllCategoryName"接口获取
*/
private String first_class;
@JSONField(name = "first_class")
private String firstClass;
/**
* 二级类目名称 Y
* 可通过"getAllCategoryName"接口获取
*/
private String second_class;
@JSONField(name = "second_class")
private String secondClass;
/**
* 三级类目名称 N
* 可通过"getAllCategoryName"接口获取
*/
private String third_class;
@JSONField(name = "third_class")
private String thirdClass;
/**
* 小程序页面的标题 N
@@ -50,17 +54,20 @@ public class AuditItem {
* 一级类目id Y
* 可通过"getAllCategoryName"接口获取
*/
private String first_id;
@JSONField(name = "first_id")
private String firstId;
/**
* 二级类目id Y
* 可通过"getAllCategoryName"接口获取
*/
private String second_id;
@JSONField(name = "second_id")
private String secondId;
/**
* 三级类目id Y
* 三级类目id N
* 可通过"getAllCategoryName"接口获取
*/
private String third_id;
@JSONField(name = "third_id")
private String thirdId;
}