hz-zhhq-app-service/greenH5modul/.svn/pristine/49/493568a18b61e874339c314733b...

145 lines
5.1 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.jysoft.weChat.util;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class MessageUtil {
/**
* 解析微信发来的请求XML
*
* @param request
* @return map
* @throws Exception
*/
@SuppressWarnings("unchecked")
public static Map<String, String> parseXml(HttpServletRequest request) throws Exception {
// 将解析结果存储在HashMap中
Map<String, String> map = new HashMap<String, String>();
// 从request中取得输入流
InputStream inputStream = request.getInputStream();
System.out.println("获取输入流");
// 读取输入流
SAXReader reader = new SAXReader();
Document document = reader.read(inputStream);
// 得到xml根元素
Element root = document.getRootElement();
// 得到根元素的所有子节点
List<Element> elementList = root.elements();
// 遍历所有子节点
for (Element e : elementList) {
System.out.println(e.getName() + "|" + e.getText());
map.put(e.getName(), e.getText());
}
// 释放资源
inputStream.close();
inputStream = null;
return map;
}
/**
* @openID oh0lEwZQQOojVQv3lP2FHVHgzgNk 根据消息类型 构造返回消息
*/
public static String buildXml(Map<String, String> map) {
String result;
String msgType = map.get("MsgType").toString();
// 取消关注事件
if (msgType.toUpperCase().equals("EVENT") && map.get("Event").toString().toUpperCase().equals("SUBSCRIBE")) {
// 第一次关注时,进行授权
String fromUserName = map.get("FromUserName");
result = buildTextMessage(map, "欢迎关注思极锐视服务号,<a href='" + Wechatconfig.DNS + Wechatconfig.registerUrl + "?openid=" + fromUserName + "'>点击此处进行用户绑定</a>");
} else if (msgType.toUpperCase().equals("TEXT")) {
String content = map.get("Content").toString();
if (content.contains("热点")) {
result = buildTextMessage(map, "http://5wjr4e.natappfree.cc/");
} else if (content.contains("音乐")) {
result = buildMusicMessage(map);
} else {
result = buildTextMessage(map, "请输入关键字");
}
} else {
String fromUserName = map.get("FromUserName");
// 开发者微信号
String toUserName = map.get("ToUserName");
result = String.format("<xml>" + "<ToUserName><![CDATA[%s]]></ToUserName>" + "<FromUserName><![CDATA[%s]]></FromUserName>" + "<CreateTime>%s</CreateTime>" + "<MsgType><![CDATA[text]]></MsgType>" + "<Content><![CDATA[%s]]></Content>" + "</xml>", fromUserName, toUserName, getUtcTime(), "请回复如下关键词:\n文本\n图片\n语音\n视频\n音乐\n图文");
}
return result;
}
/**
* 构造文本消息
*
* @param map
* @param content
* @return
*/
private static String buildTextMessage(Map<String, String> map, String content) {
// 发送方帐号
String fromUserName = map.get("FromUserName");
// 开发者微信号
String toUserName = map.get("ToUserName");
/**
* 文本消息XML数据格式
*/
return String.format("<xml>" + "<ToUserName><![CDATA[%s]]></ToUserName>" + "<FromUserName><![CDATA[%s]]></FromUserName>" + "<CreateTime>%s</CreateTime>" + "<MsgType><![CDATA[text]]></MsgType>" + "<Content><![CDATA[%s]]></Content>" + "</xml>", fromUserName, toUserName, getUtcTime(), content);
}
private static String getUtcTime() {
Date dt = new Date();// 如果不需要格式,可直接用dt,dt就是当前系统时间
DateFormat df = new SimpleDateFormat("yyyyMMddhhmm");// 设置显示格式
String nowTime = df.format(dt);
long dd = (long) 0;
try {
dd = df.parse(nowTime).getTime();
} catch (Exception e) {
}
return String.valueOf(dd);
}
/**
* 回复音乐消息
*
* @param map
* @return
*/
private static String buildMusicMessage(Map<String, String> map) {
String fromUserName = map.get("FromUserName");
String toUserName = map.get("ToUserName");
String title = "亲爱的路人";
String description = "你想听的音乐";
String hqMusicUrl = "http://www.kugou.com/song/20qzz4f.html?frombaidu#hash=20C16B9CCCCF851D1D23AF52DD963986&album_id=0";
return String.format("<xml>" + "<ToUserName><![CDATA[%s]]></ToUserName>" + "<FromUserName><![CDATA[%s]]></FromUserName>" + "<CreateTime>%s</CreateTime>" + "<MsgType><![CDATA[music]]></MsgType>" + "<Music>" + " <Title><![CDATA[%s]]></Title>" + " <Description><![CDATA[%s]]></Description>" + " <MusicUrl>< ![CDATA[%s] ]></MusicUrl>" + // 非必须项 音乐链接
" <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>" + // 非必须项 高质量音乐链接WIFI环境优先使用该链接播放音乐
"</Music>" + "</xml>", fromUserName, toUserName, getUtcTime(), title, description, hqMusicUrl, hqMusicUrl);
}
/**
* @Author yeping
* @Description 生成6位数字随机验证码
* @Date 2019-11-3
* @Param []
* @return java.lang.String
**/
public static String getVerifyCode() {
return String.valueOf(new Random().nextInt(899999) + 100000);
}
}