国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java微信公眾平臺開發(3) 接收消息的分類及實體的創建

2019-11-26 12:32:37
字體:
來源:轉載
供稿:網友

前面一篇有說道應用服務器和騰訊服務器是通過消息進行通訊的,并簡單介紹了微信端post的消息類型,這里我們將建立消息實體以方便我們后面的使用!

(一)消息實體基礎類

package com.cuiyongzhi.wechat.message.req; /** * ClassName: BaseMessage * @Description: 微信請求消息基本類 * @author dapengniao * @date 2016年3月7日 下午3:03:59 */public class BaseMessage { // 開發者微信號 private String ToUserName; // 發送方帳號(一個OpenID) private String FromUserName; // 消息創建時間 (整型) private long CreateTime; // 消息類型(text/image/location/link/video/shortvideo) 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; }}

(二)普通消息pojo實體

①圖片消息

package com.cuiyongzhi.wechat.message.req; /** * ClassName: ImageMessage * @Description: 圖片消息 * @author dapengniao * @date 2016年3月7日 下午3:04:52 */public class ImageMessage extends BaseMessage { // 圖片鏈接 private String PicUrl;  public String getPicUrl() {  return PicUrl; }  public void setPicUrl(String picUrl) {  PicUrl = picUrl; }}

②連接消息

package com.cuiyongzhi.wechat.message.req; /** * ClassName: LinkMessage * @Description: 連接消息 * @author dapengniao * @date 2016年3月7日 下午3:05:48 */public class LinkMessage extends BaseMessage { // 消息標題 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; }}

③地理位置消息

package com.cuiyongzhi.wechat.message.req; /** * ClassName: LocationMessage * @Description: 地理位置消息 * @author dapengniao * @date 2016年3月7日 下午3:06:10 */public class LocationMessage extends BaseMessage {  // 地理位置維度  private String Location_X;  // 地理位置經度  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;  } }

④文本消息

package com.cuiyongzhi.wechat.message.req; /** * ClassName: TextMessage * @Description: 文本消息 * @author dapengniao * @date 2016年3月7日 下午3:06:40 */public class TextMessage extends BaseMessage {  // 消息內容  private String Content;   public String getContent() {   return Content;  }   public void setContent(String content) {   Content = content;  } }

⑤視頻/小視屏消息

package com.cuiyongzhi.wechat.message.req; /** * ClassName: VideoMessage * @Description: 視頻/小視屏消息 * @author dapengniao * @date 2016年3月7日 下午3:12:51 */public class VideoMessage extends BaseMessage {  private String MediaId; // 視頻消息媒體id,可以調用多媒體文件下載接口拉取數據 private String ThumbMediaId; // 視頻消息縮略圖的媒體id,可以調用多媒體文件下載接口拉取數據  public String getMediaId() {  return MediaId; }  public void setMediaId(String mediaId) {  MediaId = mediaId; }  public String getThumbMediaId() {  return ThumbMediaId; }  public void setThumbMediaId(String thumbMediaId) {  ThumbMediaId = thumbMediaId; } }

⑥語音消息

package com.cuiyongzhi.wechat.message.req; /** * ClassName: VoiceMessage * @Description: 語音消息 * @author dapengniao * @date 2016年3月7日 下午3:07:10 */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;  } }

(三)消息分類處理

按照上面收到想消息類別分別做不同的分發處理,這里我們建立了自己的業務分發器(EventDispatcher、MsgDispatcher),分別做普通消息處理和事件消息處理!

①MsgDispatcher.java――用于普通消息的業務分發處理

package com.cuiyongzhi.wechat.dispatcher; import java.util.Map; import com.cuiyongzhi.wechat.util.MessageUtil; /** * ClassName: MsgDispatcher * @Description: 消息業務處理分發器 * @author dapengniao * @date 2016年3月7日 下午4:04:21 */public class MsgDispatcher { public static String processMessage(Map<String, String> map) {  if (map.get("MsgType").equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) { // 文本消息   System.out.println("==============這是文本消息!");  }     if (map.get("MsgType").equals(MessageUtil.REQ_MESSAGE_TYPE_IMAGE)) { // 圖片消息   System.out.println("==============這是圖片消息!");  }     if (map.get("MsgType").equals(MessageUtil.REQ_MESSAGE_TYPE_LINK)) { // 鏈接消息   System.out.println("==============這是鏈接消息!");  }     if (map.get("MsgType").equals(MessageUtil.REQ_MESSAGE_TYPE_LOCATION)) { // 位置消息   System.out.println("==============這是位置消息!");  }     if (map.get("MsgType").equals(MessageUtil.REQ_MESSAGE_TYPE_VIDEO)) { // 視頻消息   System.out.println("==============這是視頻消息!");  }       if (map.get("MsgType").equals(MessageUtil.REQ_MESSAGE_TYPE_VOICE)) { // 語音消息   System.out.println("==============這是語音消息!");  }   return null; }}

②EventDispatcher.java――事件消息的業務分發處理

package com.cuiyongzhi.wechat.dispatcher; import java.util.Map; import com.cuiyongzhi.wechat.util.MessageUtil; /** * ClassName: EventDispatcher * @Description: 事件消息業務分發器 * @author dapengniao * @date 2016年3月7日 下午4:04:41 */public class EventDispatcher { public static String processEvent(Map<String, String> map) {  if (map.get("Event").equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)) { //關注事件   System.out.println("==============這是關注事件!");  }     if (map.get("Event").equals(MessageUtil.EVENT_TYPE_UNSUBSCRIBE)) { //取消關注事件   System.out.println("==============這是取消關注事件!");  }     if (map.get("Event").equals(MessageUtil.EVENT_TYPE_SCAN)) { //掃描二維碼事件   System.out.println("==============這是掃描二維碼事件!");  }     if (map.get("Event").equals(MessageUtil.EVENT_TYPE_LOCATION)) { //位置上報事件   System.out.println("==============這是位置上報事件!");  }     if (map.get("Event").equals(MessageUtil.EVENT_TYPE_CLICK)) { //自定義菜單點擊事件   System.out.println("==============這是自定義菜單點擊事件!");  }     if (map.get("Event").equals(MessageUtil.EVENT_TYPE_VIEW)) { //自定義菜單View事件   System.out.println("==============這是自定義菜單View事件!");  }        return null; }}

這個時候我們需要把我們的消息入口【WechatSecurity.java】中的post方法做些修改,最終結果如下:

package com.cuiyongzhi.wechat.controller; import java.io.PrintWriter;import java.util.Map; import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam; import com.cuiyongzhi.wechat.dispatcher.EventDispatcher;import com.cuiyongzhi.wechat.dispatcher.MsgDispatcher;import com.cuiyongzhi.wechat.util.MessageUtil;import com.cuiyongzhi.wechat.util.SignUtil; @Controller@RequestMapping("/wechat")public class WechatSecurity { private static Logger logger = Logger.getLogger(WechatSecurity.class);  /**  *   * @Description: 用于接收get參數,返回驗證參數  * @param @param request  * @param @param response  * @param @param signature  * @param @param timestamp  * @param @param nonce  * @param @param echostr  * @author dapengniao  * @date 2016年3月4日 下午6:20:00  */ @RequestMapping(value = "security", method = RequestMethod.GET) public void doGet(   HttpServletRequest request,   HttpServletResponse response,   @RequestParam(value = "signature", required = true) String signature,   @RequestParam(value = "timestamp", required = true) String timestamp,   @RequestParam(value = "nonce", required = true) String nonce,   @RequestParam(value = "echostr", required = true) String echostr) {  try {   if (SignUtil.checkSignature(signature, timestamp, nonce)) {    PrintWriter out = response.getWriter();    out.print(echostr);    out.close();   } else {    logger.info("這里存在非法請求!");   }  } catch (Exception e) {   logger.error(e, e);  } }  /**  * @Description: 接收微信端消息處理并做分發  * @param @param request  * @param @param response   * @author dapengniao  * @date 2016年3月7日 下午4:06:47  */ @RequestMapping(value = "security", method = RequestMethod.POST) public void DoPost(HttpServletRequest request,HttpServletResponse response) {  try{   Map<String, String> map=MessageUtil.parseXml(request);   String msgtype=map.get("MsgType");   if(MessageUtil.REQ_MESSAGE_TYPE_EVENT.equals(msgtype)){    EventDispatcher.processEvent(map); //進入事件處理   }else{    MsgDispatcher.processMessage(map); //進入消息處理   }  }catch(Exception e){   logger.error(e,e);  } }}

最后我們運行成功項目之后我們可以通過發送不同類型的消息來驗證我們的消息分類的正確性,如下圖所示:

新建了這么多文件,最后來看下我們的整個項目的目錄結構:

前面講述的都是消息的接收,那么下一篇起將講述【回復消息的分類及實體的創建】,感謝你的翻閱,如有疑問可以留言一起討論!

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 瑞丽市| 文山县| 仁化县| 庆云县| 固阳县| 黎川县| 汪清县| 固原市| 鹰潭市| 凤翔县| 乌审旗| 西青区| 呈贡县| 板桥市| 民乐县| 丘北县| 玉山县| 定州市| 济源市| 沧州市| 汝城县| 阿克苏市| 达孜县| 石林| 和硕县| 咸阳市| 西城区| 阳高县| 新源县| 贵定县| 丽江市| 镇平县| 夏邑县| 保德县| 兴海县| 东阳市| 兴国县| 莱州市| 屯昌县| 蕉岭县| 卢氏县|