|
@ -1,11 +1,16 @@
|
|
|
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.restmodel.web.endpoint.EnvelopRestEndpoint;
|
|
|
import com.yihu.jw.util.wechat.WeixinMessagePushUtils;
|
|
|
import com.yihu.jw.wechat.dao.WxPushLogDao;
|
|
|
import com.yihu.jw.wechat.service.WxAccessTokenService;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.json.JSONObject;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@ -18,6 +23,9 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.security.MessageDigest;
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
/*
|
|
|
* Created by lyr on 2016/08/11.
|
|
@ -36,6 +44,9 @@ public class WechatCoreController extends EnvelopRestEndpoint {
|
|
|
private WxAccessTokenService wxAccessTokenService;
|
|
|
@Value("${wechat.id}")
|
|
|
private String wechatId;
|
|
|
private static String wechat_token ="xAeQrX7uOD0OusIZ5JUQzrLPYIQBlqbS" ;
|
|
|
@Autowired
|
|
|
private WxPushLogDao wxPushLogDao;
|
|
|
|
|
|
/**
|
|
|
* 微信接口验证
|
|
@ -51,15 +62,21 @@ public class WechatCoreController extends EnvelopRestEndpoint {
|
|
|
String timestamp = request.getParameter("timestamp").toString();
|
|
|
String nonce = request.getParameter("nonce").toString();
|
|
|
String echostr = request.getParameter("echostr").toString();
|
|
|
|
|
|
// 验证成功,返回验证码
|
|
|
response.getWriter().print(echostr);
|
|
|
logger.info("echostr"+echostr+"===nonce"+nonce+"====signature"+signature+"====timestamp"+timestamp);
|
|
|
if (validate(signature, timestamp, nonce)) {
|
|
|
// 验证成功,返回验证码
|
|
|
response.getWriter().print(echostr);
|
|
|
} else {
|
|
|
// 验证失败
|
|
|
response.setStatus(401);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
// 服务器错误
|
|
|
response.setStatus(500);
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/* *//*
|
|
|
* 接收居民微信回复的消息
|
|
|
*
|
|
@ -110,6 +127,56 @@ public class WechatCoreController extends EnvelopRestEndpoint {
|
|
|
response.setHeader("Content-type", "text/html;charset=UTF-8");
|
|
|
//这句话的意思,是告诉servlet用UTF-8转码,而不是用默认的ISO8859
|
|
|
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);
|
|
|
|
|
@ -126,8 +193,7 @@ public class WechatCoreController extends EnvelopRestEndpoint {
|
|
|
response.setStatus(500);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
*//*
|
|
|
/*
|
|
|
* 微信推送消息验证
|
|
|
*
|
|
|
* @param signature
|
|
@ -135,7 +201,7 @@ public class WechatCoreController extends EnvelopRestEndpoint {
|
|
|
* @param nonce
|
|
|
* @return
|
|
|
* @throws NoSuchAlgorithmException
|
|
|
*//*
|
|
|
*/
|
|
|
private boolean validate(String signature, String timestamp, String nonce) throws NoSuchAlgorithmException {
|
|
|
String token = this.wechat_token;
|
|
|
// 字典序排序
|
|
@ -161,12 +227,12 @@ public class WechatCoreController extends EnvelopRestEndpoint {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
*//*
|
|
|
/*
|
|
|
* 将字节数组转换为字符串
|
|
|
*
|
|
|
* @param byteArray
|
|
|
* @return
|
|
|
*//*
|
|
|
*/
|
|
|
private static String byteToStr(byte[] byteArray) {
|
|
|
String strDigest = "";
|
|
|
for (int i = 0; i < byteArray.length; i++) {
|
|
@ -177,12 +243,12 @@ public class WechatCoreController extends EnvelopRestEndpoint {
|
|
|
|
|
|
|
|
|
|
|
|
*//*
|
|
|
/*
|
|
|
* 将字节转换为字符
|
|
|
*
|
|
|
* @param mByte
|
|
|
* @return
|
|
|
*//*
|
|
|
*/
|
|
|
private static String byteToHexStr(byte mByte) {
|
|
|
char[] Digit = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
|
|
|
char[] tempArr = new char[2];
|
|
@ -190,6 +256,6 @@ public class WechatCoreController extends EnvelopRestEndpoint {
|
|
|
tempArr[1] = Digit[mByte & 0X0F];
|
|
|
String s = new String(tempArr);
|
|
|
return s;
|
|
|
}*/
|
|
|
}
|
|
|
|
|
|
}
|