2 files added
1 files modified
| | |
| | | import me.zhengjie.modules.project.domain.Project; |
| | | import me.zhengjie.modules.project.repository.ProjectRepository; |
| | | import me.zhengjie.modules.project.service.dto.ProjectQueryCriteria; |
| | | import me.zhengjie.modules.temp.service.WechatRobotService; |
| | | import me.zhengjie.modules.temp.service.WechatSendHistoryService; |
| | | import me.zhengjie.utils.QueryHelp; |
| | | import org.springframework.http.HttpStatus; |
| | |
| | | |
| | | private final ProjectRepository projectRepository; |
| | | private final WechatSendHistoryService wechatSendHistoryService; |
| | | private final WechatRobotService wechatRobotService; |
| | | |
| | | @AnonymousPutMapping(value = "/updateProjectLiableUserId") |
| | | @Log("修改项目责任人ID") |
| | |
| | | wechatSendHistoryService.handleApprovalCallback(msgSignature, timestamp, nonce, request, response); |
| | | return new ResponseEntity<>(HttpStatus.OK); |
| | | } |
| | | |
| | | @AnonymousGetMapping(value = "/wechat/robot/backUrl") |
| | | @Log("企业微信智能机器人验证回调URL") |
| | | @ApiOperation("企业微信智能机器人验证回调URL") |
| | | public void robotBack(@RequestParam(value = "msg_signature") String msgSignature, @RequestParam(value = "timestamp") String timestamp, |
| | | @RequestParam(value = "nonce") String nonce, @RequestParam(value = "echostr") String echostr, HttpServletResponse response){ |
| | | wechatRobotService.robotBack(msgSignature, timestamp, nonce, echostr, response); |
| | | } |
| | | |
| | | @AnonymousPostMapping(value = "/wechat/robot/backUrl") |
| | | @Log("处理企业微信智能机器人回调事件") |
| | | @ApiOperation("处理企业微信智能机器人回调事件") |
| | | public ResponseEntity<Object> handleRobotCallback(@RequestParam(value = "msg_signature") String msgSignature, @RequestParam(value = "timestamp") String timestamp, |
| | | @RequestParam(value = "nonce") String nonce, HttpServletRequest request, HttpServletResponse response){ |
| | | return new ResponseEntity<>(wechatRobotService.handleRobotCallback(msgSignature, timestamp, nonce, request, response), HttpStatus.OK); |
| | | } |
| | | } |
| New file |
| | |
| | | package me.zhengjie.modules.temp.service; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | |
| | | public interface WechatRobotService { |
| | | |
| | | /** |
| | | * 企业微信智能机器人验证回调URL |
| | | * @param msgSignature 企业微信加密签名 |
| | | * @param timestamp 时间戳 |
| | | * @param nonce 随机数 |
| | | * @param echostr 加密的字符串 |
| | | * @param response 响应 |
| | | * @return String |
| | | */ |
| | | void robotBack(String msgSignature, String timestamp, String nonce, String echostr, HttpServletResponse response); |
| | | |
| | | /** |
| | | * 处理企业微信智能机器人回调事件 |
| | | * @param msgSignature 企业微信加密签名 |
| | | * @param timestamp 时间戳 |
| | | * @param nonce 随机数 |
| | | * @param request 请求 |
| | | * @param response 响应 |
| | | * @return String |
| | | */ |
| | | Object handleRobotCallback(String msgSignature, String timestamp, String nonce, HttpServletRequest request, HttpServletResponse response); |
| | | } |
| New file |
| | |
| | | package me.zhengjie.modules.temp.service.impl; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import lombok.RequiredArgsConstructor; |
| | | import me.zhengjie.modules.temp.service.WechatRobotService; |
| | | import me.zhengjie.utils.weixin.WXBizJsonMsgCrypt; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.BufferedReader; |
| | | import java.io.InputStreamReader; |
| | | import java.io.PrintWriter; |
| | | import java.sql.Timestamp; |
| | | |
| | | @Service |
| | | @RequiredArgsConstructor |
| | | public class WechatRobotServiceImpl implements WechatRobotService { |
| | | |
| | | private final String CORP_ID = "wwc0fd49735bfcf0ad"; |
| | | private final String TOKEN = "WTAgLRQif"; |
| | | private final String ENCODING_AES_KEY = "w2FEuoDg2D7DxnmJn4DL6YiD47OSHwB34WXVwa2eriY"; |
| | | private final String APPROVAL_AGENT_ID = "1000005"; |
| | | |
| | | @Override |
| | | public void robotBack(String msgSignature, String timestamp, String nonce, String echostr, HttpServletResponse response) { |
| | | try { |
| | | WXBizJsonMsgCrypt wxcpt = new WXBizJsonMsgCrypt(TOKEN, ENCODING_AES_KEY, CORP_ID); |
| | | // 设置响应内容类型为纯文本 |
| | | response.setContentType("text/plain; charset=UTF-8"); |
| | | // 获取PrintWriter对象 |
| | | PrintWriter out = response.getWriter(); |
| | | // 直接返回 |
| | | String message = wxcpt.VerifyURL(msgSignature, timestamp, nonce, echostr); |
| | | out.print(message); |
| | | // 刷新并关闭PrintWriter |
| | | out.flush(); |
| | | out.close(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public Object handleRobotCallback(String msgSignature, String timestamp, String nonce, HttpServletRequest request, HttpServletResponse response) { |
| | | try { |
| | | WXBizJsonMsgCrypt wxcpt = new WXBizJsonMsgCrypt(TOKEN, ENCODING_AES_KEY, CORP_ID); |
| | | // 读取回调数据 |
| | | StringBuilder sb = new StringBuilder(); |
| | | BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream())); |
| | | String line; |
| | | while ((line = reader.readLine()) != null) { |
| | | sb.append(line); |
| | | } |
| | | |
| | | String callbackData = sb.toString(); |
| | | |
| | | JSONObject jsonObject = JSONObject.parseObject(callbackData); |
| | | String decryptedMessage = wxcpt.DecryptMsg(msgSignature, timestamp, nonce, jsonObject.toString()); |
| | | System.out.println("接收到机器人发送的加密消息:" + jsonObject.toJSONString()); |
| | | System.out.println("接收到机器人发送的解密后消息:" + decryptedMessage); |
| | | |
| | | return wxcpt.EncryptMsg("消息已收到", "", nonce); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | } |