• 微信第三方平台消息后台处理


     1 一、微信消息请求路由
     2     //注入,处理微信推送消息的处理器接口
     3       @Autowired
     4       @Qualifier("textHandler")   
     5       private WxMpMessageHandler textHandler;
     6       @Autowired
     7       @Qualifier("viewHandler")  
     8       private WxMpMessageHandler viewHandler;
     9       @Autowired
    10       @Qualifier("clickHandler") 
    11       private WxMpMessageHandler clickHandler;
    12       @Autowired
    13       @Qualifier("subscribeHandler") 
    14       private WxMpMessageHandler subscribeHandler;
    15       @Autowired
    16       @Qualifier("unsubscribeHandler")  
    17       private WxMpMessageHandler unsubscribeHandler; 
    18 
    19 /**
    20        * 初始化路由过滤规则
    21        */
    22       @PostConstruct 
    23       public void init() {
    24         wxMpService = new WxMpServiceImpl();
    25         wxMpMessageRouter = new WxMpMessageRouter(wxMpService);
    26         wxMpMessageRouter
    27             .rule()
    28                 .async(false)
    29                 .msgType(WxConsts.XML_MSG_EVENT).event(WxConsts.EVT_SUBSCRIBE) // 微信推送过来的消息的类型,和发送给微信xml格式消息的消息类型
    30                 .handler(subscribeHandler)
    31                 .end()
    32             .rule()
    33                 .async(false)
    34                 .msgType(WxConsts.XML_MSG_EVENT).event(WxConsts.EVT_UNSUBSCRIBE)
    35                 .handler(unsubscribeHandler)
    36                 .end()
    37             .rule()
    38                 .async(false)
    39                 .msgType(WxConsts.XML_MSG_EVENT).event(WxConsts.EVT_VIEW)
    40                 .handler(viewHandler)
    41                 .end()
    42             .rule()
    43                 .async(false)
    44                 .msgType(WxConsts.XML_MSG_EVENT).event(WxConsts.EVT_CLICK)
    45                 .handler(clickHandler)
    46                 .end()
    47             .rule()
    48                 .async(false)
    49                 .msgType(WxConsts.XML_MSG_TEXT)
    50                 .handler(textHandler)
    51                 .end();
    52       } 
    53  /**
    54        * 消息处理
    55        */
    56       public String getHandlePost(String xmlMsg,String appid)  {
    57           logger.info("消息处理:{}{}",xmlMsg,appid);
    58           WxMpXmlMessage message = XStreamTransformer.fromXml(WxMpXmlMessage.class, xmlMsg);
    59           WxMpXmlOutMessage outMessage = wxMpMessageRouter.route(message,appid);
    60 //          TODO
    61           //日志记录
    62 //          WXJTransLog WXJTransLog = new WXJTransLog();
    63 //          WXJTransLog.setLogid(IDUtils.getMsgId());
    64 //          WXJTransLog.setPlatformid(message.getToUserName());
    65 //          WXJTransLog.setEvent(message.getEvent());
    66 //          WXJTransLog.setEventkey(message.getEventKey());
    67 //          WXJTransLog.setFromusername(message.getFromUserName());
    68 //          WXJTransLog.setMsgtype(message.getMsgType());
    69 //          WXJTransLog.setCreagetime(DateUtil.getDate());
    70 //          WXJTransLog.setTextcontext(message.getContent());
    71 //          if(message.getMsgType().equals(WxConsts.XML_MSG_EVENT)&&message.getEvent().equals(WxConsts.EVT_TEMPLATESENDJOBFINISH)){
    72 //              
    73 //          }else{
    74 //              iWXJTransLogMapper.insertTransLog(WXJTransLog);    
    75 //          }
    76           return outMessage.toXml();
    77       }   

    二、处理微信推送消息的处理器接口

     1 /**
     2  * 处理微信推送消息的处理器接口
     3  *
     4  * @author Daniel Qian
     5  */
     6 public interface WxMpMessageHandler {
     7 
     8   /**
     9    * @param wxMessage
    10    * @param context        上下文,如果handler或interceptor之间有信息要传递,可以用这个
    11    * @param wxMpService
    12    * @param sessionManager
    13    * @return xml格式的消息,如果在异步规则里处理的话,可以返回null
    14    */
    15   public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
    16                                       Map<String, Object> context,
    17                                  WxMpService wxMpService,
    18                                       WxSessionManager sessionManager,String appid) throws WxErrorException;
    19 
    20 }

    三、处理微信消息

     1 /**
     2    * 处理微信消息
     3    * @param wxMessage
     4    */
     5   @SuppressWarnings("rawtypes")
     6 public WxMpXmlOutMessage route(final WxMpXmlMessage wxMessage,final String appid) {
     7     if (isDuplicateMessage(wxMessage)) {
     8       // 如果是重复消息,那么就不做处理
     9 //      return null;
    10         return WxMpXmlOutMessage.TEXT().build();
    11     }
    12 
    13     final List<WxMpMessageRouterRule> matchRules = new ArrayList<WxMpMessageRouterRule>();
    14     // 收集匹配的规则
    15     for (final WxMpMessageRouterRule rule : rules) {
    16       if (rule.test(wxMessage)) {
    17         matchRules.add(rule);
    18         if(!rule.isReEnter()) {
    19           break;
    20         }
    21       }
    22     }
    23 
    24     if (matchRules.size() == 0) {
    25         WxMpXmlOutMessage res = null;
    26         return res;
    27     }
    28 
    29     WxMpXmlOutMessage res = null;
    30     final List<Future> futures = new ArrayList<Future>();
    31     for (final WxMpMessageRouterRule rule : matchRules) {
    32       // 返回最后一个非异步的rule的执行结果
    33       if(rule.isAsync()) {
    34         futures.add(
    35             executorService.submit(new Runnable() {
    36               public void run() {
    37                 rule.service(wxMessage, wxMpService, sessionManager, exceptionHandler,appid);
    38               }
    39             })
    40         );
    41       } else {
    42         res = rule.service(wxMessage, wxMpService, sessionManager, exceptionHandler,appid);
    43       }
    44     }
    45     return res;
    46   }

    四、处理微信推送过来的消息rule.service

     1 /**
     2    * 处理微信推送过来的消息
     3    *
     4    * @param wxMessage
     5    * @return true 代表继续执行别的router,false 代表停止执行别的router
     6    */
     7   protected WxMpXmlOutMessage service(WxMpXmlMessage wxMessage,
     8       WxMpService wxMpService,
     9       WxSessionManager sessionManager,
    10       WxErrorExceptionHandler exceptionHandler,String appid) {
    11 
    12     try {
    13 
    14       Map<String, Object> context = new HashMap<String, Object>();
    15       // 如果拦截器不通过
    16       for (WxMpMessageInterceptor interceptor : this.interceptors) {
    17         if (!interceptor.intercept(wxMessage, context, wxMpService, sessionManager)) {
    18           return null;
    19         }
    20       }
    21 
    22       // 交给handler处理
    23       WxMpXmlOutMessage res = null;
    24       for (WxMpMessageHandler handler : this.handlers) {
    25         // 返回最后handler的结果
    26         res = handler.handle(wxMessage, context, wxMpService, sessionManager,appid);
    27       }
    28       return res;
    29     } catch (WxErrorException e) {
    30       exceptionHandler.handle(e);
    31     }
    32     return null;
    33 
    34   }

    五、此时再将之前的handler实现,处理,即可完成微信消息处理

     1 /**
     2  * 关注公众号事件处理
     3  * @author gll
     4  * @version 1.0
     5  */
     6 @Service("subscribeHandler") 
     7 public class SubscribeHandler extends BaseHandler implements WxMpMessageHandler{
     8 
     9     private static final Logger logger = LogManager.getLogger(SubscribeHandler.class);
    10     
    11     @Autowired
    12     private WxdResMessageMapper resMessageMapper;
    13     @Autowired
    14     private WxdMessageMapper messageMapper;
    15     @Autowired
    16     private WxbUserMapper userMapper;
    17     @Autowired
    18     private WxAuthService wxAuthService;    //获取accesstoken接口
  • 相关阅读:
    挑战程序设计竞赛 2.1 最基础的“穷竭搜索”
    HDU 5145 NPY and girls(莫队算法+乘法逆元)
    BZOJ 4300 绝世好题(位运算)
    HDU 5724 Chess(博弈论)
    BZOJ 1177 [Apio2009]Oil(递推)
    Codeforces 706D Vasiliy's Multiset(可持久化字典树)
    HDU 3374 String Problem (KMP+最小最大表示)
    POJ 2758 Checking the Text(Hash+二分答案)
    HDU 5782 Cycle(KMP+Hash)
    POJ 3450 Corporate Identity(KMP)
  • 原文地址:https://www.cnblogs.com/guoziyi/p/6273430.html
Copyright © 2020-2023  润新知