WxGZHFunctionController.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package jnpf.message.controller;
  2. import cn.hutool.core.util.ObjectUtil;
  3. import com.alibaba.fastjson.JSONObject;
  4. import io.swagger.v3.oas.annotations.Parameter;
  5. import io.swagger.v3.oas.annotations.Parameters;
  6. import io.swagger.v3.oas.annotations.tags.Tag;
  7. import io.swagger.v3.oas.annotations.Operation;
  8. import jakarta.servlet.http.HttpServletRequest;
  9. import jakarta.servlet.http.HttpServletResponse;
  10. import jnpf.base.entity.SysConfigEntity;
  11. import jnpf.base.service.SysconfigService;
  12. import jnpf.message.entity.AccountConfigEntity;
  13. import jnpf.message.entity.WechatUserEntity;
  14. import jnpf.message.service.AccountConfigService;
  15. import jnpf.message.service.WechatUserService;
  16. import jnpf.permission.entity.SocialsUserEntity;
  17. import jnpf.permission.service.SocialsUserService;
  18. import jnpf.permission.service.UserService;
  19. import jnpf.util.*;
  20. import jnpf.util.wxutil.mp.WXGZHWebChatUtil;
  21. import jnpf.util.wxutil.mp.aes.WXBizMsgCrypt;
  22. import lombok.extern.slf4j.Slf4j;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.stereotype.Controller;
  25. import org.springframework.web.bind.annotation.*;
  26. import java.util.HashMap;
  27. import java.util.List;
  28. import java.util.Map;
  29. /**
  30. * 发送消息模型
  31. */
  32. @Tag(name = "微信公众号事件接收", description = "WechatOpen")
  33. @Controller
  34. @RequestMapping("/api/message/WechatOpen")
  35. @Slf4j
  36. public class WxGZHFunctionController {
  37. @Autowired
  38. private UserService userService;
  39. @Autowired
  40. private SysconfigService sysconfigService;
  41. @Autowired
  42. private AccountConfigService accountConfigService;
  43. @Autowired
  44. private SocialsUserService socialsUserService;
  45. @Autowired
  46. private WechatUserService wechatUserService;
  47. /**
  48. * 服务器基本配置链接微信公众号验证
  49. *
  50. * @param request 请求对象
  51. * @param response 响应对象
  52. * @return
  53. */
  54. @Operation(summary = "服务器基本配置链接微信公众号验证")
  55. @ResponseBody
  56. @Parameters({
  57. @Parameter(name = "enCode", description = "微信公众号账号配置编码", required = true)
  58. })
  59. @GetMapping("/token/{enCode}")
  60. public String token(@PathVariable("enCode") String enCode, HttpServletRequest request, HttpServletResponse response) throws Exception {
  61. //获取微信公众号账号配置
  62. AccountConfigEntity accountConfigEntity = accountConfigService.getInfoByEnCode(enCode,"7");
  63. if(ObjectUtil.isEmpty(accountConfigEntity)){
  64. log.info("未找到与编码相对应的微信公众号配置");
  65. return "";
  66. }
  67. //微信公众号服务器配置token
  68. String wxToken = accountConfigEntity.getAgentId();
  69. String signature = request.getParameter("signature");
  70. String echostr = XSSEscape.escape(request.getParameter("echostr"));
  71. String timestamp = request.getParameter("timestamp");
  72. String nonce = request.getParameter("nonce");
  73. String sortStr = WXGZHWebChatUtil.sort(wxToken,timestamp,nonce);
  74. String mySinStr = WXGZHWebChatUtil.shal(sortStr);
  75. if(StringUtil.isNotBlank(signature) && mySinStr.equals(signature)){
  76. return echostr;
  77. }else {
  78. log.info("微信公众号链接失败");
  79. return echostr;
  80. }
  81. }
  82. /**
  83. * 微信公众号事件请求
  84. *
  85. * @param request 请求对象
  86. * @param response 响应对象
  87. * @return
  88. * @throws Exception
  89. */
  90. @Operation(summary = "微信公众号事件请求")
  91. @ResponseBody
  92. @PostMapping("/token/{enCode}")
  93. /**
  94. * 微信公众号事件请求
  95. */
  96. public String tokenPost(@PathVariable("enCode") String enCode,HttpServletRequest request, HttpServletResponse response) throws Exception {
  97. log.info("微信公众号请求事件");
  98. //获取微信公众号账号配置
  99. AccountConfigEntity accountConfigEntity = accountConfigService.getInfoByEnCode(enCode,"7");
  100. if(ObjectUtil.isEmpty(accountConfigEntity)){
  101. log.info("未找到与编码相对应的微信公众号配置");
  102. return "";
  103. }
  104. //微信公众号服务器配置token
  105. String wxToken = accountConfigEntity.getAgentId();
  106. //微信公众号服务器配置EncodingAesKey
  107. String encodingAesKey = accountConfigEntity.getBearer();
  108. //微信公众号AppId
  109. String wxAppId = accountConfigEntity.getAppId();
  110. // 获取系统配置
  111. String msgSignature = request.getParameter("msg_signature");
  112. String encrypt_type = request.getParameter("encrypt_type");
  113. String signature = request.getParameter("signature");
  114. String echostr = XSSEscape.escape(request.getParameter("echostr"));
  115. String timestamp = request.getParameter("timestamp");
  116. String nonce = request.getParameter("nonce");
  117. String sortStr = WXGZHWebChatUtil.sort(wxToken,timestamp,nonce);
  118. String mySinStr = WXGZHWebChatUtil.shal(sortStr);
  119. //验签
  120. if(StringUtil.isNotBlank(signature) && mySinStr.equals(signature)){
  121. //事件信息
  122. Map<String ,Object> map = WXGZHWebChatUtil.parseXml(request);
  123. //事件信息
  124. String Event = String.valueOf(map.get("Event"));
  125. String openid = String.valueOf(map.get("FromUserName"));
  126. //公众号原始id
  127. String gzhId = String.valueOf(map.get("ToUserName"));
  128. if("aes".equals(encrypt_type)) {
  129. WXBizMsgCrypt pc = new WXBizMsgCrypt(wxToken, encodingAesKey, wxAppId);
  130. String encrypt = String.valueOf(map.get("Encrypt"));
  131. String format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%1$s]]></Encrypt></xml>";
  132. String fromXML = String.format(format, encrypt);
  133. // 获取解密后消息明文
  134. String result = pc.decryptMsg(msgSignature, timestamp, nonce, fromXML);
  135. Map<String, Object> resultMap = WXGZHWebChatUtil.xmlToMap(result);
  136. // 获取解密后事件信息
  137. Event = String.valueOf(resultMap.get("Event"));
  138. openid = String.valueOf(resultMap.get("FromUserName"));
  139. gzhId = String.valueOf(resultMap.get("ToUserName"));
  140. }
  141. String appId = accountConfigEntity.getAppId();
  142. String appsecret = accountConfigEntity.getAppSecret();
  143. String token = WXGZHWebChatUtil.getAccessToken(appId,appsecret);
  144. if("subscribe".equals(Event)){
  145. //用户关注事件
  146. if(StringUtil.isNotBlank(token)){
  147. JSONObject rstObj = WXGZHWebChatUtil.getUsetInfo(token,openid);
  148. if(rstObj.containsKey("unionid")){
  149. String unionid = rstObj.getString("unionid");
  150. SocialsUserEntity socialsUserEntity = socialsUserService.getInfoBySocialId(unionid,"wechat_open");
  151. if(socialsUserEntity==null){
  152. log.info("微信公众号未绑定系统账号,请登录小程序绑定");
  153. return "";
  154. }else{
  155. WechatUserEntity wechatUserEntity = wechatUserService.getInfoByGzhId(socialsUserEntity.getUserId(),gzhId);
  156. if(wechatUserEntity==null){
  157. WechatUserEntity entity = new WechatUserEntity();
  158. entity.setId(RandomUtil.uuId());
  159. entity.setUserId(socialsUserEntity.getUserId());
  160. entity.setGzhId(gzhId);
  161. entity.setCloseMark(1);
  162. entity.setCreatorTime(DateUtil.getNowDate());
  163. entity.setOpenId(openid);
  164. wechatUserService.create(entity);
  165. return "";
  166. }else {
  167. if(wechatUserEntity.getCloseMark()==0){
  168. wechatUserEntity.setCloseMark(1);
  169. }
  170. wechatUserEntity.setOpenId(openid);
  171. wechatUserEntity.setLastModifyTime(DateUtil.getNowDate());
  172. wechatUserService.update(wechatUserEntity.getId(),wechatUserEntity);
  173. }
  174. return "";
  175. }
  176. }else{
  177. log.info("微信公众号未绑定系统账号,请登录小程序绑定");
  178. return "";
  179. }
  180. }else{
  181. log.error("微信公众号token错误,请查看配置");
  182. return "";
  183. }
  184. }else if("unsubscribe".equals(Event)){
  185. //用户取消关注事件
  186. if(StringUtil.isNotBlank(token)){
  187. JSONObject rstObj = WXGZHWebChatUtil.getUsetInfo(token,openid);
  188. if(rstObj.containsKey("unionid")){
  189. String unionid = rstObj.getString("unionid");
  190. SocialsUserEntity socialsUserEntity = socialsUserService.getInfoBySocialId(unionid,"wechat_open");
  191. if(socialsUserEntity==null){
  192. log.info("微信公众号未绑定系统账号,请登录小程序绑定");
  193. }else{
  194. WechatUserEntity wechatUserEntity = wechatUserService.getInfoByGzhId(socialsUserEntity.getUserId(),gzhId);
  195. if(wechatUserEntity==null){
  196. WechatUserEntity entity = new WechatUserEntity();
  197. entity.setId(RandomUtil.uuId());
  198. entity.setUserId(socialsUserEntity.getUserId());
  199. entity.setGzhId(gzhId);
  200. entity.setCloseMark(0);
  201. entity.setCreatorTime(DateUtil.getNowDate());
  202. entity.setOpenId(openid);
  203. wechatUserService.create(entity);
  204. return "";
  205. }else {
  206. if(wechatUserEntity.getCloseMark()==1){
  207. wechatUserEntity.setCloseMark(0);
  208. }
  209. wechatUserEntity.setOpenId(openid);
  210. wechatUserEntity.setLastModifyTime(DateUtil.getNowDate());
  211. wechatUserService.update(wechatUserEntity.getId(),wechatUserEntity);
  212. return "";
  213. }
  214. }
  215. }else{
  216. log.info("微信公众号未绑定系统账号,请登录小程序绑定");
  217. return "";
  218. }
  219. }else{
  220. log.error("微信公众号token错误,请查看配置");
  221. return "";
  222. }
  223. return "";
  224. }else {
  225. return "";
  226. }
  227. }else {
  228. log.info("微信公众号事件请求失败");
  229. return echostr;
  230. }
  231. }
  232. /**
  233. * 获取系统配置
  234. */
  235. private Map<String, String> getSystemConfig() {
  236. // 获取系统配置
  237. List<SysConfigEntity> configList = sysconfigService.getList("SysConfig");
  238. Map<String, String> objModel = new HashMap<>(16);
  239. for (SysConfigEntity entity : configList) {
  240. objModel.put(entity.getFkey(), entity.getValue());
  241. }
  242. return objModel;
  243. }
  244. }