服务器之家

服务器之家 > 正文

asp.net微信开发(高级群发文本)

时间:2019-12-28 13:49     来源/作者:将哥

首先我们先来讲解一下群发文本信息的过程,我个人开发程序是首先要有UI才能下手去写代码,界面如下,

 asp.net微信开发(高级群发文本)

asp.net微信开发(高级群发文本)

看图我们也可以看出首先我们要获取该微信号本月还能群发几条信息,关于怎么计算,就是群发成功一条信息,就在本地数据库存储一条信息,用来计算条数,(这个我相信都会),大于4条就不能发送(这里我已经限制死了,因为服务号每月只能发送4条,多发送也没用,用户只能收到4条,除非使用预览功能,挨个发送,但预览功能也只能发送100次,或许可能使用开发者模式下群发信息可以多发送N次哦,因为我群发了两次之后,再进入到微信公众平台官网后台看到的居然还能群发4条,有点郁闷哦!),群发对象可选择为全部用户或分组用户,和由于节省群发次数,这里我就不测试群发文字信息了,具体参考如下代码:

绑定本月剩余群发条数

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/// <summary>
/// 绑定本月剩余群发条数
/// </summary>
private void BindMassCount()
{
WxMassService wms = new WxMassService();
List<WxMassInfo> wxmaslist = wms.GetMonthMassCount();
//官方微信服务号每月只能群发4条信息,(订阅号每天1条)多余信息,将不会成功推送,这里已经设定为4
this.lbMassCounts.Text = (4 - int.Parse(wxmaslist.Count.ToString())).ToString();
 
if (wxmaslist.Count >= 4)
{
this.LinkBtnSubSend.Enabled = false;
this.LinkBtnSubSend.Attributes.Add("Onclick", "return confirm('群发信息已达上限!请下月初再试!')");
}
else
{
this.LinkBtnSubSend.Enabled = true;
this.LinkBtnSubSend.Attributes.Add("Onclick", "return confirm('您确定要群发此条信息??')");
}
}

绑定分组列表

?
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
/// <summary>
/// 绑定分组列表
/// </summary>
private void BindGroupList()
{
WeiXinServer wxs = new WeiXinServer();
 
///从缓存读取accesstoken
string Access_token = Cache["Access_token"] as string;
 
if (Access_token == null)
{
//如果为空,重新获取
Access_token = wxs.GetAccessToken();
 
//设置缓存的数据7000秒后过期
Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
}
 
string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);
 
string jsonres = "";
 
string content = Cache["AllGroups_content"] as string;
 
if (content == null)
{
jsonres = "https://api.weixin.qq.com/cgi-bin/groups/get?access_token=" + Access_tokento;
 
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(jsonres);
myRequest.Method = "GET";
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
content = reader.ReadToEnd();
reader.Close();
 
//设置缓存的数据7000秒后过期
Cache.Insert("AllGroups_content", content, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
}
 
//使用前需要引用Newtonsoft.json.dll文件
JObject jsonObj = JObject.Parse(content);
 
 
int groupsnum = jsonObj["groups"].Count();
 
this.DDLGroupList.Items.Clear();//清除
 
for (int i = 0; i < groupsnum; i++)
{
this.DDLGroupList.Items.Add(new ListItem(jsonObj["groups"][i]["name"].ToString() + "(" + jsonObj["groups"][i]["count"].ToString() + ")", jsonObj["groups"][i]["id"].ToString()));
}
}
/// <summary>
/// 选择群发对象类型,显示隐藏分组列表项
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void DDLMassType_SelectedIndexChanged(object sender, EventArgs e)
{
if (int.Parse(this.DDLMassType.SelectedValue.ToString()) > 0)
{
this.DDLGroupList.Visible = true;
}
else
{
 this.DDLGroupList.Visible = false;
}
}

群发

 

?
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/// <summary>
/// 群发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void LinkBtnSubSend_Click(object sender, EventArgs e)
{
//根据单选按钮判断类型,发送
///如果选择的是文本消息
if (this.RadioBtnList.SelectedValue.ToString().Equals("0"))
{
if (String.IsNullOrWhiteSpace(this.txtwenben.InnerText.ToString().Trim()))
{
 ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('请输入您要群发文本内容!');", true);
 return;
}
if (this.txtwenben.InnerText.ToString().Trim().Length<10)
{
 ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('文本内容至少需要10个字符以上!');", true);
 return;
}
 
WxMassService wms = new WxMassService();
List<WxMassInfo> wxmaslist = wms.GetMonthMassCount();
 
if (wxmaslist.Count >= 4)
{
 ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('本月可群发消息数量已达上限!');", true);
 return;
}
else
{
 
 
 //如何群发类型为全部用户,根据openID列表群发给全部用户,订阅号不可用,服务号认证后可用
 if (this.DDLMassType.SelectedValue.ToString().Equals("0"))
 {
 StringBuilder sbs = new StringBuilder();
 sbs.Append(GetAllUserOpenIDList());
 
 WeiXinServer wxs = new WeiXinServer();
 
 ///从缓存读取accesstoken
 string Access_token = Cache["Access_token"] as string;
 
 if (Access_token == null)
 {
 //如果为空,重新获取
 Access_token = wxs.GetAccessToken();
 
 //设置缓存的数据7000秒后过期
 Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
 }
 
 string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);
 
 
 string posturl = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=" + Access_tokento;
 
 ///群发POST数据示例如下:
 //  {
 // "touser":[
 // "OPENID1",
 // "OPENID2"
 // ],
 // "msgtype": "text",
 // "text": { "content": "hello from boxer."}
 //}
 
 string postData = "{\"touser\":[" + sbs.ToString() +
 "],\"msgtype\":\"text\",\"text\":{\"content\":\"" + this.txtwenben.InnerText.ToString() +
 "\"}";
 
 
 string tuwenres = wxs.GetPage(posturl, postData);
 
 //使用前需药引用Newtonsoft.json.dll文件
 JObject jsonObj = JObject.Parse(tuwenres);
 
 if (jsonObj["errcode"].ToString().Equals("0"))
 {
  //群发成功后,保存记录
 WxMassInfo wmi = new WxMassInfo();
 
 wmi.ImageUrl = "";
 wmi.type = "文本";
 wmi.contents = this.txtwenben.InnerText.ToString().Trim();
 wmi.title = this.txtwenben.InnerText.ToString().Substring(0, 10) + "...";
 
 if (this.DDLMassType.SelectedValue.ToString().Equals("0"))
 {
 wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();
 }
 else
 {
 wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();
 }
 
 wmi.massStatus = "成功";//群发成功之后返回的状态码
 wmi.massMessageID = jsonObj["msg_id"].ToString();//群发成功之后返回的消息ID
 
 
 wmi.massDate = System.DateTime.Now.ToString();
 
 int num = wms.AddWxMassInfo(wmi);
 
 if (num > 0)
 {
 Session["wmninfo"] = null;
 ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!数据已保存!');location='WxMassManage.aspx';", true);
 return;
 }
 else
 {
 ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!数据保存失败!');", true);
 return;
 }
 }
 else
 {
 ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务提交失败!!');", true);
 return;
 }
 }
 else
 {
 string group_id = this.DDLGroupList.SelectedValue.ToString();
 
 
 WeiXinServer wxs = new WeiXinServer();
 
 ///从缓存读取accesstoken
 string Access_token = Cache["Access_token"] as string;
 
 if (Access_token == null)
 {
 //如果为空,重新获取
 Access_token = wxs.GetAccessToken();
 
 //设置缓存的数据7000秒后过期
 Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
 }
 
 string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);
 
 
 string posturl = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=" + Access_tokento;
 
 ///群发POST数据示例如下:
 // {
 // "filter":{
 // "is_to_all":false
 // "group_id":"2"
 // },
 // "text":{
 // "content":"CONTENT"
 // },
 // "msgtype":"text"
 //}
 //}
 
 string postData = "{\"filter\":{\"is_to_all\":\"false\"\"group_id\":\"" + group_id +
 "\"},\"text\":{\"content\":\"" + this.txtwenben.InnerText.ToString() +
 "\"},\"msgtype\":\"text\"}";
 
 
 string tuwenres = wxs.GetPage(posturl, postData);
 
 //使用前需药引用Newtonsoft.json.dll文件
 JObject jsonObj = JObject.Parse(tuwenres);
 
 if (jsonObj["errcode"].ToString().Equals("0"))
 {
 //群发成功后,保存记录
 WxMassInfo wmi = new WxMassInfo();
 
 wmi.ImageUrl = "";
 wmi.type = "文本";
 wmi.contents = this.txtwenben.InnerText.ToString().Trim();
 wmi.title = this.txtwenben.InnerText.ToString().Substring(0, 10) + "...";
 
 if (this.DDLMassType.SelectedValue.ToString().Equals("0"))
 {
 wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();
 }
 else
 {
 wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();
 }
 
 wmi.massStatus = "成功";//群发成功之后返回的状态码
 wmi.massMessageID = jsonObj["msg_id"].ToString();//群发成功之后返回的消息ID
 
 
 wmi.massDate = System.DateTime.Now.ToString();
 
 int num = wms.AddWxMassInfo(wmi);
 
 if (num > 0)
 {
 Session["wmninfo"] = null;
 ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!数据已保存!');location='WxMassManage.aspx';", true);
 return;
 }
 else
 {
 ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!数据保存失败!');", true);
 return;
 }
 }
 else
 {
 ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务提交失败!!');", true);
 return;
 }
 }
 
 
}
}
//如果选择的是图文消息
if (this.RadioBtnList.SelectedValue.ToString().Equals("1"))
{
if (String.IsNullOrWhiteSpace(this.lbtuwenmedai_id.Text.ToString().Trim()))
{
 ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('请选择或新建图文素材再进行群发!');", true);
 return;
}
 
WxMassService wms = new WxMassService();
 
List<WxMassInfo> wxmaslist = wms.GetMonthMassCount();
 
if (wxmaslist.Count >= 4)
{
 ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('本月可群发消息数量已达上限!');", true);
 return;
}
else
{
 
 //如何群发类型为全部用户,根据openID列表群发给全部用户,订阅号不可用,服务号认证后可用
 if (this.DDLMassType.SelectedValue.ToString().Equals("0"))
 {
 StringBuilder sbs = new StringBuilder();
 sbs.Append(GetAllUserOpenIDList());
 
 WeiXinServer wxs = new WeiXinServer();
 
 ///从缓存读取accesstoken
 string Access_token = Cache["Access_token"] as string;
 
 if (Access_token == null)
 {
 //如果为空,重新获取
 Access_token = wxs.GetAccessToken();
 
 //设置缓存的数据7000秒后过期
 Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
 }
 
 string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);
 
 
 string posturl = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=" + Access_tokento;
 
 ///群发POST数据示例如下:
 // {
 // "touser":[
 // "OPENID1",
 // "OPENID2"
 // ],
 // "mpnews":{
 // "media_id":"123dsdajkasd231jhksad"
 // },
 // "msgtype":"mpnews"
 //}
 
 string postData = "{\"touser\":[" + sbs.ToString() +
 "],\"mpnews\":{\"media_id\":\"" + this.lbtuwenmedai_id.Text.ToString() +
 "\"},\"msgtype\":\"mpnews\"}";
 
 
 string tuwenres = wxs.GetPage(posturl, postData);
 
 //使用前需药引用Newtonsoft.json.dll文件
 JObject jsonObj = JObject.Parse(tuwenres);
 
 if (jsonObj["errcode"].ToString().Equals("0"))
 {
 Session["media_id"] = null;
 WxMassInfo wmi = new WxMassInfo();
 if (Session["wmninfo"] != null)
 {
 WxMpNewsInfo wmninfo = Session["wmninfo"] as WxMpNewsInfo;
 
 wmi.title = wmninfo.title.ToString();
 wmi.contents = wmninfo.contents.ToString();
 wmi.ImageUrl = wmninfo.ImageUrl.ToString();
 
 
 wmi.type = "图文";
 
 if (this.DDLMassType.SelectedValue.ToString().Equals("0"))
 {
  wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();
 }
 else
 {
  wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();
 }
 
 wmi.massStatus = "成功";//群发成功之后返回的状态码
 wmi.massMessageID = jsonObj["msg_id"].ToString();//群发成功之后返回的消息ID
 
 wmi.massDate = System.DateTime.Now.ToString();
 
 int num = wms.AddWxMassInfo(wmi);
 
 if (num > 0)
 {
  Session["wmninfo"] = null;
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!数据已保存!');location='WxMassManage.aspx';", true);
  return;
 }
 else
 {
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!数据保存失败!');", true);
  return;
 }
 }
 else
 {
 wmi.title = "";
 wmi.contents = "";
 wmi.ImageUrl = "";
 wmi.type = "图文";
 
 if (this.DDLMassType.SelectedValue.ToString().Equals("0"))
 {
  wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();
 }
 else
 {
  wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();
 }
 
 wmi.massStatus = "成功";//群发成功之后返回的状态码
 wmi.massMessageID = jsonObj["msg_id"].ToString();//群发成功之后返回的消息ID
 
 wmi.massDate = System.DateTime.Now.ToString();
 
 int num = wms.AddWxMassInfo(wmi);
 
 if (num > 0)
 {
  Session["wmninfo"] = null;
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!图文部分数据已保存!');location='WxMassManage.aspx';", true);
  return;
 }
 else
 {
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!数据保存失败!');", true);
  return;
 }
 }
 }
 else
 {
 ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务提交失败!!');", true);
 return;
 }
 
 
 }
 else
 {
 //根据分组进行群发,订阅号和服务号认证后均可用
 
 string group_id = this.DDLGroupList.SelectedValue.ToString();
 
 
 WeiXinServer wxs = new WeiXinServer();
 
 ///从缓存读取accesstoken
 string Access_token = Cache["Access_token"] as string;
 
 if (Access_token == null)
 {
 //如果为空,重新获取
 Access_token = wxs.GetAccessToken();
 
 //设置缓存的数据7000秒后过期
 Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
 }
 
 string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);
 
 
 string posturl = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=" + Access_tokento;
 
 ///群发POST数据示例如下:
 // {
 // "filter":{
 // "is_to_all":false
 // "group_id":"2"
 // },
 // "mpnews":{
 // "media_id":"123dsdajkasd231jhksad"
 // },
 // "msgtype":"mpnews"
 //}
 
 string postData = "{\"filter\":{\"is_to_all\":\"false\"\"group_id\":\""+group_id+
 "\"},\"mpnews\":{\"media_id\":\"" + this.lbtuwenmedai_id.Text.ToString() +
 "\"},\"msgtype\":\"mpnews\"}";
 
 
 string tuwenres = wxs.GetPage(posturl, postData);
 
 //使用前需药引用Newtonsoft.json.dll文件
 JObject jsonObj = JObject.Parse(tuwenres);
 
 if (jsonObj["errcode"].ToString().Equals("0"))
 {
 Session["media_id"] = null;
 WxMassInfo wmi = new WxMassInfo();
 if (Session["wmninfo"] != null)
 {
 WxMpNewsInfo wmninfo = Session["wmninfo"] as WxMpNewsInfo;
 
 wmi.title = wmninfo.title.ToString();
 wmi.contents = wmninfo.contents.ToString();
 wmi.ImageUrl = wmninfo.ImageUrl.ToString();
 
 
 wmi.type = "图文";
 
 if (this.DDLMassType.SelectedValue.ToString().Equals("0"))
 {
  wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();
 }
 else
 {
  wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();
 }
 
 wmi.massStatus = "成功";//群发成功之后返回的状态码
 wmi.massMessageID = jsonObj["msg_id"].ToString();//群发成功之后返回的消息ID
 
 wmi.massDate = System.DateTime.Now.ToString();
 
 int num = wms.AddWxMassInfo(wmi);
 
 if (num > 0)
 {
  Session["wmninfo"] = null;
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!数据已保存!');location='WxMassManage.aspx';", true);
  return;
 }
 else
 {
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!数据保存失败!');", true);
  return;
 }
 }
 else
 {
 wmi.title = "";
 wmi.contents = "";
 wmi.ImageUrl = "";
 wmi.type = "图文";
 
 if (this.DDLMassType.SelectedValue.ToString().Equals("0"))
 {
  wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();
 }
 else
 {
  wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();
 }
 
 wmi.massStatus = "成功";//群发成功之后返回的状态码
 wmi.massMessageID = jsonObj["msg_id"].ToString();//群发成功之后返回的消息ID
 
 wmi.massDate = System.DateTime.Now.ToString();
 
 int num = wms.AddWxMassInfo(wmi);
 
 if (num > 0)
 {
  Session["wmninfo"] = null;
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!图文部分数据已保存!');location='WxMassManage.aspx';", true);
  return;
 }
 else
 {
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务已提交成功!!!数据保存失败!');", true);
  return;
 }
 }
 }
 else
 {
 ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群发任务提交失败!!');", true);
 return;
 }
 }
}
}
}

发送前预览

?
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
/// <summary>
 /// 发送前预览
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 protected void LinkBtnSendPreview_Click(object sender, EventArgs e)
 {
 WeiXinServer wxs = new WeiXinServer();
 
 ///从缓存读取accesstoken
 string Access_token = Cache["Access_token"] as string;
 
 if (Access_token == null)
 {
 //如果为空,重新获取
 Access_token = wxs.GetAccessToken();
 
 //设置缓存的数据7000秒后过期
 Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
 }
 
 string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);
 
 
 string posturl = "https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=" + Access_tokento;
 
 ///如果选择的是文本消息
 if (this.RadioBtnList.SelectedValue.ToString().Equals("0"))
 {
 if (String.IsNullOrWhiteSpace(this.txtwenben.InnerText.ToString().Trim()))
 {
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('请输入您要发送预览的文本内容!');", true);
  return;
 }
 if (this.txttoUserName.Value.ToString().Trim().Equals("请输入用户微信号"))
 {
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('请输入接收消息的用户微信号!');", true);
  return;
 }
 //文本消息的json数据{
 // "touser":"OPENID", 可改为对微信号预览,例如towxname:zhangsan
 // "text":{
 // "content":"CONTENT"
 // },
 // "msgtype":"text"
 //}
 string postData = "{\"towxname\":\"" + this.txttoUserName.Value.ToString() +
   "\",\"text\":{\"content\":\"" + this.txtwenben.InnerText.ToString() +
   "\"},\"msgtype\":\"text\"}";
 
 string tuwenres = wxs.GetPage(posturl, postData);
 
 //使用前需药引用Newtonsoft.json.dll文件
 JObject jsonObj = JObject.Parse(tuwenres);
 
 if (jsonObj["errcode"].ToString().Equals("0"))
 {
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('发送预览成功!!');", true);
  return;
 }
 else
 {
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('发送预览失败!!');", true);
  return;
 }
 }
 //如果选择的是图文消息
 if (this.RadioBtnList.SelectedValue.ToString().Equals("1"))
 {
 if(String.IsNullOrWhiteSpace(this.lbtuwenmedai_id.Text.ToString().Trim()))
 {
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('请选择要预览的图文素材!');", true);
  return;
 }
 if (this.txttoUserName.Value.ToString().Trim().Equals("请输入用户微信号"))
 {
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('请输入接收消息的用户微信号!');", true);
  return;
 }
 //图文消息的json数据{
 // "touser":"OPENID", 可改为对微信号预览,例如towxname:zhangsan
  // "mpnews":{
  // "media_id":"123dsdajkasd231jhksad"
  // },
  // "msgtype":"mpnews"
  //}
 string postData = "{\"towxname\":\"" + this.txttoUserName.Value.ToString() +
  "\",\"mpnews\":{\"media_id\":\"" + this.lbtuwenmedai_id.Text.ToString() +
  "\"},\"msgtype\":\"mpnews\"}";
 
 string tuwenres = wxs.GetPage(posturl, postData);
 
 //使用前需药引用Newtonsoft.json.dll文件
 JObject jsonObj = JObject.Parse(tuwenres);
 
 if (jsonObj["errcode"].ToString().Equals("0"))
 {
  Session["media_id"] = null;
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('发送预览成功!!');", true);
  return;
 }
 else
 {
  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('发送预览失败!!');", true);
  return;
 }
 
 
 }
 
 }

关键部分,获取全部用户的openID并串联成字符串:

?
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
/// <summary>
/// 获取所有微信用户的OpenID
/// </summary>
/// <returns></returns>
protected string GetAllUserOpenIDList()
{
StringBuilder sb = new StringBuilder();
 
WeiXinServer wxs = new WeiXinServer();
 
///从缓存读取accesstoken
string Access_token = Cache["Access_token"] as string;
 
if (Access_token == null)
{
//如果为空,重新获取
Access_token = wxs.GetAccessToken();
 
//设置缓存的数据7000秒后过期
Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
}
 
string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);
 
string jsonres = "";
 
string content = Cache["AllUserOpenList_content"] as string;
 
if (content == null)
{
jsonres = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + Access_tokento;
 
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(jsonres);
myRequest.Method = "GET";
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
content = reader.ReadToEnd();
reader.Close();
 
//设置缓存的数据7000秒后过期
Cache.Insert("AllUserOpenList_content", content, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
}
 
//使用前需要引用Newtonsoft.json.dll文件
JObject jsonObj = JObject.Parse(content);
 
 
if (jsonObj.ToString().Contains("count"))
{
int totalnum = int.Parse(jsonObj["count"].ToString());
 
 
 
for (int i = 0; i < totalnum; i++)
{
 sb.Append('"');
 sb.Append(jsonObj["data"]["openid"][i].ToString());
 sb.Append('"');
 sb.Append(",");
}
}
 
return sb.Remove(sb.ToString().LastIndexOf(","),1).ToString();
}

至此结束,下一章将继续讲解群发图文信息,因群发图文信息之前,需要先上传图文信息所需的素材,获取media_id,所以本章不做介绍,下一章将介绍新建单图文信息并群发,希望大家喜欢。

相关文章

热门资讯

玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
配置IIS网站web服务器的安全策略配置解决方案
配置IIS网站web服务器的安全策略配置解决方案 2019-05-23
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情 2019-06-22
Nginx服务器究竟是怎么执行PHP项目
Nginx服务器究竟是怎么执行PHP项目 2019-05-24
返回顶部