@ApiOperation(value = "验证")
@RequestMapping(value = "/verify_wx_token")
public void verifyWXToken(HttpServletRequest request, HttpServletResponse response) throws Exception {
coreServlet.doGet(request, response);
coreServlet.doPost(request, response);
}
@Slf4j
@Service
public class CoreServlet extends HttpServlet {
/**
* 确认请求来自微信服务器
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
String msgSignature = request.getParameter("signature");
String msgTimestamp = request.getParameter("timestamp");
String msgNonce = request.getParameter("nonce");
String echostr = request.getParameter("echostr");
log.info("echostr" + echostr);
// 响应消息
try {
if (WXPublicUtils.verifyUrl(msgSignature, msgTimestamp, msgNonce)) {
try {
// out.write(echostr);
// out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (AesException e) {
e.printStackTrace();
}
}
/**
* 处理微信服务器发来的消息
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO 消息的接收、处理、响应
response.setCharacterEncoding("ISO-8859-1");
request.setCharacterEncoding("ISO-8859-1");
PrintWriter out = response.getWriter();
// 从request中取得输入流
HashMap<String, String> map = new HashMap<>();
InputStream inputStream = request.getInputStream();
// 读取输入流
SAXReader reader = new SAXReader();
Document document = null;
try {
document = reader.read(inputStream);
// 得到xml根元素
Element root = document.getRootElement();
// 得到根元素的所有子节点
List<Element> elementList = root.elements();
// 遍历所有子节点
for (Element e : elementList) {
map.put(e.getName(), e.getText());
}
if (StringUtils.isNotEmpty(map.get("EventKey"))) {
String eventKey = map.get("EventKey").toString();
String content = null;
String prefix = eventKey.substring(0, 8);
if ("qrscene_".equals(prefix)) {
content = eventKey.substring(8);
}else {
content = eventKey;
}
if (StringUtils.isNotEmpty(content)) {
String[] split = content.split(",");
String openId = map.get("FromUserName").toString();
StringBuilder builder = new StringBuilder();
log.info(split.length+"数组长度");
if (split.length == 3) {
// builder.append("<a href='http://192.168.0.243:9193/h5/#/authentication?tenantId="+ split[0]+"&projectId=" + split[1] + "¢erOrganizationId=" + split[2]+ "&openId="+openId+"\'>" + "患者验证</a>");
builder.append("<a href='https://mlzy001.idrugsafety.cn/h5/#/authentication?tenantId=" + split[0] + "&projectId=" + split[1] + "¢erOrganizationId=" + split[2] + "&openId=" + openId + "\'>" + "受试者请先进行身份验证,用于接收随访推送!</a>");
String userName = map.get("FromUserName").toString();
String toUerName = map.get("ToUserName").toString();
String s = MessageUtil.initText(toUerName, userName, builder.toString());
out.write(s);
}
}
}
// 释放资源
inputStream.close();
inputStream = null;
} catch (DocumentException e) {
e.printStackTrace();
}
}
微信公众号开发者模式
最新推荐文章于 2022-11-04 14:42:55 发布