import com.cyb.weixin.mp.handler.*;importcom.google.common.collect.Maps;importme.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;importme.chanjar.weixin.mp.api.WxMpMessageRouter;importme.chanjar.weixin.mp.api.WxMpService;importme.chanjar.weixin.mp.api.impl.WxMpServiceImpl;importme.chanjar.weixin.mp.constant.WxMpEventConstants;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.context.properties.EnableConfigurationProperties;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importjava.util.List;importjava.util.Map;importjava.util.stream.Collectors;import static me.chanjar.weixin.common.api.WxConsts.*;/*** wechat mp configuration
*
*@authorBinary Wang(https://github.com/binarywang)*/@Configuration
@EnableConfigurationProperties(WxMpProperties.class)public classWxMpConfiguration {privateLogHandler logHandler;privateNullHandler nullHandler;privateKfSessionHandler kfSessionHandler;privateStoreCheckNotifyHandler storeCheckNotifyHandler;privateLocationHandler locationHandler;privateMenuHandler menuHandler;privateMsgHandler msgHandler;privateUnsubscribeHandler unsubscribeHandler;privateSubscribeHandler subscribeHandler;privateScanHandler scanHandler;privateWxMpProperties properties;private static Map routers =Maps.newHashMap();private static Map mpServices =Maps.newHashMap();
@AutowiredpublicWxMpConfiguration(LogHandler logHandler, NullHandler nullHandler, KfSessionHandler kfSessionHandler,
StoreCheckNotifyHandler storeCheckNotifyHandler, LocationHandler locationHandler,
MenuHandler menuHandler, MsgHandler msgHandler, UnsubscribeHandler unsubscribeHandler,
SubscribeHandler subscribeHandler, ScanHandler scanHandler, WxMpProperties properties) {this.logHandler =logHandler;this.nullHandler =nullHandler;this.kfSessionHandler =kfSessionHandler;this.storeCheckNotifyHandler =storeCheckNotifyHandler;this.locationHandler =locationHandler;this.menuHandler =menuHandler;this.msgHandler =msgHandler;this.unsubscribeHandler =unsubscribeHandler;this.subscribeHandler =subscribeHandler;this.scanHandler =scanHandler;this.properties =properties;
}public static MapgetRouters() {returnrouters;
}public static MapgetMpServices() {returnmpServices;
}
@BeanpublicObject services() {//代码里getConfigs()处报错的同学,请注意仔细阅读项目说明,你的IDE需要引入lombok插件!!!!
final List configs = this.properties.getConfigs();if (configs == null) {throw new RuntimeException("大哥,拜托先看下项目首页的说明(readme文件),添加下相关配置,注意别配错了!");
}
mpServices= configs.stream().map(a ->{
WxMpInMemoryConfigStorage configStorage= newWxMpInMemoryConfigStorage();
configStorage.setAppId(a.getAppId());
configStorage.setSecret(a.getSecret());
configStorage.setToken(a.getToken());
configStorage.setAesKey(a.getAesKey());
WxMpService service= newWxMpServiceImpl();
service.setWxMpConfigStorage(configStorage);
routers.put(a.getAppId(),this.newRouter(service));returnservice;
}).collect(Collectors.toMap(s-> s.getWxMpConfigStorage().getAppId(), a -> a, (o, n) ->o));returnBoolean.TRUE;
}privateWxMpMessageRouter newRouter(WxMpService wxMpService) {final WxMpMessageRouter newRouter = newWxMpMessageRouter(wxMpService);//记录所有事件的日志 (异步执行)
newRouter.rule().handler(this.logHandler).next();//接收客服会话管理事件
newRouter.rule().async(false).msgType(XmlMsgType.EVENT)
.event(WxMpEventConstants.CustomerService.KF_CREATE_SESSION)
.handler(this.kfSessionHandler).end();
newRouter.rule().async(false).msgType(XmlMsgType.EVENT)
.event(WxMpEventConstants.CustomerService.KF_CLOSE_SESSION)
.handler(this.kfSessionHandler)
.end();
newRouter.rule().async(false).msgType(XmlMsgType.EVENT)
.event(WxMpEventConstants.CustomerService.KF_SWITCH_SESSION)
.handler(this.kfSessionHandler).end();//门店审核事件
newRouter.rule().async(false).msgType(XmlMsgType.EVENT)
.event(WxMpEventConstants.POI_CHECK_NOTIFY)
.handler(this.storeCheckNotifyHandler).end();//自定义菜单事件
newRouter.rule().async(false).msgType(XmlMsgType.EVENT)
.event(MenuButtonType.CLICK).handler(this.menuHandler).end();//点击菜单连接事件
newRouter.rule().async(false).msgType(XmlMsgType.EVENT)
.event(MenuButtonType.VIEW).handler(this.nullHandler).end();//关注事件
newRouter.rule().async(false).msgType(XmlMsgType.EVENT)
.event(EventType.SUBSCRIBE).handler(this.subscribeHandler)
.end();//取消关注事件
newRouter.rule().async(false).msgType(XmlMsgType.EVENT)
.event(EventType.UNSUBSCRIBE)
.handler(this.unsubscribeHandler).end();//上报地理位置事件
newRouter.rule().async(false).msgType(XmlMsgType.EVENT)
.event(EventType.LOCATION).handler(this.locationHandler)
.end();//接收地理位置消息
newRouter.rule().async(false).msgType(XmlMsgType.LOCATION)
.handler(this.locationHandler).end();//扫码事件
newRouter.rule().async(false).msgType(XmlMsgType.EVENT)
.event(EventType.SCAN).handler(this.scanHandler).end();//默认
newRouter.rule().async(false).handler(this.msgHandler).end();returnnewRouter;
}
}