This commit is contained in:
2023-07-29 14:38:31 +08:00
7 changed files with 266 additions and 9 deletions

View File

@@ -5,9 +5,10 @@ import com.jsowell.common.annotation.Anonymous;
import com.jsowell.common.core.controller.BaseController; import com.jsowell.common.core.controller.BaseController;
import com.jsowell.common.response.RestApiResponse; import com.jsowell.common.response.RestApiResponse;
import com.jsowell.pile.domain.AuthorizationEventResult; import com.jsowell.pile.domain.AuthorizationEventResult;
import com.jsowell.pile.dto.GetComponentTokenDTO; import com.jsowell.pile.dto.agentDev.CommitCodeDTO;
import com.jsowell.pile.dto.agentDev.GetComponentTokenDTO;
import com.jsowell.pile.vo.agentDev.AuthInfo;
import com.jsowell.service.AgentDevService; import com.jsowell.service.AgentDevService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@@ -28,7 +29,7 @@ public class AgentDevController extends BaseController {
private AgentDevService agentDevService; private AgentDevService agentDevService;
/** /**
* 授权事件接收URL,验证票据、 * 授权事件接收URL,验证票据、授权事件
* *
* @param timestamp * @param timestamp
* @param nonce * @param nonce
@@ -82,4 +83,47 @@ public class AgentDevController extends BaseController {
return response; return response;
} }
/**
* 使用授权码获取授权信息
*
* @param authorizationCode
* @return
*/
@GetMapping("/getAuthInfo/{authorizationCode}")
public RestApiResponse<?> getAuthInfo(@PathVariable("authorizationCode") String authorizationCode) {
logger.info("使用授权码获取授权信息 params:{}", authorizationCode);
RestApiResponse<?> response = null;
try {
AuthInfo authInfo = agentDevService.getAuthInfoByAuthCode(authorizationCode);
response = new RestApiResponse<>(authInfo);
} catch (Exception e) {
logger.error("使用授权码获取授权信息 error,", e);
response = new RestApiResponse<>(e);
}
logger.info("使用授权码获取授权信息 result:{}", response);
return response;
}
/**
* 提交代码并生成体验版小程序
*
* @param dto
* @return
*/
@PostMapping("/commitCode")
public RestApiResponse<?> commitCode(@RequestBody CommitCodeDTO dto) {
logger.info("提交代码并生成体验版小程序 params:{}", JSONObject.toJSONString(dto));
RestApiResponse<?> response = null;
try {
String commitResult = agentDevService.commitCode(dto);
response = new RestApiResponse<>(commitResult);
} catch (Exception e) {
logger.error("提交代码并生成体验版小程序 error,", e);
response = new RestApiResponse<>(e);
}
logger.info("提交代码并生成体验版小程序 result:{}", response);
return response;
}
} }

View File

@@ -8,15 +8,17 @@ import com.jsowell.common.util.http.HttpUtils;
import com.jsowell.common.util.wxplatform.AesException; import com.jsowell.common.util.wxplatform.AesException;
import com.jsowell.common.util.wxplatform.WXBizMsgCrypt; import com.jsowell.common.util.wxplatform.WXBizMsgCrypt;
import com.jsowell.common.util.wxplatform.WXXmlToMapUtil; import com.jsowell.common.util.wxplatform.WXXmlToMapUtil;
import com.jsowell.pile.dto.GetComponentTokenDTO; import com.jsowell.pile.domain.agentDev.CategoryInfo;
import com.jsowell.pile.dto.agentDev.CommitCodeDTO;
import com.jsowell.pile.dto.agentDev.GetComponentTokenDTO;
import com.jsowell.pile.vo.agentDev.AuthInfo;
import org.apache.commons.collections.MapUtils; import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Locale; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@@ -124,6 +126,7 @@ public class AgentDevService {
/** /**
* 小程序/公众号授权事件 * 小程序/公众号授权事件
*
* @param map * @param map
*/ */
private void authorizedInform(Map<String, String> map) { private void authorizedInform(Map<String, String> map) {
@@ -209,4 +212,83 @@ public class AgentDevService {
return token; return token;
} }
/**
* 使用授权码获取授权信息
* 并将授权信息中的 接口调用令牌、刷新令牌存入缓存
*
* @param authorizationCode
*/
public AuthInfo getAuthInfoByAuthCode(String authorizationCode) {
// 获取 component_access_token
GetComponentTokenDTO dto = GetComponentTokenDTO.builder()
.appId(PLATFORM_APP_ID)
.appSecret(PLATFORM_APP_SECRET)
.verifyTicket(null)
.build();
String componentToken = getComponentToken(dto);
// 使用授权码获取授权信息 url
String url = "https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=" + componentToken;
JSONObject jsonObject = new JSONObject();
jsonObject.put("component_appid", PLATFORM_APP_ID);
jsonObject.put("authorization_code", authorizationCode);
String result = HttpUtils.sendPost(url, JSONObject.toJSONString(jsonObject));
logger.info("获取第三方平台 使用授权码获取授权信息 请求参数:{}, 请求结果:{}", JSONObject.toJSONString(jsonObject), result);
AuthInfo authInfo = JSONObject.parseObject(result, AuthInfo.class);
if (authInfo == null) {
throw new RuntimeException("获取第三方平台 使用授权码获取授权信息 error");
}
String authorizerAccessToken = authInfo.getAuthorizerAccessToken(); // 接口调用令牌, 默认有效期 7200 秒
String authorizerRefreshToken = authInfo.getAuthorizerRefreshToken(); // 刷新令牌 永久保存
String authorizerAppid = authInfo.getAuthorizerAppid(); // 授权方 appid
String authAccessTokenKey = CacheConstants.AUTHORIZER_ACCESS_TOKEN + authorizerAppid;
redisCache.setCacheObject(authAccessTokenKey, authorizerAccessToken, authInfo.getExpiredTime(), TimeUnit.SECONDS);
String authRefreshTokenKey = CacheConstants.AUTHORIZER_REFRESH_TOKEN + authorizerAppid;
redisCache.setCacheObject(authRefreshTokenKey, authorizerRefreshToken);
return authInfo;
}
/**
* 提交代码并生成体验版小程序
*
* @param dto
* @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为空");
}
// 提交代码 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("user_version", dto.getUserVersion());
jsonObject.put("user_desc", dto.getUserDesc());
String result = HttpUtils.sendPost(url, JSONObject.toJSONString(jsonObject));
logger.info("获取第三方平台 使用授权码获取授权信息 请求参数:{}, 请求结果:{}", JSONObject.toJSONString(jsonObject), result);
// 将返回结果转为json对象
JSONObject resultJson = JSONObject.parseObject(result);
return resultJson.getString("errmsg");
}
public String submitAudit() {
return null;
}
} }

View File

@@ -113,18 +113,28 @@ public class CacheConstants {
/** /**
* 微信第三方平台 用户授权码 * 微信第三方平台 用户授权码
*/ */
public static final String COMPONENT_AUTHORIZATION_CODE = "component_authorization_code"; public static final String COMPONENT_AUTHORIZATION_CODE = "component_authorization_code:";
/** /**
* 微信第三方平台 用户预授权码 * 微信第三方平台 用户预授权码
*/ */
public static final String COMPONENT_PRE_AUTHORIZATION_CODE = "component_pre_authorization_code"; public static final String COMPONENT_PRE_AUTHORIZATION_CODE = "component_pre_authorization_code:";
/** /**
* 微信第三方平台 token * 微信第三方平台 token
*/ */
public static final String COMPONENT_ACCESS_TOKEN = "component_access_token:"; public static final String COMPONENT_ACCESS_TOKEN = "component_access_token:";
/**
* 接口调用令牌
*/
public static final String AUTHORIZER_ACCESS_TOKEN = "authorizer_access_token:";
/**
* 刷新令牌
*/
public static final String AUTHORIZER_REFRESH_TOKEN = "authorizer_refresh_token:";
/** /**
* 充电桩sn生成 key * 充电桩sn生成 key
*/ */

View File

@@ -0,0 +1,49 @@
package com.jsowell.pile.dto.agentDev;
import com.alibaba.fastjson2.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 微信第三方平台 提交代码并生成体验版DTO
*
* @author Lemon
* @Date 2023/7/29 13:26
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class CommitCodeDTO {
/**
* 授权方appid
*/
private String authorizerAppid;
/**
* 代码库中的代码模板 ID可通过getTemplateList接口获取代码模板template_id。
*/
// @JSONField(name = "template_id")
private String templateId;
/**
* 为了方便第三方平台的开发者引入 extAppid 的开发调试工作引入ext.json配置文件概念该参数则是用于控制ext.json配置文件的内容。
*/
// @JSONField(name = "ext_json")
private String extJson;
/**
* 代码版本号,开发者可自定义(长度不要超过 64 个字符)
*/
// @JSONField(name = "user_version")
private String userVersion;
/**
* 代码描述,开发者可自定义
*/
// @JSONField(name = "user_desc")
private String userDesc;
}

View File

@@ -1,6 +1,9 @@
package com.jsowell.pile.dto; package com.jsowell.pile.dto.agentDev;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
/** /**
* 获取第三方平台令牌 DTO * 获取第三方平台令牌 DTO
@@ -9,6 +12,9 @@ import lombok.Data;
* @Date 2023/7/28 14:05 * @Date 2023/7/28 14:05
*/ */
@Data @Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class GetComponentTokenDTO { public class GetComponentTokenDTO {
/** /**
* 第三方平台 appId * 第三方平台 appId

View File

@@ -0,0 +1,47 @@
package com.jsowell.pile.vo.agentDev;
import com.alibaba.fastjson2.annotation.JSONField;
import lombok.Data;
import java.util.List;
/**
* 用户授权信息实体
*
* @author Lemon
* @Date 2023/7/29 9:18
*/
@Data
public class AuthInfo {
/**
* 授权方 appid
*/
@JSONField(name = "authorizer_appid")
private String authorizerAppid;
/**
* 接口调用令牌
*/
@JSONField(name = "authorizer_access_token")
private String authorizerAccessToken;
/**
* authorizer_access_token 的有效期, 单位:秒
*/
@JSONField(name = "expires_in")
private Integer expiredTime;
/**
* 刷新令牌
* 刷新令牌主要用于第三方平台获取和刷新已授权用户的 authorizer_access_token。
* 一旦丢失,只能让用户重新授权,才能再次拿到新的刷新令牌。用户重新授权后,之前的刷新令牌会失效
*/
@JSONField(name = "authorizer_refresh_token")
private String authorizerRefreshToken;
/**
* 授权给开发者的权限集列表
*/
@JSONField(name = "func_info")
private List<PermissionInfo> permissionInfoList;
}

View File

@@ -0,0 +1,19 @@
package com.jsowell.pile.vo.agentDev;
import com.alibaba.fastjson2.annotation.JSONField;
import lombok.Data;
/**
* TODO
*
* @author Lemon
* @Date 2023/7/29 9:24
*/
@Data
public class PermissionInfo {
@JSONField(name = "funcscope_category")
private String funcscopeCategory;
private String id;
}