|  | @ -1,12 +1,16 @@
 | 
												
													
														
															|  | package com.yihu.jw.hospital.endpoint.wechat;
 |  | package com.yihu.jw.hospital.endpoint.wechat;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | import com.yihu.jw.entity.base.wx.WxAccessTokenDO;
 | 
												
													
														
															|  | 
 |  | import com.yihu.jw.entity.base.wx.WxTemplateConfigDO;
 | 
												
													
														
															|  | import com.yihu.jw.hospital.service.wechat.WeiXinCoreService;
 |  | import com.yihu.jw.hospital.service.wechat.WeiXinCoreService;
 | 
												
													
														
															|  | import com.yihu.jw.restmodel.web.endpoint.EnvelopRestEndpoint;
 |  | import com.yihu.jw.restmodel.web.endpoint.EnvelopRestEndpoint;
 | 
												
													
														
															|  | import com.yihu.jw.util.wechat.WeixinMessagePushUtils;
 |  | import com.yihu.jw.util.wechat.WeixinMessagePushUtils;
 | 
												
													
														
															|  | 
 |  | import com.yihu.jw.wechat.dao.WxPushLogDao;
 | 
												
													
														
															|  | import com.yihu.jw.wechat.service.WxAccessTokenService;
 |  | import com.yihu.jw.wechat.service.WxAccessTokenService;
 | 
												
													
														
															|  | import io.swagger.annotations.Api;
 |  | import io.swagger.annotations.Api;
 | 
												
													
														
															|  | import org.apache.commons.lang3.StringUtils;
 |  | import org.apache.commons.lang3.StringUtils;
 | 
												
													
														
															|  | 
 |  | import org.json.JSONObject;
 | 
												
													
														
															|  | import org.slf4j.Logger;
 |  | import org.slf4j.Logger;
 | 
												
													
														
															|  | import org.slf4j.LoggerFactory;
 |  | import org.slf4j.LoggerFactory;
 | 
												
													
														
															|  | import org.springframework.beans.factory.annotation.Autowired;
 |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
												
											
												
													
														
															|  | @ -41,6 +45,8 @@ public class WechatCoreController extends EnvelopRestEndpoint {
 | 
												
													
														
															|  |     @Value("${wechat.id}")
 |  |     @Value("${wechat.id}")
 | 
												
													
														
															|  |     private String wechatId;
 |  |     private String wechatId;
 | 
												
													
														
															|  |     private static String wechat_token ="xAeQrX7uOD0OusIZ5JUQzrLPYIQBlqbS" ;
 |  |     private static String wechat_token ="xAeQrX7uOD0OusIZ5JUQzrLPYIQBlqbS" ;
 | 
												
													
														
															|  | 
 |  |     @Autowired
 | 
												
													
														
															|  | 
 |  |     private WxPushLogDao wxPushLogDao;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |     /**
 |  |     /**
 | 
												
													
														
															|  |      * 微信接口验证
 |  |      * 微信接口验证
 | 
												
											
												
													
														
															|  | @ -56,7 +62,7 @@ public class WechatCoreController extends EnvelopRestEndpoint {
 | 
												
													
														
															|  |             String timestamp = request.getParameter("timestamp").toString();
 |  |             String timestamp = request.getParameter("timestamp").toString();
 | 
												
													
														
															|  |             String nonce = request.getParameter("nonce").toString();
 |  |             String nonce = request.getParameter("nonce").toString();
 | 
												
													
														
															|  |             String echostr = request.getParameter("echostr").toString();
 |  |             String echostr = request.getParameter("echostr").toString();
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |             logger.info("echostr"+echostr+"===nonce"+nonce+"====signature"+signature+"====timestamp"+timestamp);
 | 
												
													
														
															|  |             if (validate(signature, timestamp, nonce)) {
 |  |             if (validate(signature, timestamp, nonce)) {
 | 
												
													
														
															|  |                 // 验证成功,返回验证码
 |  |                 // 验证成功,返回验证码
 | 
												
													
														
															|  |                 response.getWriter().print(echostr);
 |  |                 response.getWriter().print(echostr);
 | 
												
											
												
													
														
															|  | @ -70,39 +76,7 @@ public class WechatCoreController extends EnvelopRestEndpoint {
 | 
												
													
														
															|  |         }
 |  |         }
 | 
												
													
														
															|  |     }
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |     /**
 |  | 
 | 
												
													
														
															|  |      * 微信推送消息验证
 |  | 
 | 
												
													
														
															|  |      *
 |  | 
 | 
												
													
														
															|  |      * @param signature
 |  | 
 | 
												
													
														
															|  |      * @param timestamp
 |  | 
 | 
												
													
														
															|  |      * @param nonce
 |  | 
 | 
												
													
														
															|  |      * @return
 |  | 
 | 
												
													
														
															|  |      * @throws NoSuchAlgorithmException
 |  | 
 | 
												
													
														
															|  |      */
 |  | 
 | 
												
													
														
															|  |     private boolean validate(String signature, String timestamp, String nonce) throws NoSuchAlgorithmException {
 |  | 
 | 
												
													
														
															|  |         String token = this.wechat_token;
 |  | 
 | 
												
													
														
															|  |         // 字典序排序
 |  | 
 | 
												
													
														
															|  |         String[] array = new String[]{token, timestamp, nonce};
 |  | 
 | 
												
													
														
															|  |         Arrays.sort(array);
 |  | 
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |         //连接字典序排序后字符串
 |  | 
 | 
												
													
														
															|  |         String content = "";
 |  | 
 | 
												
													
														
															|  |         for (String str : array) {
 |  | 
 | 
												
													
														
															|  |             content += str;
 |  | 
 | 
												
													
														
															|  |         }
 |  | 
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |         // 解析
 |  | 
 | 
												
													
														
															|  |         MessageDigest md = MessageDigest.getInstance("SHA-1");
 |  | 
 | 
												
													
														
															|  |         String decodeStr = "";
 |  | 
 | 
												
													
														
															|  |         byte[] bytes = md.digest(content.getBytes());
 |  | 
 | 
												
													
														
															|  |         decodeStr = byteToStr(bytes);
 |  | 
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |         if (StringUtils.isNotEmpty(decodeStr) && decodeStr.equals(signature.toUpperCase())) {
 |  | 
 | 
												
													
														
															|  |             return true;
 |  | 
 | 
												
													
														
															|  |         } else {
 |  | 
 | 
												
													
														
															|  |             return false;
 |  | 
 | 
												
													
														
															|  |         }
 |  | 
 | 
												
													
														
															|  |     }
 |  | 
 | 
												
													
														
															|  |   /* *//*
 |  |   /* *//*
 | 
												
													
														
															|  |      * 接收居民微信回复的消息
 |  |      * 接收居民微信回复的消息
 | 
												
													
														
															|  |      *
 |  |      *
 | 
												
											
												
													
														
															|  | @ -153,6 +127,56 @@ public class WechatCoreController extends EnvelopRestEndpoint {
 | 
												
													
														
															|  |                     response.setHeader("Content-type", "text/html;charset=UTF-8");
 |  |                     response.setHeader("Content-type", "text/html;charset=UTF-8");
 | 
												
													
														
															|  |                     //这句话的意思,是告诉servlet用UTF-8转码,而不是用默认的ISO8859
 |  |                     //这句话的意思,是告诉servlet用UTF-8转码,而不是用默认的ISO8859
 | 
												
													
														
															|  |                     response.setCharacterEncoding("UTF-8");
 |  |                     response.setCharacterEncoding("UTF-8");
 | 
												
													
														
															|  | 
 |  |                     response.getWriter().print(null);
 | 
												
													
														
															|  | 
 |  |                 }
 | 
												
													
														
															|  | 
 |  |             } else {
 | 
												
													
														
															|  | 
 |  |                 // 验证失败
 | 
												
													
														
															|  | 
 |  |                 response.setStatus(401);
 | 
												
													
														
															|  | 
 |  |             }
 | 
												
													
														
															|  | 
 |  |         } catch (Exception e) {
 | 
												
													
														
															|  | 
 |  |             e.printStackTrace();
 | 
												
													
														
															|  | 
 |  |             // 服务器错误
 | 
												
													
														
															|  | 
 |  |             response.setStatus(500);
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  |     }*/
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     /**
 | 
												
													
														
															|  | 
 |  |      * 接收居民微信回复的消息
 | 
												
													
														
															|  | 
 |  |      *
 | 
												
													
														
															|  | 
 |  |      * @return
 | 
												
													
														
															|  | 
 |  |      */
 | 
												
													
														
															|  | 
 |  |     @RequestMapping(method = RequestMethod.POST)
 | 
												
													
														
															|  | 
 |  |     @ResponseBody
 | 
												
													
														
															|  | 
 |  |     public void receiveMessages(HttpServletRequest request, HttpServletResponse response) {
 | 
												
													
														
															|  | 
 |  |         try {
 | 
												
													
														
															|  | 
 |  |             String signature = request.getParameter("signature").toString();
 | 
												
													
														
															|  | 
 |  |             String timestamp = request.getParameter("timestamp").toString();
 | 
												
													
														
															|  | 
 |  |             String nonce = request.getParameter("nonce").toString();
 | 
												
													
														
															|  | 
 |  |             WxAccessTokenDO wxAccessTokenDO = wxAccessTokenService.getWxAccessTokenById(wechatId);
 | 
												
													
														
															|  | 
 |  |             if (validate(signature, timestamp, nonce)) {
 | 
												
													
														
															|  | 
 |  |                 String xmlStr = weiXinCoreService.messageProcess(request, wxAccessTokenDO.getAccessToken());
 | 
												
													
														
															|  | 
 |  |                 // 判断返回值是xml、json格式(取关是空串)
 | 
												
													
														
															|  | 
 |  |                 Boolean flag = weiXinCoreService.isXML(xmlStr);
 | 
												
													
														
															|  | 
 |  |                 if (xmlStr == "error") {
 | 
												
													
														
															|  | 
 |  |                     // 服务器错误
 | 
												
													
														
															|  | 
 |  |                     response.setStatus(500);
 | 
												
													
														
															|  | 
 |  |                 } else if (!flag && StringUtils.isNotEmpty(xmlStr)) {
 | 
												
													
														
															|  | 
 |  |                     JSONObject json = new JSONObject(xmlStr);
 | 
												
													
														
															|  | 
 |  |                     if (json.has("openId")) {
 | 
												
													
														
															|  | 
 |  |                         if (StringUtils.isNotEmpty(json.getString("openId")) && !("undefined".equals(json.getString("openId")))) {
 | 
												
													
														
															|  | 
 |  |                             logger.info("调用眼科微信模板消息接口的入参:AccessToken="+wxAccessTokenDO.getAccessToken()+"---Openid="+json.getString(""));
 | 
												
													
														
															|  | 
 |  |                             WxTemplateConfigDO wxTemplateConfigDO = new WxTemplateConfigDO();
 | 
												
													
														
															|  | 
 |  |                             wxTemplateConfigDO.setRemark(json.toString());
 | 
												
													
														
															|  | 
 |  |                             weixinMessagePushUtils.putWxMsg(wxAccessTokenDO.getAccessToken(), json.getString("openId"), wxTemplateConfigDO);
 | 
												
													
														
															|  | 
 |  |                             logger.info("发送成功");
 | 
												
													
														
															|  | 
 |  |                         }
 | 
												
													
														
															|  | 
 |  |                     }
 | 
												
													
														
															|  | 
 |  |                 } else {
 | 
												
													
														
															|  | 
 |  |                     // 返回消息(图文消息)
 | 
												
													
														
															|  | 
 |  |                     response.setHeader("Content-type", "text/html;charset=UTF-8");
 | 
												
													
														
															|  | 
 |  |                     //这句话的意思,是告诉servlet用UTF-8转码,而不是用默认的ISO8859
 | 
												
													
														
															|  | 
 |  |                     response.setCharacterEncoding("UTF-8");
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |                     logger.info(xmlStr);
 |  |                     logger.info(xmlStr);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
											
												
													
														
															|  | @ -169,8 +193,7 @@ public class WechatCoreController extends EnvelopRestEndpoint {
 | 
												
													
														
															|  |             response.setStatus(500);
 |  |             response.setStatus(500);
 | 
												
													
														
															|  |         }
 |  |         }
 | 
												
													
														
															|  |     }
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |      *//*
 |  | 
 | 
												
													
														
															|  | 
 |  |      /*
 | 
												
													
														
															|  |      * 微信推送消息验证
 |  |      * 微信推送消息验证
 | 
												
													
														
															|  |      *
 |  |      *
 | 
												
													
														
															|  |      * @param signature
 |  |      * @param signature
 | 
												
											
												
													
														
															|  | @ -178,7 +201,7 @@ public class WechatCoreController extends EnvelopRestEndpoint {
 | 
												
													
														
															|  |      * @param nonce
 |  |      * @param nonce
 | 
												
													
														
															|  |      * @return
 |  |      * @return
 | 
												
													
														
															|  |      * @throws NoSuchAlgorithmException
 |  |      * @throws NoSuchAlgorithmException
 | 
												
													
														
															|  |      *//*
 |  | 
 | 
												
													
														
															|  | 
 |  |      */
 | 
												
													
														
															|  |     private boolean validate(String signature, String timestamp, String nonce) throws NoSuchAlgorithmException {
 |  |     private boolean validate(String signature, String timestamp, String nonce) throws NoSuchAlgorithmException {
 | 
												
													
														
															|  |         String token = this.wechat_token;
 |  |         String token = this.wechat_token;
 | 
												
													
														
															|  |         // 字典序排序
 |  |         // 字典序排序
 | 
												
											
												
													
														
															|  | @ -204,7 +227,7 @@ public class WechatCoreController extends EnvelopRestEndpoint {
 | 
												
													
														
															|  |         }
 |  |         }
 | 
												
													
														
															|  |     }
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  |     *//*
 |  | 
 | 
												
													
														
															|  | 
 |  |     /*
 | 
												
													
														
															|  |      * 将字节数组转换为字符串
 |  |      * 将字节数组转换为字符串
 | 
												
													
														
															|  |      *
 |  |      *
 | 
												
													
														
															|  |      * @param byteArray
 |  |      * @param byteArray
 |