|
| 1 | +package com.github.binarywang.demo.wechat.config; |
| 2 | + |
| 3 | +import org.springframework.beans.factory.annotation.Autowired; |
| 4 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 5 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 6 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 7 | +import org.springframework.context.annotation.Bean; |
| 8 | +import org.springframework.context.annotation.Configuration; |
| 9 | + |
| 10 | +import com.github.binarywang.demo.wechat.handler.AbstractHandler; |
| 11 | +import com.github.binarywang.demo.wechat.handler.KfSessionHandler; |
| 12 | +import com.github.binarywang.demo.wechat.handler.LocationHandler; |
| 13 | +import com.github.binarywang.demo.wechat.handler.LogHandler; |
| 14 | +import com.github.binarywang.demo.wechat.handler.MenuHandler; |
| 15 | +import com.github.binarywang.demo.wechat.handler.MsgHandler; |
| 16 | +import com.github.binarywang.demo.wechat.handler.NullHandler; |
| 17 | +import com.github.binarywang.demo.wechat.handler.StoreCheckNotifyHandler; |
| 18 | +import com.github.binarywang.demo.wechat.handler.SubscribeHandler; |
| 19 | +import com.github.binarywang.demo.wechat.handler.UnsubscribeHandler; |
| 20 | + |
| 21 | +import me.chanjar.weixin.common.api.WxConsts; |
| 22 | +import me.chanjar.weixin.mp.api.WxMpConfigStorage; |
| 23 | +import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage; |
| 24 | +import me.chanjar.weixin.mp.api.WxMpMessageRouter; |
| 25 | +import me.chanjar.weixin.mp.api.WxMpService; |
| 26 | +import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; |
| 27 | + |
| 28 | +/** |
| 29 | + * wechat mp configuration |
| 30 | + * |
| 31 | + * @author Binary Wang |
| 32 | + */ |
| 33 | +@Configuration |
| 34 | +@ConditionalOnClass(WxMpService.class) |
| 35 | +@EnableConfigurationProperties(WechatMpProperties.class) |
| 36 | +public class WechatMpConfiguration { |
| 37 | + @Autowired |
| 38 | + private WechatMpProperties properties; |
| 39 | + |
| 40 | + @Bean |
| 41 | + @ConditionalOnMissingBean |
| 42 | + public WxMpConfigStorage configStorage() { |
| 43 | + WxMpInMemoryConfigStorage configStorage = new WxMpInMemoryConfigStorage(); |
| 44 | + configStorage.setAppId(this.properties.getAppId()); |
| 45 | + configStorage.setSecret(this.properties.getSecret()); |
| 46 | + configStorage.setToken(this.properties.getToken()); |
| 47 | + configStorage.setAesKey(this.properties.getAesKey()); |
| 48 | + configStorage.setPartnerId(this.properties.getPartnerId()); |
| 49 | + configStorage.setPartnerKey(this.properties.getPartnerKey()); |
| 50 | + return configStorage; |
| 51 | + } |
| 52 | + |
| 53 | + @Bean |
| 54 | + @ConditionalOnMissingBean |
| 55 | + public WxMpService wxMpService(WxMpConfigStorage configStorage) { |
| 56 | + WxMpService wxMpService = new WxMpServiceImpl(); |
| 57 | + wxMpService.setWxMpConfigStorage(configStorage); |
| 58 | + return wxMpService; |
| 59 | + } |
| 60 | + |
| 61 | + @Bean |
| 62 | + public WxMpMessageRouter router(WxMpService wxMpService) { |
| 63 | + final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService); |
| 64 | + |
| 65 | + // 记录所有事件的日志 |
| 66 | + newRouter.rule().handler(this.logHandler).next(); |
| 67 | + |
| 68 | + // 接收客服会话管理事件 |
| 69 | + newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT) |
| 70 | + .event(WxConsts.EVT_KF_CREATE_SESSION) |
| 71 | + .handler(this.kfSessionHandler).end(); |
| 72 | + newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT) |
| 73 | + .event(WxConsts.EVT_KF_CLOSE_SESSION).handler(this.kfSessionHandler) |
| 74 | + .end(); |
| 75 | + newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT) |
| 76 | + .event(WxConsts.EVT_KF_SWITCH_SESSION) |
| 77 | + .handler(this.kfSessionHandler).end(); |
| 78 | + |
| 79 | + // 门店审核事件 |
| 80 | + newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT) |
| 81 | + .event(WxConsts.EVT_POI_CHECK_NOTIFY) |
| 82 | + .handler(this.storeCheckNotifyHandler).end(); |
| 83 | + |
| 84 | + // 自定义菜单事件 |
| 85 | + newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT) |
| 86 | + .event(WxConsts.BUTTON_CLICK).handler(this.getMenuHandler()).end(); |
| 87 | + |
| 88 | + // 点击菜单连接事件 |
| 89 | + newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT) |
| 90 | + .event(WxConsts.BUTTON_VIEW).handler(this.nullHandler).end(); |
| 91 | + |
| 92 | + // 关注事件 |
| 93 | + newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT) |
| 94 | + .event(WxConsts.EVT_SUBSCRIBE).handler(this.getSubscribeHandler()) |
| 95 | + .end(); |
| 96 | + |
| 97 | + // 取消关注事件 |
| 98 | + newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT) |
| 99 | + .event(WxConsts.EVT_UNSUBSCRIBE) |
| 100 | + .handler(this.getUnsubscribeHandler()).end(); |
| 101 | + |
| 102 | + // 上报地理位置事件 |
| 103 | + newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT) |
| 104 | + .event(WxConsts.EVT_LOCATION).handler(this.getLocationHandler()) |
| 105 | + .end(); |
| 106 | + |
| 107 | + // 接收地理位置消息 |
| 108 | + newRouter.rule().async(false).msgType(WxConsts.XML_MSG_LOCATION) |
| 109 | + .handler(this.getLocationHandler()).end(); |
| 110 | + |
| 111 | + // 扫码事件 |
| 112 | + newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT) |
| 113 | + .event(WxConsts.EVT_SCAN).handler(this.getScanHandler()).end(); |
| 114 | + |
| 115 | + // 默认 |
| 116 | + newRouter.rule().async(false).handler(this.getMsgHandler()).end(); |
| 117 | + |
| 118 | + return newRouter; |
| 119 | + } |
| 120 | + |
| 121 | + @Autowired |
| 122 | + private LocationHandler locationHandler; |
| 123 | + |
| 124 | + @Autowired |
| 125 | + private MenuHandler menuHandler; |
| 126 | + |
| 127 | + @Autowired |
| 128 | + private MsgHandler msgHandler; |
| 129 | + |
| 130 | + @Autowired |
| 131 | + protected LogHandler logHandler; |
| 132 | + |
| 133 | + @Autowired |
| 134 | + protected NullHandler nullHandler; |
| 135 | + |
| 136 | + @Autowired |
| 137 | + protected KfSessionHandler kfSessionHandler; |
| 138 | + |
| 139 | + @Autowired |
| 140 | + protected StoreCheckNotifyHandler storeCheckNotifyHandler; |
| 141 | + |
| 142 | + @Autowired |
| 143 | + private UnsubscribeHandler unsubscribeHandler; |
| 144 | + |
| 145 | + @Autowired |
| 146 | + private SubscribeHandler subscribeHandler; |
| 147 | + |
| 148 | + protected MenuHandler getMenuHandler() { |
| 149 | + return this.menuHandler; |
| 150 | + } |
| 151 | + |
| 152 | + protected SubscribeHandler getSubscribeHandler() { |
| 153 | + return this.subscribeHandler; |
| 154 | + } |
| 155 | + |
| 156 | + protected UnsubscribeHandler getUnsubscribeHandler() { |
| 157 | + return this.unsubscribeHandler; |
| 158 | + } |
| 159 | + |
| 160 | + protected AbstractHandler getLocationHandler() { |
| 161 | + return this.locationHandler; |
| 162 | + } |
| 163 | + |
| 164 | + protected MsgHandler getMsgHandler() { |
| 165 | + return this.msgHandler; |
| 166 | + } |
| 167 | + |
| 168 | + protected AbstractHandler getScanHandler() { |
| 169 | + return null; |
| 170 | + } |
| 171 | + |
| 172 | +} |
0 commit comments