1:先创建两个实体类
import lombok.Getter; import lombok.Setter; import java.util.List; @Setter @Getter public class NewsMessage extends WxMessage{ // 图文消息个数,限制为10条以内 private int ArticleCount; // 多条图文消息信息,默认第一个item为大图 private List<Article> Articles; }
import lombok.Getter; import lombok.Setter; @Setter @Getter public class Article { // 图文消息名称 private String Title; // 图文消息描述 private String Description; // 图片链接,支持JPG、PNG格式,较好的效果为大图640*320,小图80*80 private String PicUrl; // 点击图文消息跳转链接 private String Url; }
4:创建私有方法回复图文消息
private String getWxNewsMessage(String fromUserName, String toUserName) { // 创建图文消息(回复用) NewsMessage newsMessage = new NewsMessage(); newsMessage.setToUserName(fromUserName);//开发者账号 newsMessage.setFromUserName(toUserName);//用户openId newsMessage.setCreateTime(new Date().getTime()+"");//创建时间 newsMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);//图文消息 List<Article> articleList = new ArrayList<>(); Article article = new Article(); article.setTitle("标题"); article.setDescription("描述"); article.setPicUrl("图片地址"); article.setUrl("https://www.cnblogs.com/zlw-xf/p/7221035.html"); articleList.add(article); // 设置图文消息个数 newsMessage.setArticleCount(articleList.size()); // 设置图文消息包含的图文集合 newsMessage.setArticles(articleList); // 将图文消息对象转换成xml字符串 String respMessage = MessageUtil.messageToXml(newsMessage); return respMessage; }
5:调用方法messageToXml
/** * 图文消息对象转换成xml * * @param wxNewsMessage 图文消息对象 * @return xml */ public static String messageToXml(Object wxNewsMessage){ XStream xstream = new XStream(new DomDriver("utf-8")); xstream.autodetectAnnotations(true); xstream.alias("xml", wxNewsMessage.getClass()); xstream.alias("item",new Article().getClass()); return xstream.toXML(wxNewsMessage); }
3:判断是否是关键字 调用回复图文消息
private String doReplyText(Map<String, String> requestMap) throws Exception { String respMessage = null; String fromUserName = requestMap.get("FromUserName"); // 公众帐号 String toUserName = requestMap.get("ToUserName"); // 消息类型 String msgType = requestMap.get("MsgType"); String content = requestMap.get("Content"); log.info("收到用户输入的文本消息:" + content); String replyContent=null; if (content.trim().equals("关键字")) { respMessage = getWxNewsMessage(fromUserName,toUserName); } else { respMessage = getWxTxtMessage(fromUserName,toUserName,"此功能后续开放..."); } return respMessage; }
2:判断微信公众号输入的是什么消息类型
public String doWxPost(HttpServletRequest request) { String respMessage = null; try { // xml请求解析 Map<String, String> requestMap = MessageUtil.xmlToMap(request); // 发送方帐号(open_id) String fromUserName = requestMap.get("FromUserName"); // 公众帐号 String toUserName = requestMap.get("ToUserName"); // 消息类型 String msgType = requestMap.get("MsgType"); log.info("FromUserName is:" + fromUserName + ", ToUserName is:" + toUserName + ", MsgType is:" + msgType); // 文本消息 if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) { respMessage = doReplyText(requestMap); } else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT)) { String eventType = requestMap.get("Event");// 事件类型 log.info("收到用户的事件消息:" + eventType); // 订阅 if (eventType.equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)) { /** * 订阅后默认回复 */ respMessage = doSubscribe(requestMap); } else if (eventType.equals(MessageUtil.EVENT_TYPE_UNSUBSCRIBE)) {// 取消订阅 log.info("----取消订阅----"); } else if (eventType.equals(MessageUtil.EVENT_TYPE_CLICK)) { respMessage = doMenuClickAndReturn(requestMap); } else if (eventType.equals(MessageUtil.CARD_PASS_CHECK)) { log.info("卡券审核通过"); } else if (eventType.equals(MessageUtil.CARD_NOT_PASS_CHECK)) { log.info("卡券未审核通过"); } else if (eventType.equals(MessageUtil.USER_GET_CARD)) {//用户领取卡券 doUserGetCard(requestMap); } else if (eventType.equals(MessageUtil.USER_DEL_CARD)) {//用户删除卡券 doUserDelCard(requestMap); } else if (eventType.equals(MessageUtil.USER_CONSUME_CARD)) {//核销卡券 doUserConsumeCard(requestMap); } } } catch (Exception e) { e.printStackTrace(); log.error("error:" + e.getMessage()); } return respMessage; }