首页>代码>微信公众账号开发接口实现 - java servlet>/Wechat/src/com/nko/utils/DisposeMessage.java
package com.nko.utils;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;

import com.nko.bean.Keyword;
import com.nko.bean.Parameter;

public class DisposeMessage {
//	static final String MESSAGESAVEDPATH = "F:\\code workspace\\CebbankCreditedCard\\WebRoot\\Files\\savedMessage";
	static final String MESSAGESAVEDPATH = "C:\\Wechat\\Files\\";

	/**
	 * 
	 * @param wechatXML 微信封装的XML用户消息
	 * @return
	 * @throws IOException
	 */
	public static Parameter getMostlyParameter(String wechatXML)
			throws IOException {
		Parameter param = new Parameter();
		param.setFromUserName(substring(wechatXML, "<FromUserName><![CDATA["));
		param.setToUserName(substring(wechatXML, "<ToUserName><![CDATA["));
		param.setMsgType(substring(wechatXML, "<MsgType><![CDATA["));
		if(param.getMsgType().equals("text") || param.getMsgType().equals("link"))
			param.setContent(substring(wechatXML, "<Content><![CDATA["));
		if(param.getMsgType().equals("event")) {
			param.setEvent(substring(wechatXML, "<Event><![CDATA["));
			param.setEventKey(substring(wechatXML, "<EventKey><![CDATA["));
		}
		param.setTime(substring(wechatXML, "<CreateTime>"));

		JSONArray json = new JSONArray();

		try {
			json = JSONArray.fromObject(param);
		} catch (Exception e) {
			e.printStackTrace();
		}

		DisposeFile.WriteFile(MESSAGESAVEDPATH, json.toString().getBytes());

		return param;
	}

	public static String substring(String wechatXML,  String lblPri) {
		String lblEnd = "</CreateTime>";
		if(lblPri.contains("CDATA"))
			lblEnd = "]]></" + lblPri.substring(1, lblPri.indexOf("><")) + ">";

		return wechatXML.substring(wechatXML.indexOf(lblPri)
			+ lblPri.length(), wechatXML.indexOf(lblEnd));
	}

	/**
	 * 处理并回复消息
	 * @param param
	 * @param basePath
	 * @return
	 */
	public static String replyMSG(Parameter param, String basePath) {
		String toUser = param.getToUserName();
		String fromUser = param.getFromUserName();

		if (param.getMsgType().equals("event")) {
			if(param.getEvent().equals("subscribe")) {		// 添加关注
				// 从配置文件中读取自动回复内容,暂时只回复一条
				List<Parameter> params = (List<Parameter>) DisposeFile.readFile("subscribeAutoReply.json");
				param = params.get(0);
				param.setToUserName(toUser);
				param.setFromUserName(fromUser);
			}
		} else if (param.getMsgType().equals("text")) {
			// 从配置文件中读取自动回复内容
			List<Keyword> keywords = (List<Keyword>) DisposeFile.readFile("keywordAutoReply.json");
			boolean hasKey = false;		// 标识是否存在关键词定义
			for(Keyword keyword : keywords) {
				if(param.getContent().equals(keyword.getKey())) {
					hasKey = true;
					param = keyword.getParam();
					param.setToUserName(toUser);
					param.setFromUserName(fromUser);
				}
			}
			if(!hasKey) {
				List<Parameter> params = (List<Parameter>) DisposeFile.readFile("othersAutoReply.json");
				param = params.get(0);
				param.setToUserName(toUser);
				param.setFromUserName(fromUser);
			}

//			if (param.getContent().contains("百度")) {
//				param.setContent("<a href=\"http://www.baidu.com\">百度</a>");
//			} else if (param.getContent().equals("图文")) {
//				param.setMsgType("news");
//				param.setPicUrl(basePath);		// TODO: del
//				param.setContent("此处显示的是大标题");
//	/*			param.setContent("这条消息用来显示图文");
//				Parameter p = new Parameter();
//				List<Parameter> params = new ArrayList<Parameter>();
//				p.setTitle("图文消息1");
//				p.setPicUrl(basePath + "/Image/1.png");
//				p.setDescription("description");
//				p.setUrl("http://www.baidu.com");
//				params.add(p);
//				param.setItem(params);*/
//			} else
//				param.setContent("您刚刚说的是:" + param.getContent());
		}

		// TODO: servlet /valiCard

		JSONArray json = JSONArray.fromObject(param);
		DisposeFile.WriteFile(MESSAGESAVEDPATH, json.toString().getBytes());

		return getReXML(param);
	}

	/**
	 * 
	 * @param param
	 * @param type
	 * @param content
	 * 		如果type的值为text,则值为要发送的文本
	 * 		如果type的值为image/link,则值为url
	 * @return
	 */
	public static String getReXML(Parameter param) {
		StringBuffer reXML = new StringBuffer("<xml>");
		reXML.append("<ToUserName><![CDATA[" + param.getFromUserName() + "]]></ToUserName>");
		reXML.append("<FromUserName><![CDATA[" + param.getToUserName() + "]]></FromUserName>");
		reXML.append("<CreateTime>" + new Date().getTime() + "</CreateTime>");
		reXML.append("<MsgType><![CDATA[" + param.getMsgType() + "]]></MsgType>");

		if (param.getMsgType().equals("text")) {
			reXML.append("<Content><![CDATA[" + param.getContent() + "]]></Content>");
		} else if (param.getMsgType().equals("news")) {
			reXML.append("<Content>" + param.getContent() + "</Content>");
			reXML.append("<ArticleCount>" + 2 + "</ArticleCount>");
			reXML.append("<Articles>");

			reXML.append("<item>");
			reXML.append("<Title><![CDATA[10号线故障]]></Title>");
			reXML.append("<Description><![CDATA[description1]]></Description>");
			reXML.append("<PicUrl><![CDATA[" + param.getPicUrl() + "/Image/2.png" + "]]></PicUrl>");
			reXML.append("<Url><![CDATA[url]]></Url>");
			reXML.append("</item>");
			reXML.append("<item>");
			reXML.append("<Title><![CDATA[]]></Title>");
			reXML.append("<Description><![CDATA[description2]]></Description>");
			reXML.append("<PicUrl><![CDATA[" + param.getPicUrl() + "/Image/1.png" + "]]></PicUrl>");
			reXML.append("<Url><![CDATA[url]]></Url>");
			reXML.append("</item>");

			reXML.append("</Articles>");
		}

		return reXML.toString();
	}

	public static void main(String[] args) throws IOException {
		StringBuffer sb = new StringBuffer();
		sb.append("<xml>");
		sb.append("<ToUserName><![CDATA[toUser]]></ToUserName>");
		sb.append("<FromUserName><![CDATA[fromUser]]></FromUserName>");
		sb.append("<CreateTime>1373165836587</CreateTime>");
		sb.append("<MsgType><![CDATA[link]]></MsgType>");
		sb.append("<Content><![CDATA[<a href=\"http://www.baidu.com\">这是百度链接</a>]]></Content>");
		sb.append("</xml>");
		String wechatXML = sb.toString();
		Parameter param = DisposeMessage.getMostlyParameter(wechatXML);
		param.setCategory("发送");
//		System.out.println(DisposeMessage.saveMessage(""));
		JSONArray json = JSONArray.fromObject(param);
		System.out.println(json);
	}
}
最近下载更多
liu2022  LV14 2022年7月31日
dongzhan  LV12 2021年12月16日
Tayirjan  LV11 2021年12月1日
tcloud  LV1 2021年10月30日
陈志远  LV1 2021年9月8日
seiseisizuka  LV1 2021年8月5日
yuzhang  LV2 2021年2月1日
wfqdxdx  LV6 2020年5月21日
MrZive  LV6 2020年5月6日
linghongjun5002  LV10 2020年4月15日
最近浏览更多
Gin19960217  LV4 1月12日
jerryPang  LV1 2023年6月28日
井晓码 2023年4月14日
暂无贡献等级
dengjunjun  LV15 2023年1月11日
uni-code_0123  LV1 2022年11月3日
babyFF9  LV8 2022年9月26日
微信网友_6139616720490496  LV1 2022年9月19日
xiex909  LV27 2022年8月19日
liu2022  LV14 2022年7月31日
豆子小兔子  LV9 2022年7月25日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友