|
@@ -3,8 +3,10 @@ package com.idiot.operationbackend.controller;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.idiot.operationbackend.entity.Account;
|
|
|
import com.idiot.operationbackend.entity.SubscribeScene;
|
|
|
+import com.idiot.operationbackend.handler.WeChatMessageFactory;
|
|
|
import com.idiot.operationbackend.service.facade.AccountService;
|
|
|
import com.idiot.operationbackend.service.facade.SubscribeSceneService;
|
|
|
+import com.idiot.operationbackend.service.facade.WeChatMessageService;
|
|
|
import com.idiot.operationbackend.service.facade.WeChatService;
|
|
|
import com.idiot.operationbackend.support.AccountState;
|
|
|
import com.idiot.operationbackend.support.Constants;
|
|
@@ -25,6 +27,8 @@ import java.io.BufferedInputStream;
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -66,14 +70,7 @@ public class WeChatController {
|
|
|
logger.info("验证票据事件,param参数依次是nonce:{},timestamp:{},encrypt_type:{},msg_signature:{}",nonce,
|
|
|
timestamp,encryptType,msgSignature);
|
|
|
try {
|
|
|
- BufferedInputStream bis = new BufferedInputStream(request.getInputStream());
|
|
|
- ByteArrayOutputStream buf = new ByteArrayOutputStream();
|
|
|
- int result = bis.read();
|
|
|
- while(result != -1) {
|
|
|
- buf.write((byte) result);
|
|
|
- result = bis.read();
|
|
|
- }
|
|
|
- String postData = buf.toString("utf-8");
|
|
|
+ String postData = readBody(request);;
|
|
|
logger.info("微信验证票据回调,postData before decry :{}",postData);
|
|
|
postData = weChatService.decryptMsg(msgSignature,timestamp,nonce,postData);
|
|
|
logger.info("微信验证票据回调,postData after decry :{}",postData);
|
|
@@ -85,7 +82,7 @@ public class WeChatController {
|
|
|
}
|
|
|
|
|
|
|
|
|
- @GetMapping("/authCallBack")
|
|
|
+ @GetMapping("/authCallback")
|
|
|
@ApiOperation(value = "授权地址回调")
|
|
|
public void authCallBack(@RequestParam(name = "auth_code") String authCode,
|
|
|
@RequestParam(name = "expires_in") String expiresIn,
|
|
@@ -149,7 +146,44 @@ public class WeChatController {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ @PostMapping("/msgCallback/{appid}")
|
|
|
+ @ApiOperation(value = "确认授权的微信公众号")
|
|
|
+ public String receiveWeChatMessage (@PathVariable String appid,
|
|
|
+ HttpServletRequest request) throws Exception {
|
|
|
+ String nonce = request.getParameter("nonce");
|
|
|
+ String timestamp = request.getParameter("timestamp");
|
|
|
+ // 默认aes
|
|
|
+ String encryptType = request.getParameter("encrypt_type");
|
|
|
+ String msgSignature = request.getParameter("msg_signature");
|
|
|
+ logger.info("接受微信事件回调,param参数依次是nonce:{},timestamp:{},encrypt_type:{},msg_signature:{}",nonce,
|
|
|
+ timestamp,encryptType,msgSignature);
|
|
|
+ request.setCharacterEncoding("UTF-8");
|
|
|
+ String postData = readBody(request);
|
|
|
+ logger.info("接受微信事件回调,postData before decry :{}",postData);
|
|
|
+ postData = weChatService.decryptMsg(msgSignature,timestamp,nonce,postData);
|
|
|
+ logger.info("接受微信事件回调,postData after decry :{}",postData);
|
|
|
+ Map<String,String> xmlMap = weChatService.xmlToMap(postData);
|
|
|
+ String msgType = xmlMap.get("MsgType");
|
|
|
+ xmlMap.put("appId",appid);
|
|
|
+ WeChatMessageService messageService = WeChatMessageFactory.getService(msgType);
|
|
|
+ if (Objects.isNull(messageService)) {
|
|
|
+ logger.error("接受到消息暂无处理类处理:{}",msgType);
|
|
|
+ return Constants.FAIL;
|
|
|
+ }else {
|
|
|
+ return messageService.processMessage(xmlMap);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
|
|
|
+ private String readBody (HttpServletRequest request) throws IOException {
|
|
|
+ BufferedInputStream bis = new BufferedInputStream(request.getInputStream());
|
|
|
+ ByteArrayOutputStream buf = new ByteArrayOutputStream();
|
|
|
+ int result = bis.read();
|
|
|
+ while(result != -1) {
|
|
|
+ buf.write((byte) result);
|
|
|
+ result = bis.read();
|
|
|
+ }
|
|
|
+ return buf.toString("utf-8");
|
|
|
+ }
|
|
|
|
|
|
}
|