【fastweixin框架教程1】一分钟快速搭建微信公众平台JAVA服务器

【fastweixin框架教程1】一分钟快速搭建微信公众平台JAVA服务器

标签: 微信fastweixin框架服务器JAVA
3843人阅读 评论(0) 收藏 举报
分类:

  目前网上有很多,一分钟微信公众平台教程,但很少有快速搭建微信公众平台服务器教程。

  本文以fastweixin框架作为基础,以微信公众平台企业号作为目标,快速搭建微信公众平台服务器。


  有关Java微信公众平台服务器框架介绍可以参考:http://my.oschina.NET/ywbrj042/blog/402049

  本文目标:

  1.极其方便的实现微信公众平台服务端开发

  2、完成服务器绑定

  3、实现用户消息监听


  本文技术要求:要求大家熟悉JAVA基本语言和servlet

  很多大学同学可能对servlet不太熟悉,推荐大家学习书:《head first servlets and jsp》,除此之外不要求swing awt这些知识。


  首先这个框架已经为我们实现基本结构,但本人从实践中发现里面有很多需要自己扩充修改BUG的地方,本人使用完全自己实现自己的WeixinServletSupport方式。


  0、修改JDK:

  微信企业号均使用消息安全模式,安全模式都是AES加密,AES是米国政府采用的加密,所以强加密算法在早期是出口限制,同时在  jdk中有所限制,导致想使用安全模式,必须修改jdk内部的jar包替换为  JCE Unlimited Strength。

  否则你会收到异常java.security.InvalidKeyException:illegal Key Size


 

下面代码修改自WeixinServletSupport

说明:

1、里面有个MyMessageUtil,后续文章会讲述,修改适应weblogic的,如果不需要可以换回原框架MessageUtil

2、用户发来所有信息都可以处理,可以自行修改代码。

3、bindServer进行修改处理判断没有参数调用情况。

  1. package com.luozhuang;  
  2.   
  3. import com.github.sd4324530.fastweixin.company.handle.QYEventHandle;  
  4. import com.github.sd4324530.fastweixin.company.handle.QYMessageHandle;  
  5. import com.github.sd4324530.fastweixin.company.message.req.QYBaseEvent;  
  6. import com.github.sd4324530.fastweixin.company.message.req.QYBaseReq;  
  7. import com.github.sd4324530.fastweixin.company.message.req.QYBaseReqMsg;  
  8. import com.github.sd4324530.fastweixin.company.message.req.QYBatchJobEvent;  
  9. import com.github.sd4324530.fastweixin.company.message.req.QYEnterAgentEvent;  
  10. import com.github.sd4324530.fastweixin.company.message.req.QYEventType;  
  11. import com.github.sd4324530.fastweixin.company.message.req.QYImageReqMsg;  
  12. import com.github.sd4324530.fastweixin.company.message.req.QYLocationEvent;  
  13. import com.github.sd4324530.fastweixin.company.message.req.QYLocationReqMsg;  
  14. import com.github.sd4324530.fastweixin.company.message.req.QYMenuEvent;  
  15. import com.github.sd4324530.fastweixin.company.message.req.QYReqType;  
  16. import com.github.sd4324530.fastweixin.company.message.req.QYScanCodeEvent;  
  17. import com.github.sd4324530.fastweixin.company.message.req.QYSendPicInfoEvent;  
  18. import com.github.sd4324530.fastweixin.company.message.req.QYTextReqMsg;  
  19. import com.github.sd4324530.fastweixin.company.message.req.QYVideoReqMsg;  
  20. import com.github.sd4324530.fastweixin.company.message.req.QYVoiceReqMsg;  
  21. import com.github.sd4324530.fastweixin.company.message.resp.QYBaseRespMsg;  
  22. import com.github.sd4324530.fastweixin.company.message.resp.QYTextRespMsg;  
  23.   
  24. import com.qq.weixin.mp.aes.AesException;  
  25. import com.qq.weixin.mp.aes.WXBizMsgCrypt;  
  26.   
  27. import com.github.sd4324530.fastweixin.servlet.QYWeixinSupport;  
  28.   
  29. import com.github.sd4324530.fastweixin.util.BeanUtil;  
  30.   
  31. import com.github.sd4324530.fastweixin.util.CollectionUtil;  
  32.   
  33. import com.github.sd4324530.fastweixin.util.StrUtil;  
  34.   
  35. import com.fastwixinextend.MyMessageUtil;  
  36.   
  37. import java.io.IOException;  
  38. import java.io.PrintWriter;  
  39.   
  40. import java.util.List;  
  41. import java.util.Map;  
  42.   
  43. import javax.servlet.http.HttpServletRequest;  
  44.   
  45. import javax.servlet.http.HttpServletResponse;  
  46.   
  47. import org.slf4j.Logger;  
  48. import org.slf4j.LoggerFactory;  
  49.   
  50. public class MainServernSupport {  
  51.     private static final Logger LOG = LoggerFactory.getLogger(QYWeixinSupport.class);  
  52.     private static final Object LOCK = new Object();  
  53.     protected String fromUserName, toUserName;  
  54.   
  55.     /** 
  56.      * 微信消息处理器列表 
  57.      */  
  58.     private static List<QYMessageHandle> messageHandles;  
  59.   
  60.     /** 
  61.      * 子类重写,加入自定义的微信消息处理器,细化消息的处理 
  62.      * 
  63.      * @return 微信消息处理器列表 
  64.      */  
  65.     protected List<QYMessageHandle> initMessageHandles() {  
  66.         return null;  
  67.     }  
  68.   
  69.     /** 
  70.      * 子类重写,加入自定义的微信事件处理器,细化消息的处理 
  71.      * 
  72.      * @return 微信事件处理器列表 
  73.      */  
  74.     protected List<QYEventHandle> initEventHandles() {  
  75.         return null;  
  76.     }  
  77.   
  78.     /** 
  79.      * 微信事件处理器列表 
  80.      */  
  81.     private static List<QYEventHandle> eventHandles;  
  82.   
  83.     public MainServernSupport() {  
  84.         super();  
  85.     }  
  86.   
  87.     /** 
  88.      * @return 你的回调token 
  89.      */  
  90.     public static String getToken() {  
  91.         return "luozhuang";  
  92.     }  
  93.   
  94.     /** 
  95.      * @return "这里填写你的企业corpid" 
  96.      */  
  97.     public static String getCropId() {  
  98.         return "luozhuang";  
  99.     }  
  100.   
  101.     public static String getAgentID() {  
  102.         return "luozhuang";  
  103.     }  
  104.   
  105.     /** 
  106.      * @return "你的回调encodingAesKey " 
  107.      */  
  108.     public static String getAESKey() {  
  109.         return "luozhuang";  
  110.     }  
  111.   
  112.     public static String getAPPSecret() {  
  113.         return "luozhuang";  
  114.     }  
  115.   
  116.     /** 
  117.      * 绑定服务器的方法 
  118.      * @param request 
  119.      * @param response 
  120.      */  
  121.     public void bindServer(HttpServletRequest request,  
  122.                            HttpServletResponse response) {  
  123.         PrintWriter pw = null;  
  124.         try {  
  125.             pw = response.getWriter();  
  126.         } catch (IOException e) {  
  127.             LOG.debug(e);  
  128.         }  
  129.         if (StrUtil.isBlank(getToken()) || StrUtil.isBlank(getAESKey()) ||  
  130.             StrUtil.isBlank(getCropId())) {  
  131.             pw.write("成功");  
  132.             pw.flush();  
  133.             pw.close();  
  134.             return;  
  135.         }  
  136.         if (StrUtil.isBlank(request.getParameter("echostr")) || StrUtil.isBlank(request.getParameter("msg_signature")) || StrUtil.isBlank(request.getParameter("timestamp")) ||  
  137.                 StrUtil.isBlank(request.getParameter("nonce"))) {  
  138.                 pw.write("成功");  
  139.                 pw.flush();  
  140.                 pw.close();  
  141.                 return;  
  142.             }  
  143.         try {  
  144.             WXBizMsgCrypt pc =  
  145.                 new WXBizMsgCrypt(getToken(), getAESKey(), getCropId());  
  146.             String echoStr =  
  147.                 pc.VerifyURL(request.getParameter("msg_signature"),  
  148.                              request.getParameter("timestamp"),  
  149.                              request.getParameter("nonce"),  
  150.                              request.getParameter("echostr"));  
  151.             pw.write(echoStr);  
  152.             pw.flush();  
  153.             pw.close();  
  154.         } catch (AesException e) {  
  155.             if (StrUtil.isNotBlank(e.OrginexceptionMessage)) {  
  156.                   LOG.debug(e.OrginexceptionMessage);  
  157.             }  
  158.             LOG.debug("msg_signature",request.getParameter("msg_signature"));  
  159.             LOG.debug("timestamp",request.getParameter("timestamp"));  
  160.             LOG.debug("nonce",request.getParameter("nonce"));  
  161.             LOG.debug("echostr",request.getParameter("echostr"));  
  162.             LOG.debug(e);  
  163.         }  
  164.         finally  
  165.         {  
  166.             pw.write("");  
  167.             pw.flush();  
  168.             pw.close();  
  169.         }  
  170.          
  171.     }  
  172.   
  173.     /** 
  174.      * 处理微信服务器发来的请求方法 
  175.      * 
  176.      * @param request 
  177.      *            http请求对象 
  178.      * @return 处理消息的结果,已经是接口要求的XML的报文了 
  179.      */  
  180.     public String processRequest(HttpServletRequest request) {  
  181.   
  182.         try {  
  183.             Map<String, Object> reqMap = MyMessageUtil.parseXml(request, getToken(), getCropId(), getAESKey());  
  184.             fromUserName = (String) reqMap.get("FromUserName");  
  185.             toUserName = (String) reqMap.get("ToUserName");  
  186.   
  187.             String result = ProcessMessage(request, reqMap);  
  188.             return result;  
  189.         } catch (Exception e) {  
  190.             LOG.debug(e);  
  191.         }  
  192.         return null;  
  193.     }  
  194.   
  195.     /** 
  196.      * 处理微信传来信息 
  197.      *  
  198.      * @param request 
  199.      * @param reqMap 
  200.      * @return 
  201.      */  
  202.     public String ProcessMessage(HttpServletRequest request, Map<String, Object> reqMap) {  
  203.         String msgType = (String) reqMap.get("MsgType");  
  204.   
  205.         LOG.debug("收到消息,消息类型:{}", msgType);  
  206.   
  207.         QYBaseRespMsg msg = null;  
  208.         /// BUG修改:equalsIgnoreCase代替equals,因为微信传来的Type不分大小写  
  209.         if (msgType.equalsIgnoreCase(QYReqType.EVENT)) {  
  210.             String eventType = (String) reqMap.get("Event");  
  211.             if (QYEventType.SUBSCRIBE.equals(eventType)) {  
  212.                 QYBaseEvent event = new QYBaseEvent();  
  213.                 buildBasicEvent(reqMap, event);  
  214.                 msg = handleSubScribe(event);  
  215.                 if (BeanUtil.isNull(msg)) {  
  216.                     msg = processEventHandle(event);  
  217.                 }  
  218.   
  219.             } else if (QYEventType.UNSUBSCRIBE.equalsIgnoreCase(eventType)) {  
  220.                 QYBaseEvent event = new QYBaseEvent();  
  221.                 buildBasicEvent(reqMap, event);  
  222.                 msg = handleUnsubscribe(event);  
  223.                 if (BeanUtil.isNull(msg)) {  
  224.                     msg = processEventHandle(event);  
  225.                 }  
  226.             } else if (QYEventType.CLICK.equalsIgnoreCase(eventType)) {  
  227.                 String eventKey = (String) reqMap.get("EventKey");  
  228.                 LOG.debug("eventKey:{}", eventKey);  
  229.                 QYMenuEvent event = new QYMenuEvent(eventKey);  
  230.                 buildBasicEvent(reqMap, event);  
  231.                 msg = handleMenuClickEvent(event);  
  232.                 if (BeanUtil.isNull(msg)) {  
  233.                     msg = processEventHandle(event);  
  234.                 }  
  235.             } else if (QYEventType.VIEW.equalsIgnoreCase(eventType)) {  
  236.                 String eventKey = (String) reqMap.get("EventKey");  
  237.                 LOG.debug("eventKey:{}", eventKey);  
  238.                 QYMenuEvent event = new QYMenuEvent(eventKey);  
  239.                 buildBasicEvent(reqMap, event);  
  240.                 msg = handleMenuViewEvent(event);  
  241.                 if (BeanUtil.isNull(msg)) {  
  242.                     msg = processEventHandle(event);  
  243.                 }  
  244.             } else if (QYEventType.LOCATION.equalsIgnoreCase(eventType)) {  
  245.                 double latitude = Double.parseDouble((String) reqMap.get("Latitude"));  
  246.                 double longitude = Double.parseDouble((String) reqMap.get("Longitude"));  
  247.                 double precision = Double.parseDouble((String) reqMap.get("Precision"));  
  248.                 QYLocationEvent event = new QYLocationEvent(latitude, longitude, precision);  
  249.                 buildBasicEvent(reqMap, event);  
  250.                 msg = handleLocationEvent(event);  
  251.                 if (BeanUtil.isNull(msg)) {  
  252.                     msg = processEventHandle(event);  
  253.                 }  
  254.             } else if (QYEventType.SCANCODEPUSH.equalsIgnoreCase(eventType)  
  255.                     || QYEventType.SCANCODEWAITMSG.equalsIgnoreCase(eventType)) {  
  256.                 String eventKey = (String) reqMap.get("EventKey");  
  257.                 Map<String, Object> scanCodeInfo = (Map<String, Object>) reqMap.get("ScanCodeInfo");  
  258.                 String scanType = (String) scanCodeInfo.get("ScanType");  
  259.                 String scanResult = (String) scanCodeInfo.get("ScanResult");  
  260.                 QYScanCodeEvent event = new QYScanCodeEvent(eventKey, scanType, scanResult);  
  261.                 buildBasicEvent(reqMap, event);  
  262.                 msg = handleScanCodeEvent(event);  
  263.                 if (BeanUtil.isNull(msg)) {  
  264.                     msg = processEventHandle(event);  
  265.                 }  
  266.             } else if (QYEventType.PICPHOTOORALBUM.equalsIgnoreCase(eventType)  
  267.                     || QYEventType.PICSYSPHOTO.equalsIgnoreCase(eventType)  
  268.                     || QYEventType.PICWEIXIN.equalsIgnoreCase(eventType)) {  
  269.                 String eventKey = (String) reqMap.get("EventKey");  
  270.                 Map<String, Object> sendPicsInfo = (Map<String, Object>) reqMap.get("SendPicsInfo");  
  271.                 int count = Integer.parseInt((String) sendPicsInfo.get("Count"));  
  272.                 List<Map> picList = (List) sendPicsInfo.get("PicList");  
  273.                 QYSendPicInfoEvent event = new QYSendPicInfoEvent(eventKey, count, picList);  
  274.                 buildBasicEvent(reqMap, event);  
  275.                 msg = handleSendPicsInfoEvent(event);  
  276.                 if (BeanUtil.isNull(msg)) {  
  277.                     msg = processEventHandle(event);  
  278.                 }  
  279.             } else if (QYEventType.ENTERAGENT.equalsIgnoreCase(eventType)) {  
  280.                 QYEnterAgentEvent event = new QYEnterAgentEvent();  
  281.                 buildBasicEvent(reqMap, event);  
  282.                 msg = handleEnterAgentEvent(event);  
  283.                 if (BeanUtil.isNull(msg)) {  
  284.                     msg = processEventHandle(event);  
  285.                 }  
  286.             } else if (QYEventType.BATCHJOBRESULT.equalsIgnoreCase(eventType)) {  
  287.                 Map<String, Object> batchJob = (Map<String, Object>) reqMap.get("BatchJob");  
  288.                 String jobId = (String) batchJob.get("JobId");  
  289.                 String jobType = (String) batchJob.get("JobType");  
  290.                 int errCode = (Integer) batchJob.get("ErrCode");  
  291.                 String errMsg = (String) batchJob.get("ErrMsg");  
  292.                 QYBatchJobEvent event = new QYBatchJobEvent(jobId, jobType, errCode, errMsg);  
  293.                 buildBasicEvent(reqMap, event);  
  294.                 msg = handleBatchJobEvent(event);  
  295.                 if (BeanUtil.isNull(msg)) {  
  296.                     msg = processEventHandle(event);  
  297.                 }  
  298.             }  
  299.         } else {  
  300.             if (QYReqType.TEXT.equalsIgnoreCase(msgType)) {  
  301.                 String content = (String) reqMap.get("Content");  
  302.                 LOG.debug("文本消息内容:{}", content);  
  303.                 QYTextReqMsg textReqMsg = new QYTextReqMsg(content);  
  304.                 buildBasicReqMsg(reqMap, textReqMsg);  
  305.                 msg = handleTextMsg(textReqMsg);  
  306.                 if (BeanUtil.isNull(msg)) {  
  307.                     msg = processMessageHandle(textReqMsg);  
  308.                 }  
  309.             } else if (QYReqType.IMAGE.equalsIgnoreCase(msgType)) {  
  310.                 String picUrl = (String) reqMap.get("PicUrl");  
  311.                 String mediaId = (String) reqMap.get("MediaId");  
  312.                 QYImageReqMsg imageReqMsg = new QYImageReqMsg(picUrl, mediaId);  
  313.                 buildBasicReqMsg(reqMap, imageReqMsg);  
  314.                 msg = handleImageMsg(imageReqMsg);  
  315.                 if (BeanUtil.isNull(msg)) {  
  316.                     msg = processMessageHandle(imageReqMsg);  
  317.                 }  
  318.             } else if (QYReqType.VOICE.equalsIgnoreCase(msgType)) {  
  319.                 String format = (String) reqMap.get("Format");  
  320.                 String mediaId = (String) reqMap.get("MediaId");  
  321.                 QYVoiceReqMsg voiceReqMsg = new QYVoiceReqMsg(mediaId, format);  
  322.                 buildBasicReqMsg(reqMap, voiceReqMsg);  
  323.                 msg = handleVoiceMsg(voiceReqMsg);  
  324.                 if (BeanUtil.isNull(msg)) {  
  325.                     msg = processMessageHandle(voiceReqMsg);  
  326.                 }  
  327.             } else if (QYReqType.VIDEO.equalsIgnoreCase(msgType)) {  
  328.                 String thumbMediaId = (String) reqMap.get("ThumbMediaId");  
  329.                 String mediaId = (String) reqMap.get("MediaId");  
  330.                 QYVideoReqMsg videoReqMsg = new QYVideoReqMsg(mediaId, thumbMediaId);  
  331.                 buildBasicReqMsg(reqMap, videoReqMsg);  
  332.                 msg = handleVideoMsg(videoReqMsg);  
  333.                 if (BeanUtil.isNull(msg)) {  
  334.                     msg = processMessageHandle(videoReqMsg);  
  335.                 }  
  336.             } else if (QYReqType.SHORT_VIDEO.equalsIgnoreCase(msgType)) {  
  337.                 String thumbMediaId = (String) reqMap.get("ThumbMediaId");  
  338.                 String mediaId = (String) reqMap.get("MediaId");  
  339.                 QYVideoReqMsg videoReqMsg = new QYVideoReqMsg(mediaId, thumbMediaId);  
  340.                 buildBasicReqMsg(reqMap, videoReqMsg);  
  341.                 msg = handleShortVideoMsg(videoReqMsg);  
  342.                 if (BeanUtil.isNull(msg)) {  
  343.                     msg = processMessageHandle(videoReqMsg);  
  344.                 }  
  345.             } else if (QYReqType.LOCATION.equalsIgnoreCase(msgType)) {  
  346.                 double locationX = Double.parseDouble((String) reqMap.get("Location_X"));  
  347.                 double locationY = Double.parseDouble((String) reqMap.get("Location_Y"));  
  348.   
  349.                 // BUG:Scale是首字母大写  
  350.                 int scale = Integer.parseInt((String) reqMap.get("Scale"));  
  351.                 String label = (String) reqMap.get("Label");  
  352.                 // BUG:locationY 写成locationX  
  353.                 QYLocationReqMsg locationReqMsg = new QYLocationReqMsg(locationX, locationY, scale, label);  
  354.                 buildBasicReqMsg(reqMap, locationReqMsg);  
  355.                 msg = handleLocationMsg(locationReqMsg);  
  356.                 if (BeanUtil.isNull(msg)) {  
  357.                     msg = processMessageHandle(locationReqMsg);  
  358.                 }  
  359.             }  
  360.         }  
  361.   
  362.         String result = "";  
  363.         if (BeanUtil.nonNull(msg)) {  
  364.             msg.setFromUserName(toUserName);  
  365.             msg.setToUserName(fromUserName);  
  366.             result = msg.toXml();  
  367.             /// BUG:缺少这个标签  
  368.             result = "<xml>" + result + "</xml>";  
  369.             LOG.debug("回复内容:{}", result);  
  370.             try {  
  371.                 WXBizMsgCrypt pc = new WXBizMsgCrypt(getToken(), getAESKey(), getCropId());  
  372.                 result = pc.EncryptMsg(result, request.getParameter("timestamp"), request.getParameter("nonce"));  
  373.                 LOG.debug("加密后密文:{}", result);  
  374.             } catch (AesException e) {  
  375.                 LOG.error("加密异常", e);  
  376.             }  
  377.         }  
  378.         return result;  
  379.     }  
  380.   
  381.     /** 
  382.      * 处理文本消息,有需要时子类重写 
  383.      * 
  384.      * @param msg 
  385.      *            请求消息对象 
  386.      * @return 响应消息对象 
  387.      */  
  388.     protected QYBaseRespMsg handleTextMsg(QYTextReqMsg msg) {  
  389.         return new QYTextRespMsg("感谢您发送文本");  
  390.     }  
  391.   
  392.     /** 
  393.      * 处理图片消息,有需要时子类重写 
  394.      * 
  395.      * @param msg 
  396.      *            请求消息对象 
  397.      * @return 响应消息对象 
  398.      */  
  399.     protected QYBaseRespMsg handleImageMsg(QYImageReqMsg msg) {  
  400.         return new QYTextRespMsg("感谢您上报图片");  
  401.     }  
  402.   
  403.     /** 
  404.      * 处理语音消息,有需要时子类重写 
  405.      * 
  406.      * @param msg 
  407.      *            请求消息对象 
  408.      * @return 响应消息对象 
  409.      */  
  410.     protected QYBaseRespMsg handleVoiceMsg(QYVoiceReqMsg msg) {  
  411.         return new QYTextRespMsg("感谢您上报语音");  
  412.     }  
  413.   
  414.     /** 
  415.      * 处理视频消息,有需要时子类重写 
  416.      * 
  417.      * @param msg 
  418.      *            请求消息对象 
  419.      * @return 响应消息对象 
  420.      */  
  421.     protected QYBaseRespMsg handleVideoMsg(QYVideoReqMsg msg) {  
  422.         return new QYTextRespMsg("感谢您上报视频");  
  423.     }  
  424.   
  425.     /** 
  426.      * 处理小视频消息,有需要时子类重写 
  427.      * 
  428.      * @param msg 
  429.      *            请求消息对象 
  430.      * @return 响应消息对象 
  431.      */  
  432.     protected QYBaseRespMsg handleShortVideoMsg(QYVideoReqMsg msg) {  
  433.         return new QYTextRespMsg("感谢您上报视频");  
  434.     }  
  435.   
  436.     /** 
  437.      * 处理地理位置消息,有需要时子类重写 
  438.      * 
  439.      * @param msg 
  440.      *            请求消息对象 
  441.      * @return 响应消息对象 
  442.      */  
  443.     protected QYBaseRespMsg handleLocationMsg(QYLocationReqMsg msg) {  
  444.         return new QYTextRespMsg("感谢您上报地理位置");  
  445.     }  
  446.   
  447.     /** 
  448.      * 处理地理位置事件,有需要时子类重写 
  449.      * 
  450.      * @param event 
  451.      *            地理位置事件对象 
  452.      * @return 响应消息对象 
  453.      */  
  454.     protected QYBaseRespMsg handleLocationEvent(QYLocationEvent event) {  
  455.         return new QYTextRespMsg("感谢您上报地理位置");  
  456.     }  
  457.   
  458.     /** 
  459.      * 处理菜单点击事件,有需要时子类重写 
  460.      * 
  461.      * @param event 
  462.      *            菜单点击事件对象 
  463.      * @return 响应消息对象 
  464.      */  
  465.     protected QYBaseRespMsg handleMenuClickEvent(QYMenuEvent event) {  
  466.   
  467.         String eventKey = event.getEventKey();  
  468.         QYTextRespMsg textMessage = new QYTextRespMsg();  
  469.         // 根据key值判断用户点击的按钮  
  470.         if (eventKey.equals("登录")) {  
  471.             textMessage  
  472.                     .setContentBuilder("登录");  
  473.   
  474.         }  
  475.           
  476.         return textMessage;  
  477.     }  
  478.   
  479.     /** 
  480.      * 处理菜单跳转事件,有需要时子类重写 
  481.      * 
  482.      * @param event 
  483.      *            菜单跳转事件对象 
  484.      * @return 响应消息对象 
  485.      */  
  486.     protected QYBaseRespMsg handleMenuViewEvent(QYMenuEvent event) {  
  487.         return new QYTextRespMsg("感谢菜单跳转");  
  488.     }  
  489.   
  490.     /** 
  491.      * 处理菜单扫描推事件,有需要时子类重写 
  492.      *  
  493.      * @param event 
  494.      * @return 
  495.      */  
  496.     protected QYBaseRespMsg handleScanCodeEvent(QYScanCodeEvent event) {  
  497.         return new QYTextRespMsg("感谢您扫描二维码");  
  498.     }  
  499.   
  500.     /** 
  501.      * 处理菜单弹出相册事件,有需要时子类重写 
  502.      * 
  503.      * @param event 
  504.      *            菜单弹出相册事件 
  505.      * @return 响应的消息对象 
  506.      */  
  507.     protected QYBaseRespMsg handleSendPicsInfoEvent(QYSendPicInfoEvent event) {  
  508.         return handleDefaultEvent(event);  
  509.     }  
  510.   
  511.     /** 
  512.      * 处理用户进入应用事件,有需要时子类重写 
  513.      * 
  514.      * @param event 
  515.      *            添加应用事件对象 
  516.      * @return 响应的消息对象 
  517.      */  
  518.     protected QYBaseRespMsg handleEnterAgentEvent(QYEnterAgentEvent event) {  
  519.         return new QYTextRespMsg("感谢进入应用");  
  520.     }  
  521.   
  522.     /** 
  523.      * 处理异步任务通知事件,有需要时子类重写 
  524.      * 
  525.      * @param event 
  526.      *            添加通知事件对象 
  527.      * @return 响应的消息对象 
  528.      */  
  529.     protected QYBaseRespMsg handleBatchJobEvent(QYBatchJobEvent event) {  
  530.         return handleDefaultEvent(event);  
  531.     }  
  532.   
  533.     /** 
  534.      * 处理添加关注事件,有需要时子类重写 
  535.      * 
  536.      * @param event 
  537.      *            添加关注事件对象 
  538.      * @return 响应消息对象 
  539.      */  
  540.     protected QYBaseRespMsg handleSubScribe(QYBaseEvent event) {  
  541.         return new QYTextRespMsg("感谢您的关注");  
  542.     }  
  543.   
  544.     /** 
  545.      * 处理取消关注事件,有需要时子类重写 
  546.      * 
  547.      * @param event 
  548.      *            取消关注事件对象 
  549.      * @return 响应消息对象 
  550.      */  
  551.     protected QYBaseRespMsg handleUnsubscribe(QYBaseEvent event) {  
  552.         return null;  
  553.     }  
  554.   
  555.     protected QYBaseRespMsg handleDefaultMsg(QYBaseReqMsg msg) {  
  556.         return null;  
  557.     }  
  558.   
  559.     protected QYBaseRespMsg handleDefaultEvent(QYBaseEvent event) {  
  560.         return null;  
  561.     }  
  562.   
  563.     /** 
  564.      * 自定义的消息事件处理 
  565.      *  
  566.      * @param msg 
  567.      *            微信消息 
  568.      * @return 
  569.      */  
  570.     private QYBaseRespMsg processMessageHandle(QYBaseReqMsg msg) {  
  571.         if (CollectionUtil.isEmpty(messageHandles)) {  
  572.             synchronized (LOCK) {  
  573.                 messageHandles = this.initMessageHandles();  
  574.             }  
  575.         }  
  576.         if (CollectionUtil.isNotEmpty(messageHandles)) {  
  577.             for (QYMessageHandle messageHandle : messageHandles) {  
  578.                 QYBaseRespMsg resultMsg = null;  
  579.                 boolean result;  
  580.                 try {  
  581.                     result = messageHandle.beforeHandle(msg);  
  582.                 } catch (Exception e) {  
  583.                     result = false;  
  584.                 }  
  585.                 if (result) {  
  586.                     resultMsg = messageHandle.handle(msg);  
  587.                 }  
  588.                 if (BeanUtil.nonNull(resultMsg)) {  
  589.                     return resultMsg;  
  590.                 }  
  591.             }  
  592.         }  
  593.         return null;  
  594.     }  
  595.   
  596.     /** 
  597.      * 自定义的消息事件处理 
  598.      *  
  599.      * @param event 
  600.      * @return 
  601.      */  
  602.     private QYBaseRespMsg processEventHandle(QYBaseEvent event) {  
  603.         if (CollectionUtil.isEmpty(eventHandles)) {  
  604.             synchronized (LOCK) {  
  605.                 eventHandles = this.initEventHandles();  
  606.             }  
  607.         }  
  608.         if (CollectionUtil.isNotEmpty(eventHandles)) {  
  609.             for (QYEventHandle eventHandle : eventHandles) {  
  610.                 QYBaseRespMsg resultMsg = null;  
  611.                 boolean result;  
  612.                 try {  
  613.                     result = eventHandle.beforeHandle(event);  
  614.                 } catch (Exception e) {  
  615.                     result = false;  
  616.                 }  
  617.                 if (result) {  
  618.                     resultMsg = eventHandle.handle(event);  
  619.                 }  
  620.                 if (BeanUtil.nonNull(resultMsg)) {  
  621.                     return resultMsg;  
  622.                 }  
  623.             }  
  624.         }  
  625.         return null;  
  626.     }  
  627.   
  628.     private void buildBasicReqMsg(Map<String, Object> reqMap, QYBaseReqMsg reqMsg) {  
  629.         addBasicReqParams(reqMap, reqMsg);  
  630.         reqMsg.setMsgId((String) reqMap.get("MsgId"));  
  631.     }  
  632.   
  633.     private void buildBasicEvent(Map<String, Object> reqMap, QYBaseEvent event) {  
  634.         addBasicReqParams(reqMap, event);  
  635.         event.setEvent((String) reqMap.get("Event"));  
  636.     }  
  637.   
  638.     private void addBasicReqParams(Map<String, Object> reqMap, QYBaseReq req) {  
  639.         req.setMsgType((String) reqMap.get("MsgType"));  
  640.         req.setFromUserName((String) reqMap.get("FromUserName"));  
  641.         req.setToUserName((String) reqMap.get("ToUserName"));  
  642.         req.setCreateTime(Long.parseLong((String) reqMap.get("CreateTime")));  
  643.     }  
  644.   
  645. }  

  1. package com.luozhuang;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.PrintWriter;  
  5.   
  6. import javax.servlet.ServletException;  
  7. import javax.servlet.http.HttpServlet;  
  8. import javax.servlet.http.HttpServletRequest;  
  9. import javax.servlet.http.HttpServletResponse;  
  10.   
  11. /** 
  12.  * 请求处理的核心类 
  13.  * 
  14.  */  
  15. public class CoreServletDemo extends HttpServlet {  
  16.   
  17.     private static final long serialVersionUID = 4440739483644821986L;  
  18.     MainServernSupport support = new MainServernSupport();  
  19.   
  20.     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  21.         request.setCharacterEncoding("UTF-8");  
  22.         response.setCharacterEncoding("UTF-8");  
  23.         support.bindServer(request, response);  
  24.     }  
  25.   
  26.     /** 
  27.      * 重写servlet中的post方法,用于接收微信服务器发来的消息,置为final方法,用户已经无需重写这个方法啦 
  28.      * 
  29.      * @param request http请求对象 
  30.      * @param response http响应对象 
  31.      * @throws ServletException servlet异常 
  32.      * @throws IOException IO异常 
  33.      */  
  34.     public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  35.         // 将请求、响应的编码均设置为UTF-8(防止中文乱码)  
  36.         request.setCharacterEncoding("UTF-8");  
  37.         response.setCharacterEncoding("UTF-8");  
  38.         String resp = support.processRequest(request);  
  39.         PrintWriter out = response.getWriter();  
  40.         out.print(resp);  
  41.         out.close();  
  42.     }  
  43. }  



 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值