WxCpConfiguration.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package org.ruoyi.config;
  2. import com.google.common.collect.Maps;
  3. import jakarta.annotation.PostConstruct;
  4. import lombok.val;
  5. import me.chanjar.weixin.common.api.WxConsts;
  6. import me.chanjar.weixin.cp.api.WxCpService;
  7. import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
  8. import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
  9. import me.chanjar.weixin.cp.constant.WxCpConsts;
  10. import me.chanjar.weixin.cp.message.WxCpMessageHandler;
  11. import me.chanjar.weixin.cp.message.WxCpMessageRouter;
  12. import org.ruoyi.handler.*;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  15. import org.springframework.context.annotation.Configuration;
  16. import java.util.Map;
  17. import java.util.stream.Collectors;
  18. /**
  19. * 单实例配置
  20. *
  21. * @author <a href="https://github.com/binarywang">Binary Wang</a>
  22. */
  23. @Configuration
  24. @EnableConfigurationProperties(WxCpProperties.class)
  25. public class WxCpConfiguration {
  26. private final LogHandler logHandler;
  27. private NullHandler nullHandler;
  28. private LocationHandler locationHandler;
  29. private MenuHandler menuHandler;
  30. private MsgHandler msgHandler;
  31. private final UnsubscribeHandler unsubscribeHandler;
  32. private SubscribeHandler subscribeHandler;
  33. private WxCpProperties properties;
  34. private static Map<Integer, WxCpMessageRouter> routers = Maps.newHashMap();
  35. private static Map<Integer, WxCpService> cpServices = Maps.newHashMap();
  36. @Autowired
  37. public WxCpConfiguration(LogHandler logHandler, NullHandler nullHandler, LocationHandler locationHandler,
  38. MenuHandler menuHandler, MsgHandler msgHandler, UnsubscribeHandler unsubscribeHandler,
  39. SubscribeHandler subscribeHandler, WxCpProperties properties) {
  40. this.logHandler = logHandler;
  41. this.nullHandler = nullHandler;
  42. this.locationHandler = locationHandler;
  43. this.menuHandler = menuHandler;
  44. this.msgHandler = msgHandler;
  45. this.unsubscribeHandler = unsubscribeHandler;
  46. this.subscribeHandler = subscribeHandler;
  47. this.properties = properties;
  48. }
  49. public static Map<Integer, WxCpMessageRouter> getRouters() {
  50. return routers;
  51. }
  52. public static WxCpService getCpService(Integer agentId) {
  53. return cpServices.get(agentId);
  54. }
  55. @PostConstruct
  56. public void initServices() {
  57. cpServices = this.properties.getAppConfigs().stream().map(a -> {
  58. val configStorage = new WxCpDefaultConfigImpl();
  59. configStorage.setCorpId(this.properties.getCorpId());
  60. configStorage.setAgentId(a.getAgentId());
  61. configStorage.setCorpSecret(a.getSecret());
  62. configStorage.setToken(a.getToken());
  63. configStorage.setAesKey(a.getAesKey());
  64. val service = new WxCpServiceImpl();
  65. service.setWxCpConfigStorage(configStorage);
  66. routers.put(a.getAgentId(), this.newRouter(service));
  67. return service;
  68. }).collect(Collectors.toMap(service -> service.getWxCpConfigStorage().getAgentId(), a -> a));
  69. }
  70. private WxCpMessageRouter newRouter(WxCpService wxCpService) {
  71. final val newRouter = new WxCpMessageRouter(wxCpService);
  72. // 记录所有事件的日志 (异步执行)
  73. newRouter.rule().handler(this.logHandler).next();
  74. // 自定义菜单事件
  75. newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
  76. .event(WxConsts.MenuButtonType.CLICK).handler(this.menuHandler).end();
  77. // 点击菜单链接事件(这里使用了一个空的处理器,可以根据自己需要进行扩展)
  78. newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
  79. .event(WxConsts.MenuButtonType.VIEW).handler(this.nullHandler).end();
  80. // 关注事件
  81. newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
  82. .event(WxConsts.EventType.SUBSCRIBE).handler(this.subscribeHandler)
  83. .end();
  84. // 取消关注事件
  85. newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
  86. .event(WxConsts.EventType.UNSUBSCRIBE)
  87. .handler((WxCpMessageHandler) this.unsubscribeHandler).end();
  88. // 上报地理位置事件
  89. newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
  90. .event(WxConsts.EventType.LOCATION).handler(this.locationHandler)
  91. .end();
  92. // 接收地理位置消息
  93. newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.LOCATION)
  94. .handler(this.locationHandler).end();
  95. // 扫码事件(这里使用了一个空的处理器,可以根据自己需要进行扩展)
  96. newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
  97. .event(WxConsts.EventType.SCAN).handler((WxCpMessageHandler) this.nullHandler).end();
  98. newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
  99. .event(WxCpConsts.EventType.CHANGE_CONTACT).handler(new ContactChangeHandler()).end();
  100. newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
  101. .event(WxCpConsts.EventType.ENTER_AGENT).handler(new EnterAgentHandler()).end();
  102. // 默认
  103. newRouter.rule().async(false).handler(this.msgHandler).end();
  104. return newRouter;
  105. }
  106. }