新增 微信第三方平台 获取预授权码接口

This commit is contained in:
Lemon
2023-07-31 11:50:12 +08:00
parent 7913ea98d8
commit 76f15b8312
2 changed files with 52 additions and 0 deletions

View File

@@ -88,6 +88,27 @@ public class AgentDevController extends BaseController {
}
/**
* 获取预授权码
*
* @return
*/
@GetMapping("/getPreAuthorizerCode")
public RestApiResponse<?> getPreAuthorizerCode() {
logger.info("微信第三方平台 获取预授权码 start");
RestApiResponse<?> response = null;
try {
String preAuthorizerCode = agentDevService.getPreAuthorizerCode();
response = new RestApiResponse<>(preAuthorizerCode);
} catch (Exception e) {
logger.error("微信第三方平台 获取预授权码 error, ", e);
response = new RestApiResponse<>(e);
}
logger.info("微信第三方平台 获取预授权码 result:{}", response);
return response;
}
/**
* 使用授权码获取授权信息
*

View File

@@ -359,6 +359,37 @@ public class AgentDevService {
return String.valueOf(auditId);
}
/**
* 获取预授权码
*
* @return
*/
public String getPreAuthorizerCode() {
// 获取平台令牌
GetComponentTokenDTO dto = GetComponentTokenDTO.builder()
.appId(PLATFORM_APP_ID)
.appSecret(PLATFORM_APP_SECRET)
.verifyTicket(null)
.build();
String componentToken = getComponentToken(dto);
// 拼接参数
String url = "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=" + componentToken;
JSONObject jsonObject = new JSONObject();
jsonObject.put("component_appid", PLATFORM_APP_ID);
// 发送请求
String result = HttpUtils.sendPost(url, JSONObject.toJSONString(jsonObject));
logger.info("微信第三方平台 获取预授权码请求 params:{}, result:{}", JSONObject.toJSONString(jsonObject), result);
// 将返回结果转成json对象
JSONObject resultJson = JSONObject.parseObject(result);
String preAuthCode = resultJson.getString("pre_auth_code");
if (StringUtils.isBlank(preAuthCode)) {
logger.error("微信第三方平台 获取预授权码为空");
return null;
}
return preAuthCode;
}
public static void main(String[] args) {
JSONArray array = new JSONArray();