激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java微信公眾平臺之消息管理

Java微信公眾平臺之消息管理

2021-04-27 11:26Phil_Jing Java教程

這篇文章主要為大家詳細介紹了Java微信公眾平臺之消息管理的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

java微信公眾平臺開發之消息管理,一定要先看下官方文檔

微信消息管理分為接收普通消息、接收事件推送、發送消息(被動回復)、客服消息、群發消息、模板消息這幾部分

一、接收普通消息

當普通微信用戶向公眾賬號發消息時,微信服務器將post消息的xml數據包到開發者填寫的url上。

關于msgid,官方給出解釋,相當于每個消息id,關于重試的消息排重,推薦使用msgid排重。微信服務器在五秒內收不到響應會斷掉連接,并且重新發起請求,總共重試三次。

比如文本消息的xml示例

?
1
2
3
4
5
6
7
8
<xml>
 <tousername><![cdata[touser]]></tousername>
 <fromusername><![cdata[fromuser]]></fromusername>
 <createtime>1348831860</createtime>
 <msgtype><![cdata[text]]></msgtype>
 <content><![cdata[this is a test]]></content>
 <msgid>1234567890123456</msgid>
 </xml>

其他的消息去官方文檔查看,簡單封裝如下
消息抽象基類abstractmsg.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.phil.wechat.msg.model.req;
 
import java.io.serializable;
 
/**
 * 基礎消息類
 *
 * @author phil
 *
 */
public abstract class abstractmsg implements serializable {
  
 private static final long serialversionuid = -6244277633057415731l;
 private string tousername; // 開發者微信號
 private string fromusername; // 發送方帳號(一個openid)
 private string msgtype = setmsgtype(); // 消息類型 例如 /text/image
 private long createtime; // 消息創建時間 (整型)
 private long msgid; // 消息id,64位整型
 /**
  * 消息類型
  *
  * @return
  */
 public abstract string setmsgtype();
}

文本消息textmsg.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.phil.wechat.msg.model.req;
 
/**
 * 文本消息
 * @author phil
 * @date 2017年6月30日
 *
 */
public class textmsg extends abstractmsg {
 
 private static final long serialversionuid = -1764016801417503409l;
 private string content; // 文本消息
 @override
 public string setmsgtype() {
  return "text";
 }
}

其他的依樣畫葫蘆......

二、被動回復用戶消息

微信服務器在將用戶的消息發給公眾號的開發者服務器地址(開發者中心處配置)后,微信服務器在五秒內收不到響應會斷掉連接,并且重新發起請求,總共重試三次,如果在調試中,發現用戶無法收到響應的消息,可以檢查是否消息處理超時。假如服務器無法保證在五秒內處理并回復,可以直接回復空串,微信服務器不會對此作任何處理,并且不會發起重試。

如果出現“該公眾號暫時無法提供服務,請稍后再試”,原因有兩個

  • 開發者在5秒內未回復任何內容
  • 開發者回復了異常數據

比如回復的文本消息xml示例

?
1
2
3
4
5
6
7
<xml>
<tousername><![cdata[touser]]></tousername>
<fromusername><![cdata[fromuser]]></fromusername>
<createtime>12345678</createtime>
<msgtype><![cdata[text]]></msgtype>
<content><![cdata[你好]]></content>
</xml>

簡單封裝下

回復消息抽象基類respabstractmsg.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.phil.wechat.msg.model.resp;
 
import java.io.serializable;
 
/**
 * 消息基類(公眾帳號 -> 普通用戶)
 *
 * @author phil
 *
 */
public abstract class respabstractmsg{
 // 接收方帳號(收到的openid)
 private string tousername;
 // 開發者微信號
 private string fromusername;
 // 消息創建時間 (整型)
 private long createtime;
 // 消息類型(text/music/news)
 private string msgtype = setmsgtype(); // 消息類型
 public abstract string setmsgtype();
}

回復文本消息resptextmsg.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.phil.wechat.msg.model.resp;
 
/**
 * 回復圖片消息
 *
 * @author phil
 * @data 2017年3月26日
 *
 */
public class respimagemsg extends respabstractmsg {
 private image image;
 @override
 public string setmsgtype() {
  return "image";
 }
 
 /**
  *
  * @author phil
  * @date 2017年7月19日
  *
  */
 public class image {
 
  // 通過素材管理中的接口上傳多媒體文件,得到的id。
  private string mediaid;
 
  public string getmediaid() {
   return mediaid;
  }
 
  public void setmediaid(string mediaid) {
   mediaid = mediaid;
  }
 }
}

其他消息類型依樣畫葫蘆......

三、消息的處理

掌握xml解析

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package com.phil.wechat.msg.controller;
 
import java.io.ioexception;
import java.io.inputstream;
import java.util.map;
import java.util.objects;
 
import org.apache.commons.lang3.stringutils;
import org.dom4j.documentexception;
import org.slf4j.logger;
import org.slf4j.loggerfactory;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;
 
import com.phil.modules.config.wechatconfig;
import com.phil.modules.util.msgutil;
import com.phil.modules.util.signatureutil;
import com.phil.modules.util.xmlutil;
import com.phil.wechat.base.controller.basecontroller;
import com.phil.wechat.base.result.wechatresult;
import com.phil.wechat.msg.model.req.basicmsg;
import com.phil.wechat.msg.model.resp.respabstractmsg;
import com.phil.wechat.msg.model.resp.respnewsmsg;
import com.phil.wechat.msg.service.wechatmsgservice;
 
/**
 * @author phil
 * @date 2017年9月19日
 *
 */
@controller
@requestmapping("/wechat")
public class wechatmsgcontroller extends basecontroller {
  
 private logger logger = loggerfactory.getlogger(this.getclass());
  
 @autowired
 private wechatmsgservice wechatmsgservice;
  
 /**
  * 校驗信息是否是從微信服務器發出,處理消息
  * @param out
  * @throws ioexception
  */
 @requestmapping(value = "/handler", method = { requestmethod.get, requestmethod.post })
 public void processpost() throws exception {
  this.getrequest().setcharacterencoding("utf-8");
  this.getresponse().setcharacterencoding("utf-8");
  boolean ispost = objects.equals("post", this.getrequest().getmethod().touppercase());
  if (ispost) {
   logger.debug("接入成功,正在處理邏輯");
   string respxml = defaultmsgdispose(this.getrequest().getinputstream());//processrequest(request, response);
   if (stringutils.isnotblank(respxml)) {
    this.getresponse().getwriter().write(respxml);
   }
  } else {
   string signature = this.getrequest().getparameter("signature");
   // 時間戳
   string timestamp = this.getrequest().getparameter("timestamp");
   // 隨機數
   string nonce = this.getrequest().getparameter("nonce");
   // 通過檢驗signature對請求進行校驗,若校驗成功則原樣返回echostr,表示接入成功,否則接入失敗
   if (signatureutil.checksignature(signature, timestamp, nonce)) {
    // 隨機字符串
    string echostr = this.getrequest().getparameter("echostr");
    logger.debug("接入成功,echostr {}", echostr);
    this.getresponse().getwriter().write(echostr);
   }
  }
 }
 
 /**
  * 默認處理方法
  * @param input
  * @return
  * @throws exception
  * @throws documentexception
  */
 private string defaultmsgdispose(inputstream inputstream) throws exception {
  string result = null;
  if (inputstream != null) {
   map<string, string> params = xmlutil.parsestreamtomap(inputstream);
   if (params != null && params.size() > 0) {
    basicmsg msginfo = new basicmsg();
    string createtime = params.get("createtime");
    string msgid = params.get("msgid");
    msginfo.setcreatetime((createtime != null && !"".equals(createtime)) ? integer.parseint(createtime) : 0);
    msginfo.setfromusername(params.get("fromusername"));
    msginfo.setmsgid((msgid != null && !"".equals(msgid)) ? long.parselong(msgid) : 0);
    msginfo.settousername(params.get("tousername"));
    wechatresult resultobj = corehandler(msginfo, params);
    if(resultobj == null){ //
     return null;
    }
    boolean success = resultobj.issuccess(); //如果 為true,則表示返回xml文件, 直接轉換即可,否則按類型
    if (success) {
     result = resultobj.getobject().tostring();
    } else {
     int type = resultobj.gettype(); // 這里規定 1 圖文消息 否則直接轉換
     if (type == wechatresult.newsmsg) {
      respnewsmsg newsmsg = (respnewsmsg) resultobj.getobject();
      result = msgutil.newsmsgtoxml(newsmsg);
     } else {
      respabstractmsg basicmsg = (respabstractmsg) resultobj.getobject();
      result = msgutil.msgtoxml(basicmsg);
     }
    }
   } else {
    result = "msg is wrong";
   }
  }
  return result;
 }
  
 /**
  * 核心處理
  *
  * @param msg
  *   消息基類
  * @param params
  *   xml 解析出來的 數據
  * @return
  */
 private wechatresult corehandler(basicmsg msg, map<string, string> params) {
  wechatresult result = null;
  string msgtype = params.get("msgtype");
  if (stringutils.isempty(msgtype)) {
   switch (msgtype) {
   case wechatconfig.req_message_type_text: // 文本消息
    result = wechatmsgservice.textmsg(msg, params);
    break;
   case wechatconfig.req_message_type_image: // 圖片消息
    result = wechatmsgservice.imagemsg(msg, params);
    break;
   case wechatconfig.req_message_type_link: // 鏈接消息
    result = wechatmsgservice.linkmsg(msg, params);
    break;
   case wechatconfig.req_message_type_location: // 地理位置
    result = wechatmsgservice.locationmsg(msg, params);
    break;
   case wechatconfig.req_message_type_voice: // 音頻消息
    result = wechatmsgservice.voicemsg(msg, params);
    break;
   case wechatconfig.req_message_type_shortvideo: // 短視頻消息
    result = wechatmsgservice.shortvideo(msg, params);
    break;
   case wechatconfig.req_message_type_video: // 視頻消息
    result = wechatmsgservice.videomsg(msg, params);
    break;
   case wechatconfig.req_message_type_event: // 事件消息
    string eventtype = params.get("event"); //
    if (eventtype != null && !"".equals(eventtype)) {
     switch (eventtype) {
     case wechatconfig.event_type_subscribe:
      result = wechatmsgservice.subscribe(msg, params);
      break;
     case wechatconfig.event_type_unsubscribe:
      result = wechatmsgservice.unsubscribe(msg, params);
      break;
     case wechatconfig.event_type_scan:
      result = wechatmsgservice.scan(msg, params);
      break;
     case wechatconfig.event_type_location:
      result = wechatmsgservice.eventlocation(msg, params);
      break;
     case wechatconfig.event_type_click:
      result = wechatmsgservice.eventclick(msg, params);
      break;
     case wechatconfig.event_type_view:
      result = wechatmsgservice.eventview(msg, params);
      break;
     case wechatconfig.kf_create_session:
      result = wechatmsgservice.kfcreatesession(msg, params);
      break;
     case wechatconfig.kf_close_session:
      result = wechatmsgservice.kfclosesession(msg, params);
      break;
     case wechatconfig.kf_switch_session:
      result = wechatmsgservice.kfswitchsession(msg, params);
      break;
     default:
      wechatmsgservice.eventdefaultreply(msg, params);
      break;
     }
    }
    break;
   default:
    wechatmsgservice.defaultmsg(msg, params);
   }
  }
  return result;
 }
}

只是提供個思路,如若參考代碼請移步

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

原文鏈接:https://blog.csdn.net/phil_jing/article/details/76358261

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 91小视频在线观看免费版高清 | 中文字幕网址 | 中国洗澡偷拍在线播放 | 亚洲国产精品久久久久制服红楼梦 | 99国产精品欲a | 国产一区二区免费在线观看 | 国产草草视频 | 中文字幕欧美专区 | 中文字幕在线视频日本 | 国产高清自拍一区 | 亚洲福利在线视频 | 91精品欧美一区二区三区 | 成人免费毛片片v | 中文字幕在线观看视频一区 | 毛片区 | 欧美成人影院 | 国产九九在线视频 | 九九热精品视频在线 | 国产合集91合集久久日 | 日韩美香港a一级毛片 | 亚洲国产精品二区 | 久久人人爽人人爽人人片av高清 | 不卡中文一二三区 | 成人性生活视频在线观看 | 桥本有菜免费av一区二区三区 | 欧美毛片免费观看 | 国产女厕一区二区三区在线视 | 1000部精品久久久久久久久 | 久久久久久久免费看 | 国产亚洲精品久久久久久久软件 | 日本一区二区在线 | 免费观看一级黄色片 | 国产精品久久久久久久久久 | 久久久久久久久久久久久久国产 | 韩国草草影院 | 一区二区精品视频在线观看 | 妇女毛片 | 日本不卡一区二区三区在线 | 国产精品久久久久久久久久久天堂 | 国产精品一二区 | 爽成人777777婷婷 |