国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看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微信公眾平臺開發(fā)之消息管理,一定要先看下官方文檔

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

一、接收普通消息

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

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

比如文本消息的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; // 開發(fā)者微信號
 private string fromusername; // 發(fā)送方帳號(一個openid)
 private string msgtype = setmsgtype(); // 消息類型 例如 /text/image
 private long createtime; // 消息創(chuàng)建時間 (整型)
 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";
 }
}

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

二、被動回復用戶消息

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

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

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

比如回復的文本消息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;
 // 開發(fā)者微信號
 private string fromusername;
 // 消息創(chuàng)建時間 (整型)
 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;
  
 /**
  * 校驗信息是否是從微信服務器發(fā)出,處理消息
  * @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(); // 這里規(guī)定 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

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: h视频免费看 | 91精品蜜臀在线一区尤物 | 国产色视频在线播放 | 亚洲欧美在线免费 | 福利社午夜影院 | 99re6在线视频精品免费 | 99精品欧美一区二区三区 | 91精品国产91久久久久久 | 精品一区二区久久 | 天天干夜操 | 激情伊人 | 日日做 | 国产精品久久一区 | 成人在线二区 | 在线国产一区二区 | 亚洲精品三级 | 国产精品久久久久久久久久免费 | 免费成人在线观看视频 | 欧美视频精品 | 在线a电影 | 日韩欧美一区二区在线观看 | 日韩精品在线视频观看 | 欧美精品久久久 | 欧美国产高清 | www.久草.com | 国产一区av在线 | 亚洲欧美日韩电影 | 日韩一本| 日韩一区二区影视 | 天天爽夜夜爽夜夜爽精品视频 | 91av亚洲| www.久| 日韩二区 | 日韩免费在线观看视频 | 日韩欧美一区二区三 | 国产一区二区视频在线 | 日本黄色一区 | 久久久美女 | 成人1区2区 | 欧美日韩中文 | 国产精品美女久久久久aⅴ国产馆 |