WxAutoConfiguration.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package com.bofeng.wx;
  2. import com.bofeng.support.WeChatProperties;
  3. import lombok.val;
  4. import me.chanjar.weixin.common.api.WxConsts;
  5. import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
  6. import me.chanjar.weixin.mp.api.WxMpMessageRouter;
  7. import me.chanjar.weixin.mp.api.WxMpQrcodeService;
  8. import me.chanjar.weixin.mp.api.WxMpService;
  9. import me.chanjar.weixin.mp.api.impl.WxMpQrcodeServiceImpl;
  10. import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  13. import org.springframework.context.annotation.Bean;
  14. import org.springframework.context.annotation.Configuration;
  15. @Configuration
  16. @EnableConfigurationProperties(WeChatProperties.class)
  17. public class WxAutoConfiguration {
  18. private final WeChatProperties weChatProperties;
  19. private final LogHandler logHandler;
  20. private final NullHandler nullHandler;
  21. private final MenuHandler menuHandler;
  22. private final MsgHandler msgHandler;
  23. private final UnsubscribeHandler unsubscribeHandler;
  24. private final SubscribeHandler subscribeHandler;
  25. private final ScanHandler scanHandler;
  26. @Autowired
  27. public WxAutoConfiguration(WeChatProperties weChatProperties,
  28. LogHandler logHandler,
  29. NullHandler nullHandler,
  30. MenuHandler menuHandler,
  31. MsgHandler msgHandler,
  32. UnsubscribeHandler unsubscribeHandler,
  33. SubscribeHandler subscribeHandler,
  34. ScanHandler scanHandler) {
  35. this.weChatProperties = weChatProperties;
  36. this.logHandler = logHandler;
  37. this.nullHandler = nullHandler;
  38. this.menuHandler = menuHandler;
  39. this.msgHandler = msgHandler;
  40. this.unsubscribeHandler = unsubscribeHandler;
  41. this.subscribeHandler = subscribeHandler;
  42. this.scanHandler = scanHandler;
  43. }
  44. @Bean
  45. public WxMpService wxMpService() {
  46. WxMpInMemoryConfigStorage configStorage = new WxMpInMemoryConfigStorage();
  47. configStorage.setAppId(weChatProperties.getAppID());
  48. configStorage.setSecret(weChatProperties.getAppsecret());
  49. configStorage.setToken(weChatProperties.getToken());
  50. configStorage.setAesKey(weChatProperties.getAesKey());
  51. val service = new WxMpServiceImpl();
  52. service.setWxMpConfigStorage(configStorage);
  53. return service;
  54. }
  55. @Bean
  56. public WxMpQrcodeService wxMpQrcodeService(WxMpService wxMpService) {
  57. return new WxMpQrcodeServiceImpl(wxMpService);
  58. }
  59. @Bean
  60. public WxMpMessageRouter wxMpMessageRouter(WxMpService wxMpService) {
  61. final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService);
  62. // 记录所有事件的日志 (异步执行)
  63. newRouter.rule().handler(this.logHandler).next();
  64. // 关注事件
  65. newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
  66. .event(WxConsts.EventType.SUBSCRIBE).handler(this.subscribeHandler)
  67. .end();
  68. // 取消关注事件
  69. newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
  70. .event(WxConsts.EventType.UNSUBSCRIBE)
  71. .handler(this.unsubscribeHandler).end();
  72. // 扫码事件
  73. newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
  74. .event(WxConsts.EventType.SCAN).handler(this.scanHandler).end();
  75. // 自定义菜单事件
  76. newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
  77. .event(WxConsts.MenuButtonType.CLICK).handler(this.menuHandler).end();
  78. // 点击菜单连接事件
  79. newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
  80. .event(WxConsts.MenuButtonType.VIEW).handler(this.nullHandler).end();
  81. // 默认
  82. newRouter.rule().async(false).handler(this.msgHandler).end();
  83. return newRouter;
  84. }
  85. }