TencentWeComController.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package com.tzld.piaoquan.api.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.tzld.piaoquan.api.service.WeComAutoReply;
  4. import com.tzld.piaoquan.growth.common.common.constant.WeComServerConstant;
  5. import com.tzld.piaoquan.growth.common.service.WeComUserService;
  6. import com.tzld.piaoquan.growth.common.utils.wecom.WXBizMsgCrypt;
  7. import com.tzld.piaoquan.growth.common.utils.wecom.WxUtil;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.apache.commons.lang3.StringUtils;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.web.bind.annotation.GetMapping;
  12. import org.springframework.web.bind.annotation.PostMapping;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.bind.annotation.RestController;
  15. import javax.servlet.ServletInputStream;
  16. import javax.servlet.http.HttpServletRequest;
  17. import javax.servlet.http.HttpServletResponse;
  18. import java.io.BufferedReader;
  19. import java.io.InputStreamReader;
  20. import java.io.PrintWriter;
  21. import java.util.Map;
  22. import static com.tzld.piaoquan.growth.common.common.enums.CorpEnum.HNWQ;
  23. import static com.tzld.piaoquan.growth.common.common.enums.CorpEnum.YLQ;
  24. @Slf4j
  25. @RestController
  26. @RequestMapping("/wecom/server")
  27. public class TencentWeComController {
  28. @Autowired
  29. private WeComUserService weComUserService;
  30. @Autowired
  31. private WeComAutoReply weComAutoReply;
  32. @GetMapping("/verify")
  33. public void verifyGet(HttpServletRequest request, HttpServletResponse response) {
  34. try {
  35. // 微信加密签名
  36. String msgSignature = request.getParameter("msg_signature");
  37. // 时间戳
  38. String timestamp = request.getParameter("timestamp");
  39. // 随机数
  40. String nonce = request.getParameter("nonce");
  41. // 随机字符串
  42. // 如果是刷新,需返回原echostr
  43. String echoStr = request.getParameter("echostr");
  44. // 微信加密签名
  45. WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(WeComServerConstant.TOKEN,
  46. WeComServerConstant.ENCODING_AES_KEY,
  47. WeComServerConstant.CORP_ID);
  48. String sEchoStr = ""; //需要返回的明文
  49. PrintWriter out;
  50. sEchoStr = wxcpt.VerifyURL(msgSignature, timestamp,
  51. nonce, echoStr);
  52. log.info("verifyurl echostr: " + sEchoStr);
  53. // 验证URL成功,将sEchoStr返回
  54. out = response.getWriter();
  55. out.print(sEchoStr);
  56. } catch (Exception e) {
  57. //验证URL失败,错误原因请查看异常
  58. log.error("verifyGet error", e);
  59. }
  60. }
  61. /**
  62. * 刷新 ticket
  63. */
  64. @PostMapping(value = "/verify")
  65. public String verifyPost(HttpServletRequest request) {
  66. try {
  67. // 微信加密签名
  68. String msg_signature = request.getParameter("msg_signature");
  69. // 时间戳
  70. String timestamp = request.getParameter("timestamp");
  71. // 随机数
  72. String nonce = request.getParameter("nonce");
  73. String id = WeComServerConstant.CORP_ID;
  74. WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(WeComServerConstant.TOKEN, WeComServerConstant.ENCODING_AES_KEY, id);
  75. StringBuilder postData = new StringBuilder(); // 密文,对应POST请求的数据
  76. //1.获取加密的请求消息:使用输入流获得加密请求消息postData
  77. ServletInputStream in = request.getInputStream();
  78. BufferedReader reader = new BufferedReader(new InputStreamReader(in));
  79. String tempStr = ""; //作为输出字符串的临时串,用于判断是否读取完毕
  80. while (null != (tempStr = reader.readLine())) {
  81. postData.append(tempStr);
  82. }
  83. String suiteXml = wxcpt.DecryptMsg(msg_signature, timestamp, nonce, postData.toString());
  84. log.info("suiteXml: " + suiteXml);
  85. Map suiteMap = WxUtil.transferXmlToMap(suiteXml);
  86. log.info("suiteMap = {}", JSONObject.toJSONString(suiteMap));
  87. if (suiteMap != null) {
  88. String changeType = (String) suiteMap.get("ChangeType");
  89. if (StringUtils.isNotEmpty(changeType) && changeType.equals("add_external_contact")) {
  90. String userId = (String) suiteMap.get("UserID");
  91. String externalUserId = (String) suiteMap.get("ExternalUserID");
  92. String welcomeCode = (String) suiteMap.get("WelcomeCode");
  93. log.info("addStaffWithUser userId={} externalUserId={}", userId, externalUserId);
  94. weComUserService.addStaffWithUser(externalUserId, userId, HNWQ.getId());
  95. weComAutoReply.AutoReplyMessage(welcomeCode, externalUserId, userId, HNWQ.getId());
  96. }
  97. if (StringUtils.isNotEmpty(changeType) && changeType.equals("del_follow_user")) {
  98. String userId = (String) suiteMap.get("UserID");
  99. String externalUserId = (String) suiteMap.get("ExternalUserID");
  100. log.info("delStaffWithUser userId={} externalUserId={}", userId, externalUserId);
  101. weComUserService.delStaffWithUser(externalUserId, userId, HNWQ.getId(), System.currentTimeMillis());
  102. }
  103. }
  104. } catch (Exception e) {
  105. log.error("verifyPost error", e);
  106. }
  107. String success = "success";
  108. return success;
  109. }
  110. //优量圈验证接口
  111. @GetMapping("/ylq/verify")
  112. public void ylqVerifyGet(HttpServletRequest request, HttpServletResponse response) {
  113. try {
  114. // 微信加密签名
  115. String msgSignature = request.getParameter("msg_signature");
  116. // 时间戳
  117. String timestamp = request.getParameter("timestamp");
  118. // 随机数
  119. String nonce = request.getParameter("nonce");
  120. // 随机字符串
  121. // 如果是刷新,需返回原echostr
  122. String echoStr = request.getParameter("echostr");
  123. // 微信加密签名
  124. WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(WeComServerConstant.TOKEN,
  125. WeComServerConstant.ENCODING_AES_KEY,
  126. WeComServerConstant.CORP_ID);
  127. String sEchoStr = ""; //需要返回的明文
  128. PrintWriter out;
  129. sEchoStr = wxcpt.VerifyURL(msgSignature, timestamp,
  130. nonce, echoStr);
  131. log.info("verifyurl echostr: " + sEchoStr);
  132. // 验证URL成功,将sEchoStr返回
  133. out = response.getWriter();
  134. out.print(sEchoStr);
  135. } catch (Exception e) {
  136. //验证URL失败,错误原因请查看异常
  137. log.error("verifyGet error", e);
  138. }
  139. }
  140. //优量圈回调消息接口
  141. @PostMapping(value = "/ylq/verify")
  142. public String ylqVerifyPost(HttpServletRequest request) {
  143. try {
  144. // 微信加密签名
  145. String msg_signature = request.getParameter("msg_signature");
  146. // 时间戳
  147. String timestamp = request.getParameter("timestamp");
  148. // 随机数
  149. String nonce = request.getParameter("nonce");
  150. String id = WeComServerConstant.CORP_ID;
  151. WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(WeComServerConstant.TOKEN, WeComServerConstant.ENCODING_AES_KEY, id);
  152. StringBuilder postData = new StringBuilder(); // 密文,对应POST请求的数据
  153. //1.获取加密的请求消息:使用输入流获得加密请求消息postData
  154. ServletInputStream in = request.getInputStream();
  155. BufferedReader reader = new BufferedReader(new InputStreamReader(in));
  156. String tempStr = ""; //作为输出字符串的临时串,用于判断是否读取完毕
  157. while (null != (tempStr = reader.readLine())) {
  158. postData.append(tempStr);
  159. }
  160. String suiteXml = wxcpt.DecryptMsg(msg_signature, timestamp, nonce, postData.toString());
  161. log.info("suiteXml: " + suiteXml);
  162. Map suiteMap = WxUtil.transferXmlToMap(suiteXml);
  163. log.info("suiteMap = {}", JSONObject.toJSONString(suiteMap));
  164. if (suiteMap != null) {
  165. String changeType = (String) suiteMap.get("ChangeType");
  166. if (StringUtils.isNotEmpty(changeType) && changeType.equals("add_external_contact")) {
  167. String userId = (String) suiteMap.get("UserID");
  168. String externalUserId = (String) suiteMap.get("ExternalUserID");
  169. String welcomeCode = (String) suiteMap.get("WelcomeCode");
  170. log.info("addStaffWithUser userId={} externalUserId={}", userId, externalUserId);
  171. weComUserService.addStaffWithUser(externalUserId, userId, YLQ.getId());
  172. weComAutoReply.AutoReplyMessage(welcomeCode, externalUserId, userId, YLQ.getId());
  173. }
  174. if (StringUtils.isNotEmpty(changeType) && changeType.equals("del_follow_user")) {
  175. String userId = (String) suiteMap.get("UserID");
  176. String externalUserId = (String) suiteMap.get("ExternalUserID");
  177. log.info("delStaffWithUser userId={} externalUserId={}", userId, externalUserId);
  178. weComUserService.delStaffWithUser(externalUserId, userId, YLQ.getId(), System.currentTimeMillis());
  179. }
  180. }
  181. } catch (Exception e) {
  182. log.error("verifyPost error", e);
  183. }
  184. String success = "success";
  185. return success;
  186. }
  187. }