WxMaMessageRouterRule.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. package cn.binarywang.wx.miniapp.message;
  2. import cn.binarywang.wx.miniapp.api.WxMaService;
  3. import cn.binarywang.wx.miniapp.bean.WxMaMessage;
  4. import me.chanjar.weixin.common.api.WxErrorExceptionHandler;
  5. import me.chanjar.weixin.common.error.WxErrorException;
  6. import me.chanjar.weixin.common.session.WxSessionManager;
  7. import java.util.ArrayList;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.regex.Pattern;
  12. /**
  13. * @author <a href="https://github.com/binarywang">Binary Wang</a>
  14. */
  15. public class WxMaMessageRouterRule {
  16. private final WxMaMessageRouter routerBuilder;
  17. private boolean async = true;
  18. private String fromUser;
  19. private String msgType;
  20. private String event;
  21. private String eventKey;
  22. private String content;
  23. private String rContent;
  24. private WxMaMessageMatcher matcher;
  25. private boolean reEnter = false;
  26. private List<WxMaMessageHandler> handlers = new ArrayList<>();
  27. private List<WxMaMessageInterceptor> interceptors = new ArrayList<>();
  28. public WxMaMessageRouterRule(WxMaMessageRouter routerBuilder) {
  29. this.routerBuilder = routerBuilder;
  30. }
  31. /**
  32. * 设置是否异步执行,默认是true.
  33. */
  34. public WxMaMessageRouterRule async(boolean async) {
  35. this.async = async;
  36. return this;
  37. }
  38. /**
  39. * 如果msgType等于某值.
  40. */
  41. public WxMaMessageRouterRule msgType(String msgType) {
  42. this.msgType = msgType;
  43. return this;
  44. }
  45. /**
  46. * 如果event等于某值.
  47. */
  48. public WxMaMessageRouterRule event(String event) {
  49. this.event = event;
  50. return this;
  51. }
  52. /**
  53. * 如果eventKey等于某值.
  54. */
  55. public WxMaMessageRouterRule eventKey(String eventKey) {
  56. this.eventKey = eventKey;
  57. return this;
  58. }
  59. /**
  60. * 如果content等于某值.
  61. */
  62. public WxMaMessageRouterRule content(String content) {
  63. this.content = content;
  64. return this;
  65. }
  66. /**
  67. * 如果content匹配该正则表达式.
  68. */
  69. public WxMaMessageRouterRule rContent(String regex) {
  70. this.rContent = regex;
  71. return this;
  72. }
  73. /**
  74. * 如果fromUser等于某值.
  75. */
  76. public WxMaMessageRouterRule fromUser(String fromUser) {
  77. this.fromUser = fromUser;
  78. return this;
  79. }
  80. /**
  81. * 如果消息匹配某个matcher,用在用户需要自定义更复杂的匹配规则的时候.
  82. */
  83. public WxMaMessageRouterRule matcher(WxMaMessageMatcher matcher) {
  84. this.matcher = matcher;
  85. return this;
  86. }
  87. /**
  88. * 设置微信消息拦截器.
  89. */
  90. public WxMaMessageRouterRule interceptor(WxMaMessageInterceptor interceptor) {
  91. return interceptor(interceptor, (WxMaMessageInterceptor[]) null);
  92. }
  93. /**
  94. * 设置微信消息拦截器.
  95. */
  96. public WxMaMessageRouterRule interceptor(WxMaMessageInterceptor interceptor, WxMaMessageInterceptor... otherInterceptors) {
  97. this.interceptors.add(interceptor);
  98. if (otherInterceptors != null && otherInterceptors.length > 0) {
  99. for (WxMaMessageInterceptor i : otherInterceptors) {
  100. this.interceptors.add(i);
  101. }
  102. }
  103. return this;
  104. }
  105. /**
  106. * 设置微信消息处理器.
  107. */
  108. public WxMaMessageRouterRule handler(WxMaMessageHandler handler) {
  109. return handler(handler, (WxMaMessageHandler[]) null);
  110. }
  111. /**
  112. * 设置微信消息处理器.
  113. */
  114. public WxMaMessageRouterRule handler(WxMaMessageHandler handler, WxMaMessageHandler... otherHandlers) {
  115. this.handlers.add(handler);
  116. if (otherHandlers != null && otherHandlers.length > 0) {
  117. for (WxMaMessageHandler i : otherHandlers) {
  118. this.handlers.add(i);
  119. }
  120. }
  121. return this;
  122. }
  123. /**
  124. * 规则结束,代表如果一个消息匹配该规则,那么它将不再会进入其他规则.
  125. */
  126. public WxMaMessageRouter end() {
  127. this.routerBuilder.getRules().add(this);
  128. return this.routerBuilder;
  129. }
  130. /**
  131. * 规则结束,但是消息还会进入其他规则.
  132. */
  133. public WxMaMessageRouter next() {
  134. this.reEnter = true;
  135. return end();
  136. }
  137. /**
  138. * 将微信自定义的事件修正为不区分大小写.
  139. * 比如框架定义的事件常量为click,但微信传递过来的却是CLICK
  140. */
  141. protected boolean test(WxMaMessage wxMessage) {
  142. return
  143. (this.fromUser == null || this.fromUser.equals(wxMessage.getFromUser()))
  144. &&
  145. (this.msgType == null || this.msgType.toLowerCase().equals((wxMessage.getMsgType() == null ? null : wxMessage.getMsgType().toLowerCase())))
  146. &&
  147. (this.event == null || this.event.toLowerCase().equals((wxMessage.getEvent() == null ? null : wxMessage.getEvent().toLowerCase())))
  148. &&
  149. (this.content == null || this.content
  150. .equals(wxMessage.getContent() == null ? null : wxMessage.getContent().trim()))
  151. &&
  152. (this.rContent == null || Pattern
  153. .matches(this.rContent, wxMessage.getContent() == null ? "" : wxMessage.getContent().trim()))
  154. &&
  155. (this.matcher == null || this.matcher.match(wxMessage))
  156. ;
  157. }
  158. /**
  159. * 处理微信推送过来的消息.
  160. */
  161. protected WxMaXmlOutMessage service(WxMaMessage wxMessage,
  162. Map<String, Object> context,
  163. WxMaService wxMaService,
  164. WxSessionManager sessionManager,
  165. WxErrorExceptionHandler exceptionHandler) {
  166. if (context == null) {
  167. context = new HashMap<>(16);
  168. }
  169. WxMaXmlOutMessage outMessage = null;
  170. try {
  171. // 如果拦截器不通过
  172. for (WxMaMessageInterceptor interceptor : this.interceptors) {
  173. if (!interceptor.intercept(wxMessage, context, wxMaService, sessionManager)) {
  174. return null;
  175. }
  176. }
  177. // 交给handler处理
  178. for (WxMaMessageHandler handler : this.handlers) {
  179. // 返回最后handler的结果
  180. if (handler == null) {
  181. continue;
  182. }
  183. outMessage = handler.handle(wxMessage, context, wxMaService, sessionManager);
  184. }
  185. } catch (WxErrorException e) {
  186. exceptionHandler.handle(e);
  187. }
  188. return outMessage;
  189. }
  190. public WxMaMessageRouter getRouterBuilder() {
  191. return this.routerBuilder;
  192. }
  193. public boolean isAsync() {
  194. return this.async;
  195. }
  196. public void setAsync(boolean async) {
  197. this.async = async;
  198. }
  199. public String getFromUser() {
  200. return this.fromUser;
  201. }
  202. public void setFromUser(String fromUser) {
  203. this.fromUser = fromUser;
  204. }
  205. public String getMsgType() {
  206. return this.msgType;
  207. }
  208. public void setMsgType(String msgType) {
  209. this.msgType = msgType;
  210. }
  211. public String getEvent() {
  212. return this.event;
  213. }
  214. public void setEvent(String event) {
  215. this.event = event;
  216. }
  217. public String getEventKey() {
  218. return this.eventKey;
  219. }
  220. public void setEventKey(String eventKey) {
  221. this.eventKey = eventKey;
  222. }
  223. public String getContent() {
  224. return this.content;
  225. }
  226. public void setContent(String content) {
  227. this.content = content;
  228. }
  229. public String getrContent() {
  230. return this.rContent;
  231. }
  232. public void setrContent(String rContent) {
  233. this.rContent = rContent;
  234. }
  235. public WxMaMessageMatcher getMatcher() {
  236. return this.matcher;
  237. }
  238. public void setMatcher(WxMaMessageMatcher matcher) {
  239. this.matcher = matcher;
  240. }
  241. public boolean isReEnter() {
  242. return this.reEnter;
  243. }
  244. public void setReEnter(boolean reEnter) {
  245. this.reEnter = reEnter;
  246. }
  247. public List<WxMaMessageHandler> getHandlers() {
  248. return this.handlers;
  249. }
  250. public void setHandlers(List<WxMaMessageHandler> handlers) {
  251. this.handlers = handlers;
  252. }
  253. public List<WxMaMessageInterceptor> getInterceptors() {
  254. return this.interceptors;
  255. }
  256. public void setInterceptors(List<WxMaMessageInterceptor> interceptors) {
  257. this.interceptors = interceptors;
  258. }
  259. }