WxMpController.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package vip.xiaonuo.weixin.gongzhong.controller;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import cn.hutool.json.JSONUtil;
  5. import jakarta.servlet.http.HttpServletResponse;
  6. import lombok.AllArgsConstructor;
  7. import lombok.SneakyThrows;
  8. import lombok.extern.slf4j.Slf4j;
  9. import me.chanjar.weixin.mp.api.WxMpMessageRouter;
  10. import me.chanjar.weixin.mp.api.WxMpService;
  11. import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
  12. import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
  13. import org.springframework.web.bind.annotation.*;
  14. import vip.xiaonuo.coldchain.modular.alarmuser.entity.AlarmUser;
  15. import vip.xiaonuo.coldchain.modular.alarmuser.service.AlarmUserService;
  16. import vip.xiaonuo.coldchain.modular.push.entity.WeChatUser;
  17. import vip.xiaonuo.coldchain.modular.push.utils.PushUtil;
  18. import vip.xiaonuo.dev.modular.config.service.DevConfigService;
  19. import java.util.List;
  20. import java.util.Objects;
  21. @CrossOrigin
  22. @RestController
  23. @AllArgsConstructor
  24. @Slf4j
  25. @RequestMapping("/wx/mp")
  26. public class WxMpController {
  27. private final WxMpService wxMpService;
  28. private final WxMpMessageRouter wxMpMessageRouter;
  29. private final AlarmUserService alarmUserService;
  30. private final DevConfigService devConfigService;
  31. @GetMapping("/auth")
  32. public String check(@RequestParam String signature, @RequestParam String timestamp, @RequestParam String nonce, @RequestParam String echostr) {
  33. log.info("\n接收到来自微信服务器的认证消息:[{}, {}, {}, {}]", signature, timestamp, nonce, echostr);
  34. boolean checkSignature = wxMpService.checkSignature(timestamp, nonce, signature);
  35. log.info("\n校验结果:{}", checkSignature);
  36. return echostr;
  37. }
  38. @SneakyThrows
  39. @GetMapping("/auth/page")
  40. public void check(@RequestParam(required = false) String code, @RequestParam(required = false) String state, HttpServletResponse response) {
  41. log.info("\n接收到来自微信服务器的授权消息:code={},state={}", code, state);
  42. if (StrUtil.isBlank(code)) {
  43. response.getWriter().println("授权失败,请重新授权");
  44. return;
  45. }
  46. WeChatUser weChatUser = PushUtil.getUserInfo(code);
  47. log.info("获取用户信息:{}", JSONUtil.toJsonStr(weChatUser));
  48. List<AlarmUser> userList = alarmUserService.getByOpenId(weChatUser.getOpenid());
  49. AlarmUser alarmUser;
  50. if (CollUtil.isNotEmpty(userList)) {
  51. //检查用户的组织机构
  52. long count = userList.stream().filter(x -> !Objects.equals(x.getCreateOrg(), "-1")).count();
  53. if (count > 0) {
  54. //进入授权页面
  55. String redirect = devConfigService.getValueByKey("wx_front_authed_page");
  56. if (StrUtil.isNotBlank(redirect)) {
  57. response.sendRedirect(redirect);
  58. } else {
  59. response.setCharacterEncoding("UTF-8");
  60. response.getWriter().println("您已授权!\\^o^/");
  61. }
  62. return;
  63. }
  64. alarmUser = userList.get(0);
  65. } else {
  66. //没有用户信息则创建用户
  67. alarmUser = alarmUserService.subscribe(weChatUser.getOpenid());
  68. }
  69. String redirect = devConfigService.getValueByKey("wx_front_not_auth_page") + "?id=" + alarmUser.getId();
  70. log.info("进入用户授权页面 {}", redirect);
  71. response.sendRedirect(redirect);
  72. }
  73. @SneakyThrows
  74. @PostMapping("/auth")
  75. public String wxEventListener(@RequestBody String param,
  76. @RequestParam(required = false) String signature,
  77. @RequestParam(required = false) String timestamp,
  78. @RequestParam(required = false) String nonce) {
  79. log.info("\n接收微信事件:[signature=[{}], timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ",
  80. signature, timestamp, nonce, param);
  81. // 微信时间在wxMpMessageRouter中处理
  82. WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(param);
  83. WxMpXmlOutMessage outMessage = wxMpMessageRouter.route(inMessage);
  84. // 将响应消息转换为xml格式返回
  85. return outMessage == null ? "" : outMessage.toXml();
  86. }
  87. }