mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-22 03:55:17 +08:00
新增 代开发小程序 授权事件接收URL,验证票据接口
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package com.jsowell.api.uniapp;
|
||||
|
||||
import com.jsowell.common.annotation.Anonymous;
|
||||
import com.jsowell.common.core.controller.BaseController;
|
||||
import com.jsowell.pile.domain.AuthorizationEventResult;
|
||||
import com.jsowell.service.AgentDevService;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* 待开发小程序Controller
|
||||
*
|
||||
* @author Lemon
|
||||
* @Date 2023/7/27 15:09
|
||||
*/
|
||||
@Anonymous
|
||||
@RestController
|
||||
@RequestMapping("/agentDev")
|
||||
public class AgentDevController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private AgentDevService agentDevService;
|
||||
|
||||
@ApiOperation(value = "授权事件接收URL,验证票据")
|
||||
@PostMapping("/platform/event")
|
||||
public ResponseEntity<AuthorizationEventResult> wechatPlatformEvent(@RequestParam("timestamp") String timestamp,
|
||||
@RequestParam("nonce") String nonce,
|
||||
@RequestParam("msg_signature") String msgSignature,
|
||||
@RequestBody String postData) {
|
||||
AuthorizationEventResult<String> resultBean = new AuthorizationEventResult<>();
|
||||
ResponseEntity<AuthorizationEventResult> responseEntity;
|
||||
logger.debug("授权事件接收URL,验证票据");
|
||||
try {
|
||||
resultBean.setData(agentDevService.parseRequest(timestamp,nonce,msgSignature,postData));
|
||||
responseEntity = new ResponseEntity<>(resultBean, HttpStatus.OK);
|
||||
logger.debug("第三方平台授权事件接收URL,验证票据成功");
|
||||
} catch (Exception e) {
|
||||
logger.error("第三方平台授权事件接收URL,验证票据异常", e.getMessage(), e);
|
||||
AuthorizationEventResult<String> errorResultBean = new AuthorizationEventResult<>();
|
||||
errorResultBean.setMsg("第三方平台授权事件接收URL,验证票据异常");
|
||||
errorResultBean.setErrorMsg(e.getMessage());
|
||||
errorResultBean.setCode(422);
|
||||
responseEntity = new ResponseEntity<>(errorResultBean, HttpStatus.UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
return responseEntity;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package com.jsowell.service;
|
||||
|
||||
import com.jsowell.common.constant.CacheConstants;
|
||||
import com.jsowell.common.core.redis.RedisCache;
|
||||
import com.jsowell.common.util.StringUtils;
|
||||
import com.jsowell.common.util.wxplatform.AesException;
|
||||
import com.jsowell.common.util.wxplatform.WXBizMsgCrypt;
|
||||
import com.jsowell.common.util.wxplatform.WXXmlToMapUtil;
|
||||
import org.apache.commons.collections.MapUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 代开发小程序Service
|
||||
*
|
||||
* @author Lemon
|
||||
* @Date 2023/7/27 15:58
|
||||
*/
|
||||
@Service
|
||||
public class AgentDevService {
|
||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
@Autowired
|
||||
private RedisCache redisCache;
|
||||
|
||||
/**
|
||||
* 第三方平台 appid
|
||||
*/
|
||||
private static final String PLATFORM_APP_ID = "****************";
|
||||
|
||||
/**
|
||||
* 第三方平台 secret
|
||||
*/
|
||||
private static final String PLATFORM_APP_SECRET = "****************";
|
||||
|
||||
/**
|
||||
* 第三方平台 消息加解密Key
|
||||
*/
|
||||
private static final String PLATFORM_AES_KEY = "****************";
|
||||
|
||||
/**
|
||||
* 第三方平台 消息校验Token
|
||||
*/
|
||||
private static final String PLATFORM_COMPONENT_TOKEN = "****************";
|
||||
|
||||
|
||||
/**
|
||||
* 解析请求
|
||||
*
|
||||
* @param timeStamp
|
||||
* @param nonce
|
||||
* @param msgSignature
|
||||
* @param postData
|
||||
* @return
|
||||
*/
|
||||
public String parseRequest(String timeStamp, String nonce, String msgSignature, String postData) {
|
||||
logger.debug("==============================开始授权事件接收URL=================================");
|
||||
try {
|
||||
//这个类是微信官网提供的解密类,需要用到消息校验Token 消息加密Key和服务平台appid
|
||||
WXBizMsgCrypt pc = new WXBizMsgCrypt(PLATFORM_COMPONENT_TOKEN, PLATFORM_AES_KEY, PLATFORM_APP_ID);
|
||||
String xml = pc.decryptMsg(msgSignature, timeStamp, nonce, postData);
|
||||
Map<String, String> result = WXXmlToMapUtil.xmlToMap(xml);// 将xml转为map
|
||||
String componentVerifyTicket = MapUtils.getString(result, "ComponentVerifyTicket");
|
||||
if (StringUtils.isNotEmpty(componentVerifyTicket)) {
|
||||
// 存入Redis 过期时间 12 小时
|
||||
String redisKey = CacheConstants.COMPONENT_VERIFY_TICKET;
|
||||
redisCache.setCacheObject(redisKey, componentVerifyTicket, 60 * 12, TimeUnit.SECONDS);
|
||||
String verifyTicket = redisCache.getCacheObject(redisKey);
|
||||
|
||||
// 存储平台授权票据,保存ticket
|
||||
// redisTemplate.opsForValue().set("component_verify_ticket", componentVerifyTicket, 60 * 12, TimeUnit.SECONDS);
|
||||
// String verifyTicket = redisTemplate.opsForValue().get("component_verify_ticket").toString();
|
||||
logger.debug("====================授权票据【ComponentVerifyTicket】:【" + verifyTicket + "】====================");
|
||||
} else {
|
||||
throw new RuntimeException("微信开放平台,第三方平台获取【验证票据】失败");
|
||||
}
|
||||
} catch (AesException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
logger.debug("==============================结束授权事件接收URL=================================");
|
||||
return "success";
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user