WxMpConfiguration.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package com.dataeasy.server.demo.msg;
  2. import lombok.AllArgsConstructor;
  3. import me.chanjar.weixin.mp.api.WxMpMessageRouter;
  4. import me.chanjar.weixin.mp.api.WxMpService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. import static me.chanjar.weixin.common.api.WxConsts.EventType.SUBSCRIBE;
  9. import static me.chanjar.weixin.common.api.WxConsts.XmlMsgType.EVENT;
  10. /**
  11. * @author myou
  12. * @version 1.0.0
  13. * @date 2025/3/1 21:36
  14. * @description TODO
  15. */
  16. @AllArgsConstructor
  17. @Configuration
  18. public class WxMpConfiguration {
  19. // @Autowired
  20. // private ImgHandler imgHandler;
  21. // @Autowired
  22. // private TextMsgHandler textMsgHandler;
  23. // @Autowired
  24. // private KfSessionHandler kfSessionHandler;
  25. // @Autowired
  26. // private MenuHandler menuHandler;
  27. // @Autowired
  28. // private ScanHandler scanHandler;
  29. // @Autowired
  30. // private MsgHandler msgHandler;
  31. @Autowired
  32. private LogHandler logHandler;
  33. // @Autowired
  34. // private WxMpProperties wxMpProperties;
  35. @Autowired
  36. private SubscribeHandler subscribeHandler;
  37. // @Autowired
  38. // private UnsubscribeHandler unsubscribeHandler;
  39. // @Bean
  40. // public WxMpService wxMpService() {
  41. // WxMpServiceImpl wxMpService = new WxMpServiceImpl();
  42. // wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
  43. // // 设置多个微信公众号的配置
  44. // // wxMpService.setMultiConfigStorages();
  45. // return wxMpService;
  46. // }
  47. /**
  48. * 这个地方的配置是保存在本地,生产环境需要自己扩展,可以保存在Redis中等等
  49. *
  50. * @return WxMpConfigStorage
  51. */
  52. // public WxMpConfigStorage wxMpConfigStorage() {
  53. // WxMpDefaultConfigImpl storage = new WxMpDefaultConfigImpl();
  54. // storage.setAppId(wxMpProperties.getAppId());
  55. // storage.setSecret(wxMpProperties.getSecret());
  56. // storage.setAesKey(wxMpProperties.getAesKey());
  57. // storage.setToken(wxMpProperties.getToken());
  58. // return storage;
  59. // }
  60. @Bean
  61. public WxMpMessageRouter messageRouter(WxMpService wxMpService) {
  62. final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService);
  63. // 记录所有事件的日志 (异步执行)
  64. newRouter.rule().handler(this.logHandler).next();
  65. //
  66. // // 接收客服会话管理事件
  67. // newRouter.rule().async(false).msgType(EVENT).event(KF_CREATE_SESSION)
  68. // .handler(this.kfSessionHandler).end();
  69. // newRouter.rule().async(false).msgType(EVENT).event(KF_CLOSE_SESSION)
  70. // .handler(this.kfSessionHandler).end();
  71. // newRouter.rule().async(false).msgType(EVENT).event(KF_SWITCH_SESSION)
  72. // .handler(this.kfSessionHandler).end();
  73. //
  74. //
  75. // // 自定义菜单事件
  76. // newRouter.rule().async(false).msgType(EVENT).event(EventType.CLICK).handler(this.menuHandler).end();
  77. //
  78. // 关注事件
  79. newRouter.rule().async(false).msgType(EVENT).event(SUBSCRIBE).handler(this.subscribeHandler).end();
  80. // 取消关注事件
  81. // newRouter.rule().async(false).msgType(EVENT).event(UNSUBSCRIBE).handler(this.unsubscribeHandler).end();
  82. //
  83. // // 扫码事件
  84. // newRouter.rule().async(false).msgType(EVENT).event(EventType.SCAN).handler(this.scanHandler).end();
  85. //
  86. // // 文本消息处理
  87. // newRouter.rule().async(false).msgType(XmlMsgType.TEXT).handler(this.textMsgHandler).end();
  88. //
  89. // // 图片消息处理
  90. // newRouter.rule().async(false).msgType(XmlMsgType.IMAGE).handler(this.imgHandler).end();
  91. //
  92. // 默认
  93. // newRouter.rule().async(false).handler(this.msgHandler).end();
  94. return newRouter;
  95. }
  96. }