国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home WeChat Applet WeChat Development Various types of message encapsulation for WeChat secondary development

Various types of message encapsulation for WeChat secondary development

May 10, 2017 am 09:29 AM
java WeChat

This article mainly introduces the third part of the secondary development of Java WeChat in detail. Various types of message encapsulation in Java WeChat have certain reference value. Interested friends can refer to this article

The example shares the encapsulation of various types of messages in Java WeChat for your reference. The specific content is as follows

Step one:Find the com.wtz.message.response package, Create a new class Image.java

package com.wtz.message.response;

/**
 * @author wangtianze QQ:864620012
 * @date 2017年4月20日 上午8:38:37
 * version:1.0
 * description:圖片的實(shí)體類
 */
public class Image {
 //圖片媒體ID
 private String MediaId;
 
 public String getMediaId() {
 return MediaId;
 }
 public void setMediaId(String mediaId) {
 MediaId = mediaId;
 }
}

The second step:Find the com.wtz.message.response package and create a new class ImageMessage.java

package com.wtz.message.response;

/**
 * @author wangtianze QQ:864620012
 * @date 2017年4月20日 上午8:49:57
 * version:1.0
 * description:圖片消息類
 */
public class ImageMessage extends BaseMessage {
 //圖片對象
 private Image image;

 public Image getImage() {
 return image;
 }

 public void setImage(Image image) {
 this.image = image;
 }
}

The third step :Find the com.wtz.message.response package and create a new class Video.java

package com.wtz.message.response;

/**
 * @author wangtianze QQ:864620012
 * @date 2017年4月20日 上午9:02:14
 * version:1.0
 * description:視頻的實(shí)體類
 */
public class Video {
 //視頻消息的媒體ID
 private String MediaId;
 //視頻消息的標(biāo)題
 private String Title;
 //視頻消息的描述
 private String Description;
 
 public String getMediaId() {
 return MediaId;
 }
 public void setMediaId(String mediaId) {
 MediaId = mediaId;
 }
 public String getTitle() {
 return Title;
 }
 public void setTitle(String title) {
 Title = title;
 }
 public String getDescription() {
 return Description;
 }
 public void setDescription(String description) {
 Description = description;
 }
}

Step 4: Find the com.wtz.message.response package and create a new class VideoMessage. java

package com.wtz.message.response;

/**
 * @author wangtianze QQ:864620012
 * @date 2017年4月20日 上午9:10:14
 * version:1.0
 * description:視頻消息類
 */
public class VideoMessage extends BaseMessage{
 //視頻對象
 private Video video;

 public Video getVideo() {
 return video;
 }

 public void setVideo(Video video) {
 this.video = video;
 }
}

The fifth step:Find the com.wtz.message.response package and create a new class Voice.java

package com.wtz.message.response;

/**
 * @author wangtianze QQ:864620012
 * @date 2017年4月20日 上午9:21:47
 * version:1.0
 * description:語音消息實(shí)體類
 */
public class Voice {
 //語音媒體編號
 private String MediaId;
 
 public String getMediaId() {
 return MediaId;
 }
 public void setMediaId(String mediaId) {
 MediaId = mediaId;
 }
}

The sixth step: Find the com.wtz.message.response package and create a new class VoiceMessage.java

package com.wtz.message.response;

/**
 * @author wangtianze QQ:864620012
 * @date 2017年4月20日 上午9:44:58
 * version:1.0</p>
 * description:語音消息類
 */
public class VoiceMessage extends BaseMessage{
 //語音對象
 private Voice voice;

 public Voice getVoice() {
 return voice;
 }

 public void setVoice(Voice voice) {
 this.voice = voice;
 }
}

Step 7: Find the com.wtz.message.response package and create a new class Music.java

package com.wtz.message.response;
/**
 * @author wangtianze QQ:864620012
 * @date 2017年4月20日 上午9:48:11
 * version:1.0
 * description:音樂消息實(shí)體類
 */
public class Music {
 //音樂標(biāo)題
 private String Title;
 //音樂描述
 private String Description;
 //音樂鏈接
 private String MusicUrl;
 //高品質(zhì)音樂鏈接
 private String HQMusicUrl;
 //縮略圖的媒體ID
 private String ThumbMediaId;
 
 public String getTitle() {
 return Title;
 }
 public void setTitle(String title) {
 Title = title;
 }
 public String getDescription() {
 return Description;
 }
 public void setDescription(String description) {
 Description = description;
 }
 public String getMusicUrl() {
 return MusicUrl;
 }
 public void setMusicUrl(String musicUrl) {
 MusicUrl = musicUrl;
 }
 public String getHQMusicUrl() {
 return HQMusicUrl;
 }
 public void setHQMusicUrl(String hQMusicUrl) {
 HQMusicUrl = hQMusicUrl;
 }
 public String getThumbMediaId() {
 return ThumbMediaId;
 }
 public void setThumbMediaId(String thumbMediaId) {
 ThumbMediaId = thumbMediaId;
 }
}

Step 8:Find the com.wtz.message.response package and create a new class MusicMessage.java

package com.wtz.message.response;

/**
 * @author wangtianze QQ:864620012
 * @date 2017年4月20日 上午9:58:21
 * <p>version:1.0</p>
 * <p>description:音樂消息類</p>
 */
public class MusicMessage extends BaseMessage{
 //音樂對象
 private Music music;

 public Music getMusic() {
 return music;
 }

 public void setMusic(Music music) {
 this.music = music;
 }
}

Step 9:Find com.wtz .message.response package, create a new class Article.java

package com.wtz.message.response;

/**
 * @author wangtianze QQ:864620012
 * @date 2017年4月20日 上午10:05:29
 * <p>version:1.0</p>
 * <p>description:圖文實(shí)體類</p>
 */
public class Article {
 //圖文標(biāo)題
 private String Title;
 //圖文描述
 private String Description;
 //圖片鏈接,支持JPG,PNG格式,較好的效果為大圖
 private String PicUrl;
 //圖文消息跳轉(zhuǎn)鏈接
 private String Url;
 
 public String getTitle() {
 return Title;
 }
 public void setTitle(String title) {
 Title = title;
 }
 public String getDescription() {
 return Description;
 }
 public void setDescription(String description) {
 Description = description;
 }
 public String getPicUrl() {
 return PicUrl;
 }
 public void setPicUrl(String picUrl) {
 PicUrl = picUrl;
 }
 public String getUrl() {
 return Url;
 }
 public void setUrl(String url) {
 Url = url;
 }
}

Step 10:Find the com.wtz.message.response package, create a new class NewsMessage.java

package com.wtz.message.response;

import java.util.List;

/**
 * @author wangtianze QQ:864620012
 * @date 2017年4月20日 上午10:16:22
 * <p>version:1.0</p>
 * <p>description:圖文消息類</p>
 */
public class NewsMessage extends BaseMessage{
 //圖文條數(shù)
 private int ArticleCount;
 //多條圖文消息信息,默認(rèn)第一個item為大圖
 private List<Article> Articles;
 
 public int getArticleCount() {
 return ArticleCount;
 }
 public void setArticleCount(int articleCount) {
 ArticleCount = articleCount;
 }
 public List<Article> getArticles() {
 return Articles;
 }
 public void setArticles(List<Article> articles) {
 Articles = articles;
 }
}

Step 11:Create new package com.wtz.message.request, create new class BaseMessage.java

package com.wtz.message.request;

/**
 * @author wangtianze QQ:864620012
 * @date 2017年4月20日 上午10:53:23
 * <p>version:1.0</p>
 * <p>description:請求消息基類(普通用戶->公眾賬號)</p>
 * <p>站在服務(wù)器端的視角:接收信息</p>
 */
public class BaseMessage {
 //開發(fā)者微信號
 private String ToUserName;
 //發(fā)送方賬號(一個OpenId)
 private String FromUserName;
 //消息創(chuàng)建時間(整型)
 private long CreateTime;
 //消息類型
 private String MsgType;
 //消息ID,64位整型
 private long MsgId;
 
 public String getToUserName() {
 return ToUserName;
 }
 public void setToUserName(String toUserName) {
 ToUserName = toUserName;
 }
 public String getFromUserName() {
 return FromUserName;
 }
 public void setFromUserName(String fromUserName) {
 FromUserName = fromUserName;
 }
 public long getCreateTime() {
 return CreateTime;
 }
 public void setCreateTime(long createTime) {
 CreateTime = createTime;
 }
 public String getMsgType() {
 return MsgType;
 }
 public void setMsgType(String msgType) {
 MsgType = msgType;
 }
 public long getMsgId() {
 return MsgId;
 }
 public void setMsgId(long msgId) {
 MsgId = msgId;
 }
}

Step 12:Find com.wtz.message .request package, create a new class TextMessage.java

package com.wtz.message.request;

/**
 * @author wangtianze QQ:864620012
 * @date 2017年4月20日 上午11:04:35
 * <p>version:1.0</p>
 * <p>description:文本消息</p>
 */
public class TextMessage extends BaseMessage{
 //消息內(nèi)容
 private String Content;

 public String getContent() {
 return Content;
 }

 public void setContent(String content) {
 Content = content;
 }
}

Step 13:Find the com.wtz.message.request package, create a new class ImageMessage.java

package com.wtz.message.request;

/**
 * @author wangtianze QQ:864620012
 * @date 2017年4月20日 上午11:20:50
 * <p>version:1.0</p>
 * <p>description:圖片消息</p>
 */
public class ImageMessage extends BaseMessage{
 //圖片鏈接
 private String PicUrl;
 //圖片消息媒體ID
 private String MediaId;
 
 public String getPicUrl() {
 return PicUrl;
 }
 public void setPicUrl(String picUrl) {
 PicUrl = picUrl;
 }
 public String getMediaId() {
 return MediaId;
 }
 public void setMediaId(String mediaId) {
 MediaId = mediaId;
 }
}

Step 14:Find the com.wtz.message.request package and create a new class VoiceMessage.java

package com.wtz.message.request;

/**
 * @author wangtianze QQ:864620012
 * @date 2017年4月20日 上午11:27:40
 * <p>version:1.0</p>
 * <p>description:語音消息類</p>
 */
public class VoiceMessage extends BaseMessage {
 //媒體ID
 private String MediaId;
 //語音格式
 private String Format;

 public String getMediaId() {
 return MediaId;
 }
 public void setMediaId(String mediaId) {
 MediaId = mediaId;
 }
 public String getFormat() {
 return Format;
 }
 public void setFormat(String format) {
 Format = format;
 }
}

Step 15:Find com.wtz.message. request package, create a new class VideoMessage.java

package com.wtz.message.request;

/**
 * @author wangtianze QQ:864620012
 * @date 2017年4月20日 下午3:28:03
 * <p>version:1.0</p>
 * <p>description:視頻消息類</p>
 */
public class VideoMessage extends BaseMessage {
 //視頻消息媒體ID
 private String MediaId;
 //視頻消息縮略圖的媒體ID
 private String ThumbMediaId;
 
 public String getMediaId() {
 return MediaId;
 }
 public void setMediaId(String mediaId) {
 MediaId = mediaId;
 }
 public String getThumbMediaId() {
 return ThumbMediaId;
 }
 public void setThumbMediaId(String thumbMediaId) {
 ThumbMediaId = thumbMediaId;
 }
}

Step 16: Find the com.wtz.message.request package, create a new class LocatonMessage.java

package com.wtz.message.request;

/**
 * @author wangtianze QQ:864620012
 * @date 2017年4月20日 下午3:27:40
 * <p>version:1.0</p>
 * <p>description:地理位置消息類</p>
 */
public class LocationMessage extends BaseMessage {
 //地理位置緯度
 private String Location_X;
 //地理位置經(jīng)度
 private String Location_Y;
 //地圖縮放大小
 private String Scale;
 //地理位置信息
 private String Label;
 
 public String getLocation_X() {
 return Location_X;
 }
 public void setLocation_X(String location_X) {
 Location_X = location_X;
 }
 public String getLocation_Y() {
 return Location_Y;
 }
 public void setLocation_Y(String location_Y) {
 Location_Y = location_Y;
 }
 public String getScale() {
 return Scale;
 }
 public void setScale(String scale) {
 Scale = scale;
 }
 public String getLabel() {
 return Label;
 }
 public void setLabel(String label) {
 Label = label;
 }
}

Step 17: Find the com.wtz.message.request package and create a new class LinkMessage.java

package com.wtz.message.request;

/**
 * @author wangtianze QQ:864620012
 * @date 2017年4月20日 下午1:58:04
 * <p>version:1.0</p>
 * <p>description:鏈接消息類</p>
 */
public class LinkMessage extends BaseMessage {
 //消息標(biāo)題
 private String Title;
 //消息描述
 private String Description;
 //消息鏈接
 private String Url;
 
 public String getTitle() {
 return Title;
 }
 public void setTitle(String title) {
 Title = title;
 }
 public String getDescription() {
 return Description;
 }
 public void setDescription(String description) {
 Description = description;
 }
 public String getUrl() {
 return Url;
 }
 public void setUrl(String url) {
 Url = url;
 }
}

Step 18: Create a new package com.wtz.message. event, create a new class BaseEventMessage.java

package com.wtz.message.event;

/**
 * @author wangtianze QQ:864620012
 * @date 2017年4月20日 上午10:44:09
 * <p>version:1.0</p>
 * <p>description:基礎(chǔ)事件消息類</p>
 */
public class BaseEventMessage {
 //開發(fā)者微信號
 private String ToUserName;
 //發(fā)送方賬號(一個OpenID)
 private String FromUserName;
 //消息創(chuàng)建時間(整型)
 private long CreateTime;
 //消息類型
 private String MsgType;
 //事件類型
 private String Event;
 
 public String getToUserName() {
 return ToUserName;
 }
 public void setToUserName(String toUserName) {
 ToUserName = toUserName;
 }
 public String getFromUserName() {
 return FromUserName;
 }
 public void setFromUserName(String fromUserName) {
 FromUserName = fromUserName;
 }
 public long getCreateTime() {
 return CreateTime;
 }
 public void setCreateTime(long createTime) {
 CreateTime = createTime;
 }
 public String getMsgType() {
 return MsgType;
 }
 public void setMsgType(String msgType) {
 MsgType = msgType;
 }
 public String getEvent() {
 return Event;
 }
 public void setEvent(String event) {
 Event = event;
 }
}

Step 19:Find the package com.wtz.message.event, create a new class QRCodeEventMessage.java

package com.wtz.message.event;

/**
 * @author wangtianze QQ:864620012
 * @date 2017年4月20日 下午3:24:30
 * <p>version:1.0</p>
 * <p>description:掃描帶參數(shù)二維碼事件類</p>
 */
public class QRCodeEventMessage extends BaseEventMessage{
 //事件Key值
 private String EventKey;
 //用于換取二維碼圖片
 private String Ticket;
 
 public String getEventKey() {
 return EventKey;
 }
 public void setEventKey(String eventKey) {
 EventKey = eventKey;
 }
 public String getTicket() {
 return Ticket;
 }
 public void setTicket(String ticket) {
 Ticket = ticket;
 } 
}

Step 20: Find the package com.wtz.message.event and create a new class SubscribeEventMessage.java

package com.wtz.message.event;

/**
 * @author wangtianze QQ:864620012
 * @date 2017年4月20日 下午3:27:11
 * <p>version:1.0</p>
 * <p>description:關(guān)注/取消關(guān)注事件類</p>
 */
public class SubscribeEventMessage extends BaseEventMessage{
 
}

Step 21: Find the package com.wtz.message. event, create a new class MenuEventMessage.java

package com.wtz.message.event;

/**
 * @author wangtianze QQ:864620012
 * @date 2017年4月20日 下午3:32:42
 * <p>version:1.0</p>
 * <p>description:自定義菜單事件類</p>
 */
public class MenuEventMessage extends BaseEventMessage{
 //事件Key值,與自定義菜單接口中Key值對應(yīng)
 private String EventKey;

 public String getEventKey() {
 return EventKey;
 }

 public void setEventKey(String eventKey) {
 EventKey = eventKey;
 }
}

Step 22:Find the package com.wtz.message.event, create a new class LocationEventMessage.java

package com.wtz.message.event;

/**
 * @author wangtianze QQ:864620012
 * @date 2017年4月20日 下午3:37:50
 * <p>version:1.0</p>
 * <p>description:上報(bào)地理位置事件類</p>
 */
public class LocationEventMessage extends BaseEventMessage{
 //地理位置緯度
 private String Latitude;
 //地理位置經(jīng)度
 private String Longitude;
 //地理位置精度
 private String Precision;
 
 public String getLatitude() {
 return Latitude;
 }
 public void setLatitude(String latitude) {
 Latitude = latitude;
 }
 public String getLongitude() {
 return Longitude;
 }
 public void setLongitude(String longitude) {
 Longitude = longitude;
 }
 public String getPrecision() {
 return Precision;
 }
 public void setPrecision(String precision) {
 Precision = precision;
 }
}

Step 23: Find the MessageUtil.java class under the package com.wtz.util, modify this class to add various message types and event types for requests and replies, and add various types of messages to be converted into XMLFormat method

package com.wtz.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;
import com.wtz.message.response.ImageMessage;
import com.wtz.message.response.MusicMessage;
import com.wtz.message.response.NewsMessage;
import com.wtz.message.response.TextMessage;
import com.wtz.message.response.VideoMessage;
import com.wtz.message.response.VoiceMessage;

/**
 * @author wangtianze QQ:864620012
 * @date 2017年4月19日 下午3:29:58
 * <p>version:1.0</p>
 * <p>description:消息處理工具類</p>
 */
public class MessageUtil {
 //請求消息類型:文本
 public static final String REQ_MESSAGE_TYPE_TEXT = "text";
 //請求消息類型:圖片
 public static final String REQ_MESSAGE_TYPE_IMAGE = "image";
 //請求消息類型:語音
 public static final String REQ_MESSAGE_TYPE_VOICE = "voice";
 //請求消息類型:視頻
 public static final String REQ_MESSAGE_TYPE_VIDEO = "video";
 //請求消息類型:地理位置
 public static final String REQ_MESSAGE_TYPE_LOCATION = "location";
 //請求消息類型:鏈接
 public static final String REQ_MESSAGE_TYPE_LINK = "link";
 //請求消息類型:事件推送
 public static final String REQ_MESSAGE_TYPE_EVENT = "event";
 
 //---------------------------------------------------------------
 
 //事件類型:subscribe(訂閱)
 public static final String EVENT_TYPE_SUBSCRIBE = "subscribe";
 //事件類型:unsubscribe(取消訂閱)
 public static final String EVENT_TYPE_UNSUBSCRIBE = "unsubscribe";
 //事件類型:scan(用戶已關(guān)注時的掃描帶參數(shù)的二維碼)
 public static final String EVENT_TYPE_SCAN = "scan";
 //事件類型:LOCATION(上報(bào)地理位置)
 public static final String EVENT_TYPE_LOCATION = "LOCATION";
 //事件類型:CLICK(自定義菜單)
 public static final String EVENT_TYPE_CLICK = "CLICK";
 
 //---------------------------------------------------------------
 
 //響應(yīng)消息類型:文本
 public static final String RESP_MESSAGE_TYPE_TEXT = "text";
 //響應(yīng)詳細(xì)類型:圖片
 public static final String RESP_MESSAGE_TYPE_IMAGE = "image";
 //響應(yīng)消息類型:語音
 public static final String RESP_MESSAGE_TYPE_VOICE = "voice";
 //響應(yīng)消息類型:視頻
 public static final String RESP_MESSAGE_TYPE_VIDEO = "video";
 //響應(yīng)詳細(xì)類型:音樂
 public static final String RESP_MESSAGE_TYPE_MUSIC = "music";
 //響應(yīng)消息類型:圖文
 public static final String RESP_MESSAGE_TYPE_NEWS = "news";
 
 //---------------------------------------------------------------
 
 //從流中解析出每個節(jié)點(diǎn)的內(nèi)容
 public static Map<String,String> parseXml(HttpServletRequest request) throws IOException{
 Map<String,String> map = new HashMap<String,String>();
 
 //從輸入流中獲取流對象
 InputStream in = request.getInputStream();
 
 //構(gòu)建SAX閱讀器對象
 SAXReader reader = new SAXReader();
 try {
  //從流中獲得文檔對象
  Document doc = reader.read(in);
  
  //獲得根節(jié)點(diǎn)
  Element root = doc.getRootElement();
  
  //獲取根節(jié)點(diǎn)下的所有子節(jié)點(diǎn)
  List<Element> children = root.elements();
  
  for(Element e:children){
  //遍歷每一個節(jié)點(diǎn),并按照節(jié)點(diǎn)名--節(jié)點(diǎn)值放入map中
  map.put(e.getName(), e.getText());
  System.out.println("用戶發(fā)送的消息XML解析為:" + e.getName() + e.getText());
  }
  
  //關(guān)閉流
  in.close();
  in = null;
 } catch (DocumentException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 
 return map;
 }
 
 /**
 * 用于擴(kuò)展節(jié)點(diǎn)數(shù)據(jù)按照<ToUserName><![CDATA[toUser]]></ToUserName>,中間加了CDATA段
 */
 private static XStream xstream = new XStream(new XppDriver(){
 public HierarchicalStreamWriter createWriter(Writer out){
  return new PrettyPrintWriter(out){
  boolean cdata = true;
  public void startNode(String name,Class clazz){
   super.startNode(name,clazz);
  }
  
  protected void writeText(QuickWriter writer,String text){
   if(cdata){
   writer.write("<![CDATA[");
   writer.write(text);
   writer.write("]]>");
   }else{
   writer.write(text);
   }
  }
  };
 }
 });
 
 /**
 * 將文本消息對象轉(zhuǎn)換成XML格式
 * params:textMessage 文本消息對象
 * return:xml
 */
 public static String messageToXml(TextMessage textMessage){
 xstream.alias("xml",textMessage.getClass());
 return xstream.toXML(textMessage);
 }
 
 /**
 * 將圖片消息對象轉(zhuǎn)換成XML格式
 * params:imageMessage
 * return:xml
 */
 public static String messageToXml(ImageMessage imageMessage){
 xstream.alias("xml", imageMessage.getClass());
 return xstream.toXML(imageMessage);
 }
 
 /**
 * 將語音消息對象轉(zhuǎn)換成XML格式
 * params:voiceMessage
 * return:xml
 */
 public static String messageToXml(VoiceMessage voiceMessage){
 xstream.alias("xml",voiceMessage.getClass());
 return xstream.toXML(voiceMessage);
 }
 
 /**
 * 將視頻消息對象轉(zhuǎn)換成XML格式
 * params:videoMessage
 * return:xml
 */
 public static String messageToXml(VideoMessage videoMessage){
 xstream.alias("xml",videoMessage.getClass());
 return xstream.toXML(videoMessage);
 }
 
 /**
 * 將音樂消息對象轉(zhuǎn)換成XML格式
 * params:musicMessage
 * return:xml
 */
 public static String messageToXml(MusicMessage musicMessage){
 xstream.alias("xml",musicMessage.getClass());
 return xstream.toXML(musicMessage);
 }
 
 /**
 * 將圖文消息對象轉(zhuǎn)換成XML格式
 * params:newsMessage
 * return:xml
 */
 public static String messageToXml(NewsMessage newsMessage){
 xstream.alias("xml",newsMessage.getClass());
 return xstream.toXML(newsMessage);
 }
}

Step 24:Find the ProcessService.java class under the package com.wtz.util, and modify the dealRequest method to respond to various types of messages

package com.wtz.util;

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

import javax.servlet.http.HttpServletRequest;

import com.wtz.message.response.TextMessage;

/**
 * @author wangtianze QQ:864620012
 * @date 2017年4月19日 下午8:04:14
 * <p>version:1.0</p>
 * <p>description:核心服務(wù)類</p>
 */
public class ProcessService {
 public static String dealRequest(HttpServletRequest request){
 //XML格式的消息數(shù)據(jù)
 String respXml = "";
 //默認(rèn)返回的文本消息內(nèi)容
 String respContent = "未知的消息類型";
 //調(diào)用parseXml方法解析請求消息
 Map<String, String> requestMap;
 try {
  requestMap = MessageUtil.parseXml(request);
  //發(fā)送方賬號
  String fromUserName = requestMap.get("FromUserName");
  //開發(fā)者微信號
  String toUserName = requestMap.get("ToUserName");
  //消息類型
  String MsgType = requestMap.get("MsgType");
  
  //回復(fù)文本消息
  TextMessage textMessage = new TextMessage();
  textMessage.setToUserName(fromUserName);
  textMessage.setFromUserName(toUserName);
  textMessage.setCreateTime(new Date().getTime());
  textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
  
  if(MsgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)){
  respContent = "您發(fā)送的是文本消息";
  }else if(MsgType.equals(MessageUtil.REQ_MESSAGE_TYPE_IMAGE)){
  respContent = "您發(fā)送的是圖片消息";
  }else if(MsgType.equals(MessageUtil.REQ_MESSAGE_TYPE_VOICE)){
  respContent = "您發(fā)送的是語音消息";
  }else if(MsgType.equals(MessageUtil.REQ_MESSAGE_TYPE_VIDEO)){
  respContent = "您發(fā)送的是視頻消息";
  }else if(MsgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LOCATION)){
  respContent = "您發(fā)送的是地理位置消息";
  }else if(MsgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LINK)){
  respContent = "您發(fā)送的是鏈接消息";
  }else if(MsgType.equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT)){
  //事件類型
  String eventType = requestMap.get("Event");
  if(eventType.equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)){
   respContent = "感謝您的關(guān)注";
  }else if(eventType.equals(MessageUtil.EVENT_TYPE_UNSUBSCRIBE)){
   //TODO 取消訂閱后用戶不會再收到公眾號發(fā)送的消息,因此不需要回復(fù)
  }else if(eventType.equals(MessageUtil.EVENT_TYPE_SCAN)){
   //TODO 處理掃描帶參數(shù)二維碼事件
  }else if(eventType.equals(MessageUtil.EVENT_TYPE_LOCATION)){
   //TODO 處理上報(bào)地理位置事件
  }else if(eventType.equals(MessageUtil.EVENT_TYPE_CLICK)){
   //TODO 處理菜單點(diǎn)擊事件
  }
  }
  //設(shè)置文本消息的內(nèi)容
  textMessage.setContent(respContent);
  //將文本消息對象轉(zhuǎn)換成xml
  respXml = MessageUtil.messageToXml(textMessage);
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 
 return respXml;
 }
}

Various types of messages are encapsulated.

【Related recommendations】

1. WeChat public account platform source code download

2. WeChat voting source code

The above is the detailed content of Various types of message encapsulation for WeChat secondary development. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
VSCode settings.json location VSCode settings.json location Aug 01, 2025 am 06:12 AM

The settings.json file is located in the user-level or workspace-level path and is used to customize VSCode settings. 1. User-level path: Windows is C:\Users\\AppData\Roaming\Code\User\settings.json, macOS is /Users//Library/ApplicationSupport/Code/User/settings.json, Linux is /home//.config/Code/User/settings.json; 2. Workspace-level path: .vscode/settings in the project root directory

How to handle transactions in Java with JDBC? How to handle transactions in Java with JDBC? Aug 02, 2025 pm 12:29 PM

To correctly handle JDBC transactions, you must first turn off the automatic commit mode, then perform multiple operations, and finally commit or rollback according to the results; 1. Call conn.setAutoCommit(false) to start the transaction; 2. Execute multiple SQL operations, such as INSERT and UPDATE; 3. Call conn.commit() if all operations are successful, and call conn.rollback() if an exception occurs to ensure data consistency; at the same time, try-with-resources should be used to manage resources, properly handle exceptions and close connections to avoid connection leakage; in addition, it is recommended to use connection pools and set save points to achieve partial rollback, and keep transactions as short as possible to improve performance.

JD Stablecoin Official Website Where to buy JD Stablecoin JD Stablecoin Official Website Where to buy JD Stablecoin Aug 01, 2025 pm 06:51 PM

Currently, JD.com has not issued any stablecoins, and users can choose the following platforms to purchase mainstream stablecoins: 1. Binance is the platform with the largest transaction volume in the world, supports multiple fiat currency payments, and has strong liquidity; 2. OKX has powerful functions, providing 7x24-hour customer service and multiple payment methods; 3. Huobi has high reputation in the Chinese community and has a complete risk control system; 4. Gate.io has rich currency types, suitable for exploring niche assets after purchasing stablecoins; 5. There are many types of currency listed on KuCoin, which is conducive to discovering early projects; 6. Bitget is characterized by order transactions, with convenient P2P transactions, and is suitable for social trading enthusiasts. The above platforms all provide safe and reliable stablecoin purchase services.

Mastering Dependency Injection in Java with Spring and Guice Mastering Dependency Injection in Java with Spring and Guice Aug 01, 2025 am 05:53 AM

DependencyInjection(DI)isadesignpatternwhereobjectsreceivedependenciesexternally,promotingloosecouplingandeasiertestingthroughconstructor,setter,orfieldinjection.2.SpringFrameworkusesannotationslike@Component,@Service,and@AutowiredwithJava-basedconfi

Understanding the Java Virtual Machine (JVM) Internals Understanding the Java Virtual Machine (JVM) Internals Aug 01, 2025 am 06:31 AM

TheJVMenablesJava’s"writeonce,runanywhere"capabilitybyexecutingbytecodethroughfourmaincomponents:1.TheClassLoaderSubsystemloads,links,andinitializes.classfilesusingbootstrap,extension,andapplicationclassloaders,ensuringsecureandlazyclassloa

How to work with Calendar in Java? How to work with Calendar in Java? Aug 02, 2025 am 02:38 AM

Use classes in the java.time package to replace the old Date and Calendar classes; 2. Get the current date and time through LocalDate, LocalDateTime and LocalTime; 3. Create a specific date and time using the of() method; 4. Use the plus/minus method to immutably increase and decrease the time; 5. Use ZonedDateTime and ZoneId to process the time zone; 6. Format and parse date strings through DateTimeFormatter; 7. Use Instant to be compatible with the old date types when necessary; date processing in modern Java should give priority to using java.timeAPI, which provides clear, immutable and linear

Google Chrome cannot open local files Google Chrome cannot open local files Aug 01, 2025 am 05:24 AM

ChromecanopenlocalfileslikeHTMLandPDFsbyusing"Openfile"ordraggingthemintothebrowser;ensuretheaddressstartswithfile:///;2.SecurityrestrictionsblockAJAX,localStorage,andcross-folderaccessonfile://;usealocalserverlikepython-mhttp.server8000tor

Understanding Network Ports and Firewalls Understanding Network Ports and Firewalls Aug 01, 2025 am 06:40 AM

Networkportsandfirewallsworktogethertoenablecommunicationwhileensuringsecurity.1.Networkportsarevirtualendpointsnumbered0–65535,withwell-knownportslike80(HTTP),443(HTTPS),22(SSH),and25(SMTP)identifyingspecificservices.2.PortsoperateoverTCP(reliable,c

See all articles