业务背景
? ? ? ? 为更好的宣传和曝光自家的产品和业务,就需要快速高效的用户拉新,小裂变应用而生。通过对微信开放者平台的接口的实现,完成企业微信公众号,小程序的第三方委托,为被委托的企业微信实现微信生态内的用户增长服务。简单逻辑如下所示:
业务实现
? ? ? 由上图可知,小裂变的关键任务在于对微信后台的各种消息做出响应,传统响应方式如下图所示:
?
微信推送给公众号的消息类型很多,而公众号也需要针对用户不同的输入做出不同的反应。
如果使用if ... else ... 来实现的话非常难以维护,这时可以使用Router 来对消息进行路由。
Router 通过Rule从4个角度对消息进行匹配,然后交给事先指定的Handler :
- msgType
- event
- eventKey
-
content
在整个应用程序中,Router 是单例的,如下图所示:
?
关键代码:?
配置文件Configuration
@Bean
public WxOpenMessageRouter router() {
final WxOpenMessageRouter newRouter = new WxOpenMessageRouter(wxOpenService);
// 记录所有事件的日志 (异步执行)
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(MenuButtonType.CLICK).handler(this.getMenuHandler()).end();
// 点击菜单连接事件
newRouter.rule().async(false).msgType(XmlMsgType.EVENT)
.event(MenuButtonType.VIEW).handler(this.getMenuHandler()).end();
// 关注事件
newRouter.rule().async(false).msgType(XmlMsgType.EVENT)
.event(EventType.SUBSCRIBE)
.handler(this.getSubscribeHandler())
.next();
// 取消关注事件
newRouter.rule().async(false).msgType(XmlMsgType.EVENT)
.event(EventType.UNSUBSCRIBE)
.handler(this.getUnsubscribeHandler()).end();
// 扫码事件
newRouter.rule().async(false).msgType(XmlMsgType.EVENT)
.event(EventType.SCAN)
.handler(this.getScanHandler())
.end();
// 群发信息后的推送事件
newRouter.rule().async(false).msgType(XmlMsgType.EVENT)
.event(EventType.MASS_SEND_JOB_FINISH)
.handler(this.getMassSendCallBackHandler()).end();
// 文本消息事件
newRouter.rule().async(false)
.msgType(XmlMsgType.TEXT)
.handler(this.getMsgHandler()).end();
// 默认
newRouter.rule().async(false).handler(this.getNullHandler()).end();
return newRouter;
}
public class WxMpMessageRouter {
private final List<WxMpMessageRouterRule> rules = new ArrayList<>();
public WxMpMessageRouterRule rule() {
return new WxMpMessageRouterRule(this);
}
//处理微信消息
public WxMpXmlOutMessage route(final WxMpXmlMessage wxMessage, final Map<String, Object> context, WxMpService wxMpService) {
if (wxMpService == null) {
wxMpService = this.wxMpService;
}
final WxMpService mpService = wxMpService;
if (isMsgDuplicated(wxMessage)) {
// 如果是重复消息,那么就不做处理
return null;
}
final List<WxMpMessageRouterRule> matchRules = new ArrayList<>();
// 收集匹配的规则
for (final WxMpMessageRouterRule rule : this.rules) {
if (rule.test(wxMessage)) {
matchRules.add(rule);
if (!rule.isReEnter()) {
break;
}
}
}
if (matchRules.size() == 0) {
return null;
}
WxMpXmlOutMessage res = null;
final List<Future<?>> futures = new ArrayList<>();
for (final WxMpMessageRouterRule rule : matchRules) {
// 返回最后一个非异步的rule的执行结果
if (rule.isAsync()) {
futures.add(
this.executorService.submit(new Runnable() {
@Override
public void run() {
rule.service(wxMessage, context, mpService, WxMpMessageRouter.this.sessionManager, WxMpMessageRouter.this.exceptionHandler);
}
})
);
} else {
res = rule.service(wxMessage, context, mpService, this.sessionManager, this.exceptionHandler);
// 在同步操作结束,session访问结束
this.log.debug("End session access: async=false, sessionId={}", wxMessage.getFromUser());
sessionEndAccess(wxMessage);
}
}
if (futures.size() > 0) {
this.executorService.submit(new Runnable() {
@Override
public void run() {
for (Future<?> future : futures) {
try {
future.get();
WxMpMessageRouter.this.log.debug("End session access: async=true, sessionId={}", wxMessage.getFromUser());
// 异步操作结束,session访问结束
sessionEndAccess(wxMessage);
} catch (InterruptedException e) {
WxMpMessageRouter.this.log.error("Error happened when wait task finish", e);
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
WxMpMessageRouter.this.log.error("Error happened when wait task finish", e);
}
}
}
});
}
return res;
}
}
public class WxMpMessageRouterRule {
//匹配微信消息
protected boolean test(WxMpXmlMessage wxMessage) {
return
(this.fromUser == null || this.fromUser.equals(wxMessage.getFromUser()))
&&
(this.msgType == null || this.msgType.equalsIgnoreCase(wxMessage.getMsgType()))
&&
(this.event == null || this.event.equalsIgnoreCase(wxMessage.getEvent()))
&&
(this.eventKey == null || this.eventKey.equalsIgnoreCase(wxMessage.getEventKey()))
&&
(this.eventKeyRegex == null || Pattern.matches(this.eventKeyRegex, StringUtils.trimToEmpty(wxMessage.getEventKey())))
&&
(this.content == null || this.content.equals(StringUtils.trimToNull(wxMessage.getContent())))
&&
(this.rContent == null || Pattern.matches(this.rContent, StringUtils.trimToEmpty(wxMessage.getContent())))
&&
(this.matcher == null || this.matcher.match(wxMessage))
;
}
//处理微信推送过来的消息
//Params: wxMessage
//Returns: true 代表继续执行别的router,false 代表停止执行别的router
protected WxMpXmlOutMessage service(WxMpXmlMessage wxMessage,
Map<String, Object> context,
WxMpService wxMpService,
WxSessionManager sessionManager,
WxErrorExceptionHandler exceptionHandler) {
if (context == null) {
context = new HashMap<>();
}
try {
// 如果拦截器不通过
for (WxMpMessageInterceptor interceptor : this.interceptors) {
if (!interceptor.intercept(wxMessage, context, wxMpService, sessionManager)) {
return null;
}
}
// 交给handler处理
WxMpXmlOutMessage res = null;
for (WxMpMessageHandler handler : this.handlers) {
// 返回最后handler的结果
if (handler == null) {
continue;
}
res = handler.handle(wxMessage, context, wxMpService, sessionManager);
}
return res;
} catch (WxErrorException e) {
exceptionHandler.handle(e);
}
return null;
}
}
/**
* 处理扫码事件
*/
@Component("ScanHandler")
public class ScanHandler extends AbstractHandler {
//具体处理业务。。。
}
|