You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					295 lines
				
				17 KiB
			
		
		
			
		
	
	
					295 lines
				
				17 KiB
			| 
											3 years ago
										 | //package com.ynxbd.wx.servlet;
 | ||
|  | //
 | ||
|  | //import com.ynxbd.api.utils.MessageUtil;
 | ||
|  | //import com.ynxbd.api.config.WeChatConfig;
 | ||
|  | //import org.apache.log4j.Logger;
 | ||
|  | //import weixin.popular.bean.message.EventMessage;
 | ||
|  | //import weixin.popular.bean.xmlmessage.XMLMessage;
 | ||
|  | //import weixin.popular.bean.xmlmessage.XMLNewsMessage;
 | ||
|  | //import weixin.popular.bean.xmlmessage.XMLNewsMessage.Article;
 | ||
|  | //import weixin.popular.bean.xmlmessage.XMLTextMessage;
 | ||
|  | //import weixin.popular.support.ExpireKey;
 | ||
|  | //import weixin.popular.support.expirekey.DefaultExpireKey;
 | ||
|  | //import weixin.popular.util.SignatureUtil;
 | ||
|  | //import weixin.popular.util.StreamUtils;
 | ||
|  | //import weixin.popular.util.XMLConverUtil;
 | ||
|  | //
 | ||
|  | //import javax.servlet.ServletException;
 | ||
|  | //import javax.servlet.ServletInputStream;
 | ||
|  | //import javax.servlet.ServletOutputStream;
 | ||
|  | //import javax.servlet.http.HttpServlet;
 | ||
|  | //import javax.servlet.http.HttpServletRequest;
 | ||
|  | //import javax.servlet.http.HttpServletResponse;
 | ||
|  | //import java.io.IOException;
 | ||
|  | //import java.io.OutputStream;
 | ||
|  | //import java.io.UnsupportedEncodingException;
 | ||
|  | //import java.nio.charset.Charset;
 | ||
|  | //import java.util.ArrayList;
 | ||
|  | //import java.util.List;
 | ||
|  | //import java.util.Map;
 | ||
|  | //
 | ||
|  | //public class DemoServlet2 extends HttpServlet {
 | ||
|  | //    private final static Logger logger = Logger.getLogger(DemoServlet2.class);
 | ||
|  | //
 | ||
|  | //    // 重复通知过滤
 | ||
|  | //    private final static ExpireKey expireKey = new DefaultExpireKey();
 | ||
|  | //
 | ||
|  | //
 | ||
|  | //    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 | ||
|  | //        // 验证服务器的有效性
 | ||
|  | //        ServletInputStream inputStream = request.getInputStream();
 | ||
|  | //        ServletOutputStream outputStream = response.getOutputStream();
 | ||
|  | //        String signature = request.getParameter("signature");
 | ||
|  | //        String timestamp = request.getParameter("timestamp");
 | ||
|  | //        String nonce = request.getParameter("nonce");
 | ||
|  | //        String echostr = request.getParameter("echostr");
 | ||
|  | //
 | ||
|  | //        // 首次请求申请验证,返回echostr
 | ||
|  | //        if (echostr != null) {
 | ||
|  | //            outputStreamWrite(outputStream, echostr);
 | ||
|  | //            return;
 | ||
|  | //        }
 | ||
|  | //
 | ||
|  | //        // 验证请求签名
 | ||
|  | //        if (!signature.equals(SignatureUtil.generateEventMessageSignature(WeChatConfig.getToken(), timestamp, nonce))) {
 | ||
|  | //            System.out.println("The request signature is invalid");
 | ||
|  | //            return;
 | ||
|  | //        }
 | ||
|  | //
 | ||
|  | //        if (inputStream != null) {
 | ||
|  | //            // 转换XML
 | ||
|  | //            EventMessage eventMessage = XMLConverUtil.convertToObject(EventMessage.class, inputStream);
 | ||
|  | //            String key = eventMessage.getFromUserName() + "__" + eventMessage.getToUserName() + "__"
 | ||
|  | //                    + eventMessage.getMsgId() + "__" + eventMessage.getCreateTime();
 | ||
|  | //            if (expireKey.exists(key)) {
 | ||
|  | //                // 重复通知不作处理
 | ||
|  | //                return;
 | ||
|  | //            } else {
 | ||
|  | //                expireKey.add(key);
 | ||
|  | //            }
 | ||
|  | //
 | ||
|  | //            // 创建回复
 | ||
|  | //            XMLMessage xmlTextMessage = new XMLTextMessage(eventMessage.getFromUserName(), eventMessage.getToUserName(),
 | ||
|  | //                    "你好");
 | ||
|  | //            // 回复
 | ||
|  | //            xmlTextMessage.outputStreamWrite(outputStream);
 | ||
|  | //            return;
 | ||
|  | //        }
 | ||
|  | //        outputStreamWrite(outputStream, "");
 | ||
|  | //    }
 | ||
|  | //
 | ||
|  | //    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 | ||
|  | //        request.setCharacterEncoding("UTF-8");
 | ||
|  | //        response.setCharacterEncoding("UTF-8");
 | ||
|  | //
 | ||
|  | //        System.out.println("appid:" + WeChatConfig.getAppId());
 | ||
|  | //
 | ||
|  | //        // 验证服务器的有效性
 | ||
|  | //        ServletInputStream inputStream = request.getInputStream();
 | ||
|  | //        ServletOutputStream outputStream = response.getOutputStream();
 | ||
|  | //        String signature = request.getParameter("signature");
 | ||
|  | //        String timestamp = request.getParameter("timestamp");
 | ||
|  | //        String nonce = request.getParameter("nonce");
 | ||
|  | //        String echostr = request.getParameter("echostr");
 | ||
|  | //
 | ||
|  | //        // 首次请求申请验证,返回echostr
 | ||
|  | ////		if (echostr != null) {
 | ||
|  | ////			outputStreamWrite(outputStream, echostr);
 | ||
|  | ////			System.out.println("首次验证");
 | ||
|  | ////			return;
 | ||
|  | ////		}
 | ||
|  | //
 | ||
|  | ////        WxConfig = WxConfig.getInstance();
 | ||
|  | //        // 验证请求签名
 | ||
|  | ////		if (!signature.equals(SignatureUtil.generateEventMessageSignature(WxConfig.getToken(), timestamp, nonce))) {
 | ||
|  | ////			System.out.println("The request signature is invalid");
 | ||
|  | ////			return;
 | ||
|  | ////		}
 | ||
|  | //
 | ||
|  | //        // PrintWriter out = response.getWriter();
 | ||
|  | //        /*
 | ||
|  | //         * try { Map<String, String> map = MessageUtil.xmlToMap(request); if
 | ||
|  | //         * (map != null) { String toUserName = map.get("ToUserName"); String
 | ||
|  | //         * fromUserName = map.get("FromUserName"); String msgType =
 | ||
|  | //         * map.get("MsgType"); String content = map.get("Content"); String
 | ||
|  | //         * eventType = map.get("Event"); String eventKey = map.get("EventKey");
 | ||
|  | //         * System.out.println("收到消息:toUserName=" + toUserName +
 | ||
|  | //         * ", fromUserName=" + fromUserName + ", msgType=" + msgType +
 | ||
|  | //         * ", content=" + content + ", eventKey=" + eventKey + ", eventType=" +
 | ||
|  | //         * eventType);
 | ||
|  | //         *
 | ||
|  | //         * // [start]华坪县人民医院 if
 | ||
|  | //         * (WxConfig.getAppId().equals("wx18b15dc67fc96f89")) { if (content !=
 | ||
|  | //         * null) { if (content.equals("支付") || content.equals("微信支付") ||
 | ||
|  | //         * content.contains("费")) { Article article = new Article();
 | ||
|  | //         * article.setTitle("微信支付"); article.setPicurl(
 | ||
|  | //         * "https://mmbiz.qpic.cn/mmbiz_jpg/RUm4m8SVypy7nVubUuo6KgdlibcduK8l4tWnXrz7VfTO5EYOeDc9hZxR70wTQgX1sYhRR7SeND6426uczdpf1BA/0?wx_fmt=jpeg"
 | ||
|  | //         * ); article.setDescription(
 | ||
|  | //         * "请点这里进入支付页面,支付您的账单,不用再去排队缴费。如需查看检查结果,请输入“检查”。如需查看化验结果,请输入“化验”。");
 | ||
|  | //         * WxConfig WxConfig = WxConfig.getInstance();
 | ||
|  | //         * article.setUrl(WxConfig.getBaseURL() + "my-pay.jsp"); List<Article>
 | ||
|  | //         * list = new ArrayList<>(); list.add(article); XMLNewsMessage msg = new
 | ||
|  | //         * XMLNewsMessage(fromUserName, toUserName, list);
 | ||
|  | //         * msg.outputStreamWrite(response.getOutputStream()); } else if
 | ||
|  | //         * (content.equals("检查")) { Article article = new Article();
 | ||
|  | //         * article.setTitle("查看检查结果"); article.setPicurl(
 | ||
|  | //         * "https://mmbiz.qpic.cn/mmbiz_jpg/RUm4m8SVypy7nVubUuo6KgdlibcduK8l4tWnXrz7VfTO5EYOeDc9hZxR70wTQgX1sYhRR7SeND6426uczdpf1BA/0?wx_fmt=jpeg"
 | ||
|  | //         * ); article.setDescription(
 | ||
|  | //         * "请点这里查看您的检查结果,如要查看化验结果,请输入“化验”或“检验”。如需缴费,请输入“支付”。"); WxConfig
 | ||
|  | //         * WxConfig = WxConfig.getInstance();
 | ||
|  | //         * article.setUrl(WxConfig.getBaseURL() + "check.jsp"); List<Article>
 | ||
|  | //         * list = new ArrayList<>(); list.add(article); XMLNewsMessage msg = new
 | ||
|  | //         * XMLNewsMessage(fromUserName, toUserName, list);
 | ||
|  | //         * msg.outputStreamWrite(response.getOutputStream()); } else if
 | ||
|  | //         * (content.equals("检验") || content.equals("化验")) { Article article =
 | ||
|  | //         * new Article(); article.setTitle("查看化验结果"); article.setPicurl(
 | ||
|  | //         * "https://mmbiz.qpic.cn/mmbiz_jpg/RUm4m8SVypy7nVubUuo6KgdlibcduK8l4tWnXrz7VfTO5EYOeDc9hZxR70wTQgX1sYhRR7SeND6426uczdpf1BA/0?wx_fmt=jpeg"
 | ||
|  | //         * );
 | ||
|  | //         * article.setDescription("请点这里查看您的化验结果,如要查看检查结果,请输入“检查”。如需缴费,请输入“支付”。")
 | ||
|  | //         * ; WxConfig WxConfig = WxConfig.getInstance();
 | ||
|  | //         * article.setUrl(WxConfig.getBaseURL() + "inspection.jsp");
 | ||
|  | //         * List<Article> list = new ArrayList<>(); list.add(article);
 | ||
|  | //         * XMLNewsMessage msg = new XMLNewsMessage(fromUserName, toUserName,
 | ||
|  | //         * list); msg.outputStreamWrite(response.getOutputStream()); } else if
 | ||
|  | //         * (content.equals("挂号")) { Article article = new Article();
 | ||
|  | //         * article.setTitle("挂号"); article.setPicurl(
 | ||
|  | //         * "https://mmbiz.qpic.cn/mmbiz_jpg/RUm4m8SVypy7nVubUuo6KgdlibcduK8l4tWnXrz7VfTO5EYOeDc9hZxR70wTQgX1sYhRR7SeND6426uczdpf1BA/0?wx_fmt=jpeg"
 | ||
|  | //         * ); article.setDescription("请点这里进入挂号,如要查看检查结果,请输入“检查”。如需缴费,请输入“支付”。");
 | ||
|  | //         * WxConfig WxConfig = WxConfig.getInstance();
 | ||
|  | //         * article.setUrl(WxConfig.getBaseURL() + "reg.jsp"); List<Article> list
 | ||
|  | //         * = new ArrayList<>(); list.add(article); XMLNewsMessage msg = new
 | ||
|  | //         * XMLNewsMessage(fromUserName, toUserName, list);
 | ||
|  | //         * msg.outputStreamWrite(response.getOutputStream()); } } if (msgType !=
 | ||
|  | //         * null) { if (MessageUtil.MESSAGE_EVENT.equals(msgType)) { if
 | ||
|  | //         * (eventKey.equals("MYDDC")) { List<Article> list = new ArrayList<>();
 | ||
|  | //         * Article article = new Article(); article.setTitle("华坪县人民医院患者满意度调查");
 | ||
|  | //         * article.setUrl("http://mp.weixin.qq.com/s/MCswklxDHdmYr3sFP70Xdw");
 | ||
|  | //         * article.setPicurl(
 | ||
|  | //         * "https://mmbiz.qpic.cn/mmbiz_jpg/0jg8QrIOHOwYlpPRQDFKpbFQsN2ibtZqcS9H9pqX4nOCzdaAHNiciaZubLF9jiayZJBV8ADxAqL6XdbpdqolJuwJIQ/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1"
 | ||
|  | //         * ); list.add(article);
 | ||
|  | //         *
 | ||
|  | //         * article = new Article(); article.setTitle("患者满意度调查");
 | ||
|  | //         * article.setUrl("http://mp.weixin.qq.com/s/7l2YsE2Y-XSTWEXN7BJlIQ");
 | ||
|  | //         * article.setPicurl(
 | ||
|  | //         * "https://mmbiz.qpic.cn/mmbiz_jpg/0jg8QrIOHOzYGDMbzzyKicXD9QlRA5MdmpGgicibSdY0A4kmgV94qibuKylgRXbMgPb4zGNw9BvWqDXiby1XyFCFCaQ/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1"
 | ||
|  | //         * ); list.add(article);
 | ||
|  | //         *
 | ||
|  | //         * article = new Article(); article.setTitle("员工满意度调查");
 | ||
|  | //         * article.setUrl("http://mp.weixin.qq.com/s/VyoOCM8FoEZ53ZQC3yX-Yw");
 | ||
|  | //         * article.setPicurl(
 | ||
|  | //         * "https://mmbiz.qpic.cn/mmbiz_jpg/0jg8QrIOHOzYGDMbzzyKicXD9QlRA5MdmpF19PW1oFibvCDj60twgfGC556fY3wThlAhIYJsHykiaDIU8h3Igh8Dw/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1"
 | ||
|  | //         * ); list.add(article); XMLNewsMessage msg = new
 | ||
|  | //         * XMLNewsMessage(fromUserName, toUserName, list);
 | ||
|  | //         * msg.outputStreamWrite(response.getOutputStream()); } if
 | ||
|  | //         * (MessageUtil.EVENT_SUB.equals(eventType)) { Article article = new
 | ||
|  | //         * Article(); article.setTitle("微信支付使用流程!"); article.setDescription(
 | ||
|  | //         * "首先关注微信公众号“华坪县人民医院”,其次打开公众号,点击“微信支付”,首次需进行身份绑定,按要求输入相关内容,获取账单;最多可以绑定除本人外加4个人,在身份绑定页面,点“添加绑定”"
 | ||
|  | //         * ); List<Article> list = new ArrayList<>(); list.add(article);
 | ||
|  | //         * XMLNewsMessage msg = new XMLNewsMessage(fromUserName, toUserName,
 | ||
|  | //         * list); msg.outputStreamWrite(response.getOutputStream()); } } } } //
 | ||
|  | //         * [end] // [start]巍山县人民医院 else if
 | ||
|  | //         * (WxConfig.getAppId().equals("wx1e30610a28d189a8")) {
 | ||
|  | //         * logger.info("巍山县人民医院收到消息:toUserName=" + toUserName +
 | ||
|  | //         * ", fromUserName=" + fromUserName + ", msgType=" + msgType +
 | ||
|  | //         * ", content=" + content + ", eventKey=" + eventKey + ", eventType=" +
 | ||
|  | //         * eventType); if (msgType != null) { if
 | ||
|  | //         * (MessageUtil.MESSAGE_EVENT.equals(msgType)) { // 满意度 if
 | ||
|  | //         * (eventKey.equals("MYDDC")) { Article article = new Article();
 | ||
|  | //         * article.setTitle("巍山县人民医院患者满意度调查");
 | ||
|  | //         * article.setUrl("https://mp.weixin.qq.com/s/R-tX9YtRRM9Hpw2rMmw2iQ");
 | ||
|  | //         * article.setPicurl(
 | ||
|  | //         * "https://mmbiz.qpic.cn/mmbiz_jpg/5kpYU2jcMVicvMfVRATtAKOEjh4A4VvtHmCMKgD9vAfr741FDDEwiccNdj2ZTicBibeeQZfwp6zbaYMI5ubib1TaaeA/640?wx_fmt=jpeg"
 | ||
|  | //         * ); List<Article> list = new ArrayList<>(); list.add(article);
 | ||
|  | //         * XMLNewsMessage msg = new XMLNewsMessage(fromUserName, toUserName,
 | ||
|  | //         * list); msg.outputStreamWrite(response.getOutputStream()); } // 关注 if
 | ||
|  | //         * (MessageUtil.EVENT_SUB.equals(eventType)) { Article article = new
 | ||
|  | //         * Article(); article.setTitle("微信预约挂号开通喽,大家快快用起来!");
 | ||
|  | //         * article.setUrl("http://mp.weixin.qq.com/s/gl_NitQTP6_ZzoV8pwxOcw");
 | ||
|  | //         * article.setPicurl(
 | ||
|  | //         * "http://mmbiz.qpic.cn/mmbiz_jpg/5kpYU2jcMVicNybruqia0dn6kBqiakDlrTgDkweiafibyRl9uK6bkKoiaXib0xktJIywqTDzfe9fHcq5x0gggKkurDicpg/0?wx_fmt=jpeg"
 | ||
|  | //         * ); List<Article> list = new ArrayList<>(); list.add(article);
 | ||
|  | //         * XMLNewsMessage msg = new XMLNewsMessage(fromUserName, toUserName,
 | ||
|  | //         * list); msg.outputStreamWrite(response.getOutputStream()); } } } } //
 | ||
|  | //         * [end] // [start]芒市人民医院 else if
 | ||
|  | //         * (WxConfig.getAppId().equals("wxca97aaeb3c40c0d0")) { if (msgType !=
 | ||
|  | //         * null) { if (MessageUtil.MESSAGE_EVENT.equals(msgType)) { if
 | ||
|  | //         * (MessageUtil.EVENT_SUB.equals(eventType)) { Article article = new
 | ||
|  | //         * Article(); article.setTitle("微信预约挂号开通喽,大家快快用起来!");
 | ||
|  | //         * article.setUrl("https://mp.weixin.qq.com/s/rJF1qBo1-mV5CUdRsi4Z5g");
 | ||
|  | //         * article.setPicurl(
 | ||
|  | //         * "https://mmbiz.qpic.cn/mmbiz_jpg/RUm4m8SVypy7nVubUuo6KgdlibcduK8l4tWnXrz7VfTO5EYOeDc9hZxR70wTQgX1sYhRR7SeND6426uczdpf1BA/0?wx_fmt=jpeg"
 | ||
|  | //         * ); List<Article> list = new ArrayList<>(); list.add(article);
 | ||
|  | //         * XMLNewsMessage msg = new XMLNewsMessage(fromUserName, toUserName,
 | ||
|  | //         * list); msg.outputStreamWrite(response.getOutputStream()); } } } } //
 | ||
|  | //         * [end] } } catch (Exception e) { e.printStackTrace(); }
 | ||
|  | //         */
 | ||
|  | //
 | ||
|  | //        try {
 | ||
|  | //            String xmlData = StreamUtils.copyToString(request.getInputStream(), Charset.forName("utf-8"));
 | ||
|  | //            Map<String, String> map = XMLConverUtil.convertToMap(xmlData);
 | ||
|  | //            if (map != null) {
 | ||
|  | //                String toUserName = map.get("ToUserName");
 | ||
|  | //                String fromUserName = map.get("FromUserName");
 | ||
|  | //                String msgType = map.get("MsgType");
 | ||
|  | //                String content = map.get("Content");
 | ||
|  | //                String message = "";
 | ||
|  | //                System.out.println("收到消息:toUserName=" + toUserName + ", fromUserName=" + fromUserName + ", msgType="
 | ||
|  | //                        + msgType + ", content=" + content);
 | ||
|  | //
 | ||
|  | //                // 判断请求是否事件类型 event
 | ||
|  | //                if (msgType != null) {
 | ||
|  | //                    if (MessageUtil.MESSAGE_EVENT.equals(msgType)) {
 | ||
|  | //                        String eventType = map.get("Event");
 | ||
|  | //                        // 若是关注事件 subscribe
 | ||
|  | //                        if (MessageUtil.EVENT_SUB.equals(eventType)) {
 | ||
|  | //
 | ||
|  | //                            Article article = new Article();
 | ||
|  | //
 | ||
|  | //                            // article.setDescription("首先关注微信公众号“华坪县人民医院”,其次打开公众号,点击“微信支付”,首次需进行身份绑定,按要求输入相关内容,获取账单;最多可以绑定除本人外加4个人,在身份绑定页面,点“添加绑定”");
 | ||
|  | //                            // 巍山
 | ||
|  | //                            article.setTitle("微信预约挂号开通喽,大家快快用起来!");
 | ||
|  | //                            article.setUrl("http://mp.weixin.qq.com/s/gl_NitQTP6_ZzoV8pwxOcw");
 | ||
|  | //                            article.setPicurl(
 | ||
|  | //                                    "http://mmbiz.qpic.cn/mmbiz_jpg/5kpYU2jcMVicNybruqia0dn6kBqiakDlrTgDkweiafibyRl9uK6bkKoiaXib0xktJIywqTDzfe9fHcq5x0gggKkurDicpg/0?wx_fmt=jpeg");
 | ||
|  | //                            // 芒市
 | ||
|  | //                            // article.setTitle("微信挂号、微信门诊费用支付开通喽,大家快快用起来!");
 | ||
|  | //                            // article.setUrl("https://mp.weixin.qq.com/s/rJF1qBo1-mV5CUdRsi4Z5g");
 | ||
|  | //                            // article.setPicurl("https://mmbiz.qpic.cn/mmbiz_jpg/RUm4m8SVypy7nVubUuo6KgdlibcduK8l4tWnXrz7VfTO5EYOeDc9hZxR70wTQgX1sYhRR7SeND6426uczdpf1BA/0?wx_fmt=jpeg");
 | ||
|  | //                            List<Article> list = new ArrayList<>();
 | ||
|  | //                            list.add(article);
 | ||
|  | //                            XMLNewsMessage msg = new XMLNewsMessage(fromUserName, toUserName, list);
 | ||
|  | //                            msg.outputStreamWrite(response.getOutputStream());
 | ||
|  | //
 | ||
|  | //                        }
 | ||
|  | //                    }
 | ||
|  | //                }
 | ||
|  | //            }
 | ||
|  | //        } catch (Exception e) {
 | ||
|  | //            e.printStackTrace();
 | ||
|  | //        }
 | ||
|  | //
 | ||
|  | //    }
 | ||
|  | //
 | ||
|  | //    /**
 | ||
|  | //     * 数据流输出
 | ||
|  | //     *
 | ||
|  | //     * @param outputStream
 | ||
|  | //     * @param text
 | ||
|  | //     * @return
 | ||
|  | //     */
 | ||
|  | //    private boolean outputStreamWrite(OutputStream outputStream, String text) {
 | ||
|  | //        try {
 | ||
|  | //            outputStream.write(text.getBytes("utf-8"));
 | ||
|  | //        } catch (UnsupportedEncodingException e) {
 | ||
|  | //            // TODO Auto-generated catch block
 | ||
|  | //            e.printStackTrace();
 | ||
|  | //            return false;
 | ||
|  | //        } catch (IOException e) {
 | ||
|  | //            // TODO Auto-generated catch block
 | ||
|  | //            e.printStackTrace();
 | ||
|  | //            return false;
 | ||
|  | //        }
 | ||
|  | //        return true;
 | ||
|  | //    }
 | ||
|  | //}
 |