微信公众号开发之根据OpenID列表群发(十四)
<p>上一篇我们讲述了《微信公众号开发之根据标签进行群发(十二)》,这次我们讲解一下【根据OpenID列表群发】</p><h3 id="根据OpenID列表群发【订阅号不可用,服务号认证后可用】">根据OpenID列表群发【订阅号不可用,服务号认证后可用】</h3>
<p>接口调用请求说明</p>
<blockquote>
<p>http请求方式: POST https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=ACCESS_TOKEN</p>
</blockquote>
<p>POST数据说明</p>
<p>POST数据示例如下:</p>
<p>图文消息(注意图文消息的media_id需要通过上述方法来得到):</p>
<div class="cke_widget_wrapper cke_widget_block cke_widget_codeSnippet cke_widget_wrapper_has cke_widget_selected" data-cke-widget-wrapper="1" data-cke-filter="off" data-cke-display-name="代码段" data-cke-widget-id="15">
<pre class="has cke_widget_element" data-cke-widget-data="{&quot;code&quot;:&quot;{\n \&quot;touser\&quot;:[\n \&quot;OPENID1\&quot;,\n \&quot;OPENID2\&quot;\n ],\n \&quot;mpnews\&quot;:{\n \&quot;media_id\&quot;:\&quot;123dsdajkasd231jhksad\&quot;\n },\n \&quot;msgtype\&quot;:\&quot;mpnews\&quot;,\n \&quot;send_ignore_reprint\&quot;:0\n}\n&quot;,&quot;classes&quot;:{&quot;has&quot;:1}}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet"><code class="hljs">{
"touser":[
"OPENID1",
"OPENID2"
],
"mpnews":{
"media_id":"123dsdajkasd231jhksad"
},
"msgtype":"mpnews",
"send_ignore_reprint":0
}
</code></pre>
<span class="cke_reset cke_widget_drag_handler_container"><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw=="></span></div>
<p>文本:</p>
<div class="cke_widget_wrapper cke_widget_block cke_widget_codeSnippet cke_widget_wrapper_has cke_widget_selected" data-cke-widget-wrapper="1" data-cke-filter="off" data-cke-display-name="代码段" data-cke-widget-id="14">
<pre class="has cke_widget_element" data-cke-widget-data="{&quot;code&quot;:&quot;{\n \&quot;touser\&quot;:[\n \&quot;OPENID1\&quot;,\n \&quot;OPENID2\&quot;\n ],\n \&quot;msgtype\&quot;: \&quot;text\&quot;,\n \&quot;text\&quot;: { \&quot;content\&quot;: \&quot;hello from boxer.\&quot;}\n}\n&quot;,&quot;classes&quot;:{&quot;has&quot;:1}}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet"><code class="hljs">{
"touser":[
"OPENID1",
"OPENID2"
],
"msgtype": "text",
"text": { "content": "hello from boxer."}
}
</code></pre>
<span class="cke_reset cke_widget_drag_handler_container"><img class="cke_reset cke_widget_drag_handler lazyload" title="点击并拖拽以移动" alt="" width="15" height="15" data-cke-widget-drag-handler="1" data-src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw=="></span></div>
<p>语音:</p>
<div class="cke_widget_wrapper cke_widget_block cke_widget_codeSnippet cke_widget_wrapper_has cke_widget_selected" data-cke-widget-wrapper="1" data-cke-filter="off" data-cke-display-name="代码段" data-cke-widget-id="13">
<pre class="has cke_widget_element" data-cke-widget-data="{&quot;code&quot;:&quot;{\n \&quot;touser\&quot;:[\n \&quot;OPENID1\&quot;,\n \&quot;OPENID2\&quot;\n ],\n \&quot;voice\&quot;:{\n \&quot;media_id\&quot;:\&quot;mLxl6paC7z2Tl-NJT64yzJve8T9c8u9K2x-Ai6Ujd4lIH9IBuF6-2r66mamn_gIT\&quot;\n },\n \&quot;msgtype\&quot;:\&quot;voice\&quot;\n}\n&quot;,&quot;classes&quot;:{&quot;has&quot;:1}}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet"><code class="hljs">{
"touser":[
"OPENID1",
"OPENID2"
],
"voice":{
"media_id":"mLxl6paC7z2Tl-NJT64yzJve8T9c8u9K2x-Ai6Ujd4lIH9IBuF6-2r66mamn_gIT"
},
"msgtype":"voice"
}
</code></pre>
<span class="cke_reset cke_widget_drag_handler_container"><img class="cke_reset cke_widget_drag_handler lazyload" title="点击并拖拽以移动" alt="" width="15" height="15" data-cke-widget-drag-handler="1" data-src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw=="></span></div>
<p>图片:</p>
<div class="cke_widget_wrapper cke_widget_block cke_widget_codeSnippet cke_widget_wrapper_has cke_widget_selected" data-cke-widget-wrapper="1" data-cke-filter="off" data-cke-display-name="代码段" data-cke-widget-id="12">
<pre class="has cke_widget_element" data-cke-widget-data="{&quot;code&quot;:&quot;{\n \&quot;touser\&quot;:[\n \&quot;OPENID1\&quot;,\n \&quot;OPENID2\&quot;\n ],\n \&quot;image\&quot;:{\n \&quot;media_id\&quot;:\&quot;BTgN0opcW3Y5zV_ZebbsD3NFKRWf6cb7OPswPi9Q83fOJHK2P67dzxn11Cp7THat\&quot;\n },\n \&quot;msgtype\&quot;:\&quot;image\&quot;\n}\n&quot;,&quot;classes&quot;:{&quot;has&quot;:1}}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet"><code class="hljs">{
"touser":[
"OPENID1",
"OPENID2"
],
"image":{
"media_id":"BTgN0opcW3Y5zV_ZebbsD3NFKRWf6cb7OPswPi9Q83fOJHK2P67dzxn11Cp7THat"
},
"msgtype":"image"
}
</code></pre>
<span class="cke_reset cke_widget_drag_handler_container"><img class="cke_reset cke_widget_drag_handler lazyload" title="点击并拖拽以移动" alt="" width="15" height="15" data-cke-widget-drag-handler="1" data-src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw=="></span></div>
<p>视频:</p>
<p>请注意,此处视频的media_id需通过POST请求到下述接口特别地得到: https://api.weixin.qq.com/cgi-bin/media/uploadvideo?access_token=ACCESS_TOKEN POST数据如下(此处media_id需通过素材管理->新增素材来得到):</p>
<div class="cke_widget_wrapper cke_widget_block cke_widget_codeSnippet cke_widget_wrapper_has cke_widget_selected" data-cke-widget-wrapper="1" data-cke-filter="off" data-cke-display-name="代码段" data-cke-widget-id="11">
<pre class="has cke_widget_element" data-cke-widget-data="{&quot;code&quot;:&quot;{\n\&quot;media_id\&quot;: \&quot;rF4UdIMfYK3efUfyoddYRMU50zMiRmmt_l0kszupYh_SzrcW5Gaheq05p_lHuOTQ\&quot;,\n\&quot;title\&quot;: \&quot;TITLE\&quot;,\n\&quot;description\&quot;: \&quot;Description\&quot;\n}\n&quot;,&quot;classes&quot;:{&quot;has&quot;:1}}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet"><code class="hljs">{
"media_id": "rF4UdIMfYK3efUfyoddYRMU50zMiRmmt_l0kszupYh_SzrcW5Gaheq05p_lHuOTQ",
"title": "TITLE",
"description": "Description"
}
</code></pre>
<span class="cke_reset cke_widget_drag_handler_container"><img class="cke_reset cke_widget_drag_handler lazyload" title="点击并拖拽以移动" alt="" width="15" height="15" data-cke-widget-drag-handler="1" data-src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw=="></span></div>
<p>返回将为</p>
<div class="cke_widget_wrapper cke_widget_block cke_widget_codeSnippet cke_widget_wrapper_has cke_widget_selected" data-cke-widget-wrapper="1" data-cke-filter="off" data-cke-display-name="代码段" data-cke-widget-id="10">
<pre class="has cke_widget_element" data-cke-widget-data="{&quot;code&quot;:&quot;{\n\&quot;type\&quot;:\&quot;video\&quot;,\n\&quot;media_id\&quot;:\&quot;IhdaAQXuvJtGzwwc0abfXnzeezfO0NgPK6AQYShD8RQYMTtfzbLdBIQkQziv2XJc\&quot;,\n\&quot;created_at\&quot;:1398848981\n}\n&quot;,&quot;classes&quot;:{&quot;has&quot;:1}}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet"><code class="hljs">{
"type":"video",
"media_id":"IhdaAQXuvJtGzwwc0abfXnzeezfO0NgPK6AQYShD8RQYMTtfzbLdBIQkQziv2XJc",
"created_at":1398848981
}
</code></pre>
<span class="cke_reset cke_widget_drag_handler_container"><img class="cke_reset cke_widget_drag_handler lazyload" title="点击并拖拽以移动" alt="" width="15" height="15" data-cke-widget-drag-handler="1" data-src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw=="></span></div>
<p>然后,POST下述数据(将media_id改为上一步中得到的media_id),即可进行发送</p>
<div class="cke_widget_wrapper cke_widget_block cke_widget_codeSnippet cke_widget_wrapper_has cke_widget_selected" data-cke-widget-wrapper="1" data-cke-filter="off" data-cke-display-name="代码段" data-cke-widget-id="9">
<pre class="has cke_widget_element" data-cke-widget-data="{&quot;code&quot;:&quot;{\n \&quot;touser\&quot;:[\n \&quot;OPENID1\&quot;,\n \&quot;OPENID2\&quot;\n ],\n \&quot;mpvideo\&quot;:{\n \&quot;media_id\&quot;:\&quot;123dsdajkasd231jhksad\&quot;,\n \&quot;title\&quot;:\&quot;TITLE\&quot;,\n \&quot;description\&quot;:\&quot;DESCRIPTION\&quot;\n },\n \&quot;msgtype\&quot;:\&quot;mpvideo\&quot;\n}\n&quot;,&quot;classes&quot;:{&quot;has&quot;:1}}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet"><code class="hljs">{
"touser":[
"OPENID1",
"OPENID2"
],
"mpvideo":{
"media_id":"123dsdajkasd231jhksad",
"title":"TITLE",
"description":"DESCRIPTION"
},
"msgtype":"mpvideo"
}
</code></pre>
<span class="cke_reset cke_widget_drag_handler_container"><img class="cke_reset cke_widget_drag_handler lazyload" title="点击并拖拽以移动" alt="" width="15" height="15" data-cke-widget-drag-handler="1" data-src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw=="></span></div>
<p>卡券:</p>
<div class="cke_widget_wrapper cke_widget_block cke_widget_codeSnippet cke_widget_wrapper_has cke_widget_selected" data-cke-widget-wrapper="1" data-cke-filter="off" data-cke-display-name="代码段" data-cke-widget-id="8">
<pre class="has cke_widget_element" data-cke-widget-data="{&quot;code&quot;:&quot;{\n \&quot;touser\&quot;:[\n \&quot;OPENID1\&quot;,\n \&quot;OPENID2\&quot;\n ],\n \&quot;wxcard\&quot;: {\&quot;card_id\&quot;:\&quot;123dsdajkasd231jhksad\&quot;}\n \&quot;msgtype\&quot;:\&quot;wxcard\&quot;\n}\n&quot;,&quot;classes&quot;:{&quot;has&quot;:1}}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet"><code class="hljs">{
"touser":[
"OPENID1",
"OPENID2"
],
"wxcard": {"card_id":"123dsdajkasd231jhksad"}
"msgtype":"wxcard"
}
</code></pre>
<span class="cke_reset cke_widget_drag_handler_container"><img class="cke_reset cke_widget_drag_handler lazyload" title="点击并拖拽以移动" alt="" width="15" height="15" data-cke-widget-drag-handler="1" data-src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw=="></span></div>
<table class=" cke_show_border">
<thead>
<tr><th>参数</th><th>是否必须</th><th>说明</th></tr>
</thead>
<tbody>
<tr>
<td>touser</td>
<td>是</td>
<td>填写图文消息的接收者,一串OpenID列表,OpenID最少2个,最多10000个</td>
</tr>
<tr>
<td>mpnews</td>
<td>是</td>
<td>用于设定即将发送的图文消息</td>
</tr>
<tr>
<td>media_id</td>
<td>是</td>
<td>用于群发的图文消息的media_id</td>
</tr>
<tr>
<td>msgtype</td>
<td>是</td>
<td>群发的消息类型,图文消息为mpnews,文本消息为text,语音为voice,音乐为music,图片为image,视频为video,卡券为wxcard</td>
</tr>
<tr>
<td>title</td>
<td>否</td>
<td>消息的标题</td>
</tr>
<tr>
<td>description</td>
<td>否</td>
<td>消息的描述</td>
</tr>
<tr>
<td>thumb_media_id</td>
<td>是</td>
<td>视频缩略图的媒体ID</td>
</tr>
<tr>
<td>send_ignore_reprint</td>
<td>是</td>
<td>图文消息被判定为转载时,是否继续群发。 1为继续群发(转载),0为停止群发。 该参数默认为0。</td>
</tr>
</tbody>
</table>
<p>返回说明</p>
<p>返回数据示例(正确时的JSON返回结果):</p>
<div class="cke_widget_wrapper cke_widget_block cke_widget_codeSnippet cke_widget_wrapper_has cke_widget_selected" data-cke-widget-wrapper="1" data-cke-filter="off" data-cke-display-name="代码段" data-cke-widget-id="7">
<pre class="has cke_widget_element" data-cke-widget-data="{&quot;code&quot;:&quot;{\n \&quot;errcode\&quot;:0,\n \&quot;errmsg\&quot;:\&quot;send job submission success\&quot;,\n \&quot;msg_id\&quot;:34182, \n \&quot;msg_data_id\&quot;: 206227730\n}\n&quot;,&quot;classes&quot;:{&quot;has&quot;:1}}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet"><code class="hljs">{
"errcode":0,
"errmsg":"send job submission success",
"msg_id":34182,
"msg_data_id": 206227730
}
</code></pre>
<span class="cke_reset cke_widget_drag_handler_container"><img class="cke_reset cke_widget_drag_handler lazyload" title="点击并拖拽以移动" alt="" width="15" height="15" data-cke-widget-drag-handler="1" data-src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw=="></span></div>
<table class=" cke_show_border">
<thead>
<tr><th>参数</th><th>说明</th></tr>
</thead>
<tbody>
<tr>
<td>type</td>
<td>媒体文件类型,分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb),次数为news,即图文消息</td>
</tr>
<tr>
<td>errcode</td>
<td>错误码</td>
</tr>
<tr>
<td>errmsg</td>
<td>错误信息</td>
</tr>
<tr>
<td>msg_id</td>
<td>消息发送任务的ID</td>
</tr>
<tr>
<td>msg_data_id</td>
<td>消息的数据ID,,该字段只有在群发图文消息时,才会出现。可以用于在图文分析数据接口中,获取到对应的图文消息的数据,是图文分析数据接口中的msgid字段中的前半部分,详见图文分析数据接口中的msgid字段的介绍。</td>
</tr>
</tbody>
</table>
<p>请注意:在返回成功时,意味着群发任务提交成功,并不意味着此时群发已经结束,所以,仍有可能在后续的发送过程中出现异常情况导致用户未收到消息,如消息有时会进行审核、服务器不稳定等。此外,群发任务一般需要较长的时间才能全部发送完毕,请耐心等待。</p>
<p>错误时微信会返回错误码等信息,请根据错误码查询错误信息</p>
<p>一、在SendUtil中添加发送的字符串和发送接口两个方法</p>
<p><span class="cke_widget_wrapper cke_widget_inline cke_widget_image cke_image_nocaption cke_widget_selected" data-cke-widget-wrapper="1" data-cke-filter="off" data-cke-display-name="图像" data-cke-widget-id="6"><img class="has cke_widget_element lazyload" alt="" width="1200" height="870" src="https://img-blog.csdnimg.cn/20200120181756182.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70" data-cke-widget-data="{&quot;hasCaption&quot;:false,&quot;src&quot;:&quot;https://img-blog.csdnimg.cn/20200120181756182.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70&quot;,&quot;alt&quot;:&quot;&quot;,&quot;width&quot;:&quot;1200&quot;,&quot;height&quot;:&quot;870&quot;,&quot;lock&quot;:true,&quot;align&quot;:&quot;none&quot;,&quot;classes&quot;:{&quot;has&quot;:1}}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="image" data-src="https://img-blog.csdnimg.cn/20200120181756182.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70"><span class="cke_reset cke_widget_drag_handler_container"><img class="cke_reset cke_widget_drag_handler lazyload" title="点击并拖拽以移动" alt="" width="15" height="15" data-cke-widget-drag-handler="1" data-src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw=="><span class="cke_image_resizer" title="点击并拖拽以改变尺寸"></span></span></span></p>
<pre><code class="language-html">package com.xu.wemall.components.weixin;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xu.wemall.commons.constants.URIConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.util.List;
/**
* 功能:群发工具类
*/
@Slf4j
@Component
public class SendUtil {
@Autowired
private RestTemplate restTemplate;
@Autowired
private AccessTokenUtil accessTokenUtil;
private String createSendAllString(boolean isToAll, String tagId, String mediaId, String msgtype){
JSONObject data = new JSONObject();
JSONObject filter = new JSONObject();
filter.put("is_to_all",isToAll);
if(tagId != null){
filter.put("tag_id",tagId);
}
JSONObject type = new JSONObject();
if(mediaId != null){
type.put("media_id",mediaId);
}
data.put("filter",filter);
//图文消息
if(msgtype.equalsIgnoreCase("mpnews")){
data.put("mpnews",type);
data.put("send_ignore_reprint",1);
}else if(msgtype.equalsIgnoreCase("text")){
data.put("text",type); //文本
}else if(msgtype.equalsIgnoreCase("voice")){
data.put("voice",type); //声音
}else if(msgtype.equalsIgnoreCase("image")){
data.put("image",type); //图片
}else if(msgtype.equalsIgnoreCase("mpvideo")){
data.put("mpvideo",type); //声音
}else if(msgtype.equalsIgnoreCase("wxcard")) {
data.put("wxcard", type); //卡券
}
if(msgtype !=null){
data.put("msgtype",msgtype);
}
return data.toJSONString();
}
private String createSendString(List<String> openIdList, String mediaId, String msgtype){
JSONObject data = new JSONObject();
JSONArray touser = new JSONArray();
touser.addAll(openIdList);
data.put("touser",touser);
JSONObject type = new JSONObject();
if(mediaId != null){
type.put("media_id",mediaId);
}
//图文消息
if(msgtype.equalsIgnoreCase("mpnews")){
data.put("mpnews",type);
data.put("send_ignore_reprint",1);
}else if(msgtype.equalsIgnoreCase("text")){
data.put("text",type); //文本
}else if(msgtype.equalsIgnoreCase("voice")){
data.put("voice",type); //声音
}else if(msgtype.equalsIgnoreCase("image")){
data.put("image",type); //图片
}else if(msgtype.equalsIgnoreCase("mpvideo")){
data.put("mpvideo",type); //声音
}else if(msgtype.equalsIgnoreCase("wxcard")) {
data.put("wxcard", type); //卡券
}
if(msgtype !=null){
data.put("msgtype",msgtype);
}
return data.toJSONString();
}
/**
*根据标签进行群发
*/
public String sendByTagId(boolean isToAll, String tagId, String mediaId, String msgtype) {
String accessToken = accessTokenUtil.getAccessToken();
if (accessToken != null) {
log.info("URL{}", URIConstant.SEND_ALL_URL);
String url = URIConstant.SEND_ALL_URL.replace("ACCESS_TOKEN", accessToken);
log.info("SEND_ALL_URL:{}", url);
//将菜单对象转换成JSON字符串
String dataString = this.createSendAllString(isToAll, tagId, mediaId, msgtype);
log.info("dataString:{}",dataString);
//发起POST请求创建菜单
String jsonObject = restTemplate.postForObject(url, dataString,String.class);
return jsonObject;
}
return null;
}
/**
*根据OpenID列表群发
*/
public String sendByOpenId(List<String> touser, String mediaId, String msgtype) {
String accessToken = accessTokenUtil.getAccessToken();
if (accessToken != null) {
log.info("URL{}", URIConstant.SEND_URL);
String url = URIConstant.SEND_URL.replace("ACCESS_TOKEN", accessToken);
log.info("SEND_URL:{}", url);
//将菜单对象转换成JSON字符串
String dataString = this.createSendString(touser, mediaId, msgtype);
log.info("dataString:{}",dataString);
//发起POST请求创建菜单
String jsonObject = restTemplate.postForObject(url, dataString,String.class);
return jsonObject;
}
return null;
}
}
</code>二、在SendController中添加swagger方法</pre>
<p><span class="cke_widget_wrapper cke_widget_inline cke_widget_image cke_image_nocaption cke_widget_selected" data-cke-widget-wrapper="1" data-cke-filter="off" data-cke-display-name="图像" data-cke-widget-id="5"><img class="has cke_widget_element lazyload" alt="" width="1200" height="531" src="https://img-blog.csdnimg.cn/2020012018192547.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70" data-cke-widget-data="{&quot;hasCaption&quot;:false,&quot;src&quot;:&quot;https://img-blog.csdnimg.cn/2020012018192547.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70&quot;,&quot;alt&quot;:&quot;&quot;,&quot;width&quot;:&quot;1200&quot;,&quot;height&quot;:&quot;531&quot;,&quot;lock&quot;:true,&quot;align&quot;:&quot;none&quot;,&quot;classes&quot;:{&quot;has&quot;:1}}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="image" data-src="https://img-blog.csdnimg.cn/2020012018192547.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70"><span class="cke_reset cke_widget_drag_handler_container"><img class="cke_reset cke_widget_drag_handler lazyload" title="点击并拖拽以移动" alt="" width="15" height="15" data-cke-widget-drag-handler="1" data-src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw=="><span class="cke_image_resizer" title="点击并拖拽以改变尺寸"></span></span></span></p>
<div class="cke_widget_wrapper cke_widget_block cke_widget_codeSnippet cke_widget_wrapper_has cke_widget_selected" data-cke-widget-wrapper="1" data-cke-filter="off" data-cke-display-name="代码段" data-cke-widget-id="4">
<pre class="has cke_widget_element" data-cke-widget-data="{&quot;lang&quot;:&quot;html&quot;,&quot;code&quot;:&quot;package com.xu.wemall.controller.weixin;\n\nimport com.xu.wemall.components.weixin.SendUtil;\nimport io.swagger.annotations.Api;\nimport io.swagger.annotations.ApiImplicitParam;\nimport io.swagger.annotations.ApiImplicitParams;\nimport io.swagger.annotations.ApiOperation;\nimport lombok.extern.slf4j.Slf4j;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.web.bind.annotation.RequestMapping;\nimport org.springframework.web.bind.annotation.RequestMethod;\nimport org.springframework.web.bind.annotation.RequestParam;\nimport org.springframework.web.bind.annotation.RestController;\n\nimport java.util.List;\n\n/**\n * 类名称: SendController\n * 类描述: 群发API\n */\n@Slf4j\n@RestController\n@Api(tags = \&quot;群发接口\&quot;)\n@RequestMapping(value = \&quot;/send\&quot;)\npublic class SendController {\n\n @Autowired\n private SendUtil sendUtil;\n\n /**\n * 根据标签进行群发\n */\n @ApiOperation(value = \&quot;根据标签进行群发\&quot;)\n @RequestMapping(value = \&quot;/sendByTagId\&quot;, method = RequestMethod.POST)\n @ApiImplicitParams({\n @ApiImplicitParam(name=\&quot;isToAll\&quot;,value=\&quot;用于设定是否向全部用户发送,值为true或false,选择true该消息群发给所有用户,选择false可根据tag_id发送给指定群组的用户\&quot;, paramType=\&quot;query\&quot;,dataType=\&quot;String\&quot;),\n @ApiImplicitParam(name=\&quot;tagId\&quot;,value=\&quot;群发到的标签的tag_id,参见用户管理中用户分组接口,若is_to_all值为true,可不填写tag_id\&quot;, paramType=\&quot;query\&quot;,dataType=\&quot;String\&quot;),\n @ApiImplicitParam(name=\&quot;mediaId\&quot;,value=\&quot;用于群发的消息的media_id\&quot;),\n @ApiImplicitParam(name=\&quot;msgtype\&quot;,value=\&quot;群发的消息类型,图文消息为mpnews,文本消息为text,语音为voice,音乐为music,图片为image,视频为video,卡券为wxcard\&quot;, paramType=\&quot;query\&quot;,dataType=\&quot;Integer\&quot;)\n })\n public Object sendByTagId(boolean isToAll, String tagId, String mediaId, String msgtype) {\n\n String tempString = sendUtil.sendByTagId(isToAll, tagId, mediaId,msgtype);\n return tempString;\n\n }\n\n /**\n * 根据OpenID列表群发\n */\n @ApiOperation(value = \&quot;根据标签进行群发\&quot;)\n @RequestMapping(value = \&quot;/sendByOpenId\&quot;, method = RequestMethod.POST)\n @ApiImplicitParams({\n @ApiImplicitParam(name=\&quot;mediaId\&quot;,value=\&quot;用于群发的消息的media_id\&quot;),\n @ApiImplicitParam(name=\&quot;msgtype\&quot;,value=\&quot;群发的消息类型,图文消息为mpnews,文本消息为text,语音为voice,音乐为music,图片为image,视频为video,卡券为wxcard\&quot;, paramType=\&quot;query\&quot;,dataType=\&quot;Integer\&quot;)\n })\n public Object sendByOpenId(@RequestParam(value = \&quot;touser\&quot;) List&lt;String&gt; touser, String mediaId, String msgtype) {\n\n String tempString = sendUtil.sendByOpenId(touser, mediaId,msgtype);\n return tempString;\n\n }\n\n\n}\n&quot;,&quot;classes&quot;:{&quot;has&quot;:1}}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet"><code class="language-html hljs">package com.xu.wemall.controller.weixin;
import com.xu.wemall.components.weixin.SendUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* 类名称: SendController
* 类描述: 群发API
*/
@Slf4j
@RestController
@Api(tags = "群发接口")
@RequestMapping(value = "/send")
public class SendController {
@Autowired
private SendUtil sendUtil;
/**
* 根据标签进行群发
*/
@ApiOperation(value = "根据标签进行群发")
@RequestMapping(value = "/sendByTagId", method = RequestMethod.POST)
@ApiImplicitParams({
@ApiImplicitParam(name="isToAll",value="用于设定是否向全部用户发送,值为true或false,选择true该消息群发给所有用户,选择false可根据tag_id发送给指定群组的用户", paramType="query",dataType="String"),
@ApiImplicitParam(name="tagId",value="群发到的标签的tag_id,参见用户管理中用户分组接口,若is_to_all值为true,可不填写tag_id", paramType="query",dataType="String"),
@ApiImplicitParam(name="mediaId",value="用于群发的消息的media_id"),
@ApiImplicitParam(name="msgtype",value="群发的消息类型,图文消息为mpnews,文本消息为text,语音为voice,音乐为music,图片为image,视频为video,卡券为wxcard", paramType="query",dataType="Integer")
})
public Object sendByTagId(boolean isToAll, String tagId, String mediaId, String msgtype) {
String tempString = sendUtil.sendByTagId(isToAll, tagId, mediaId,msgtype);
return tempString;
}
/**
* 根据OpenID列表群发
*/
@ApiOperation(value = "根据标签进行群发")
@RequestMapping(value = "/sendByOpenId", method = RequestMethod.POST)
@ApiImplicitParams({
@ApiImplicitParam(name="mediaId",value="用于群发的消息的media_id"),
@ApiImplicitParam(name="msgtype",value="群发的消息类型,图文消息为mpnews,文本消息为text,语音为voice,音乐为music,图片为image,视频为video,卡券为wxcard", paramType="query",dataType="Integer")
})
public Object sendByOpenId(@RequestParam(value = "touser") List<span class="hljs-tag"><<span class="hljs-name">String> touser, String mediaId, String msgtype) {
String tempString = sendUtil.sendByOpenId(touser, mediaId,msgtype);
return tempString;
}
}
</span></span></code></pre>
<span class="cke_reset cke_widget_drag_handler_container"><img class="cke_reset cke_widget_drag_handler lazyload" title="点击并拖拽以移动" alt="" width="15" height="15" data-cke-widget-drag-handler="1" data-src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw=="></span></div>
<p> <strong>三,测试</strong></p>
<p>我们需要先通过获取关注公众号的用户获得至少两个openid,然后测试如下</p>
<p><span class="cke_widget_wrapper cke_widget_inline cke_widget_image cke_image_nocaption cke_widget_selected" data-cke-widget-wrapper="1" data-cke-filter="off" data-cke-display-name="图像" data-cke-widget-id="3"><img class="has cke_widget_element lazyload" alt="" width="1186" height="885" src="https://img-blog.csdnimg.cn/20200120182050310.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70" data-cke-widget-data="{&quot;hasCaption&quot;:false,&quot;src&quot;:&quot;https://img-blog.csdnimg.cn/20200120182050310.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70&quot;,&quot;alt&quot;:&quot;&quot;,&quot;width&quot;:&quot;1186&quot;,&quot;height&quot;:&quot;885&quot;,&quot;lock&quot;:true,&quot;align&quot;:&quot;none&quot;,&quot;classes&quot;:{&quot;has&quot;:1}}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="image" data-src="https://img-blog.csdnimg.cn/20200120182050310.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70"><span class="cke_reset cke_widget_drag_handler_container"><img class="cke_reset cke_widget_drag_handler lazyload" title="点击并拖拽以移动" alt="" width="15" height="15" data-cke-widget-drag-handler="1" data-src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw=="><span class="cke_image_resizer" title="点击并拖拽以改变尺寸"></span></span></span></p>
<p>结果(泪奔中……)结果又是没有权限,因为我们用的是测试账号,测试账号现在已经没有群发功能权限了</p>
<p><span class="cke_widget_wrapper cke_widget_inline cke_widget_image cke_image_nocaption cke_widget_selected" data-cke-widget-wrapper="1" data-cke-filter="off" data-cke-display-name="图像" data-cke-widget-id="2"><img class="has cke_widget_element lazyload" alt="" width="1200" height="742" src="https://img-blog.csdnimg.cn/20200120182234558.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70" data-cke-widget-data="{&quot;hasCaption&quot;:false,&quot;src&quot;:&quot;https://img-blog.csdnimg.cn/20200120182234558.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70&quot;,&quot;alt&quot;:&quot;&quot;,&quot;width&quot;:&quot;1200&quot;,&quot;height&quot;:&quot;742&quot;,&quot;lock&quot;:true,&quot;align&quot;:&quot;none&quot;,&quot;classes&quot;:{&quot;has&quot;:1}}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="image" data-src="https://img-blog.csdnimg.cn/20200120182234558.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70"><span class="cke_reset cke_widget_drag_handler_container"><img class="cke_reset cke_widget_drag_handler lazyload" title="点击并拖拽以移动" alt="" width="15" height="15" data-cke-widget-drag-handler="1" data-src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw=="><span class="cke_image_resizer" title="点击并拖拽以改变尺寸"></span></span></span></p>
<p>查询微信公众号开发文档中的【全局返回码说明】确认的确是没有这个接口的权限,哎</p>
<p><span class="cke_widget_wrapper cke_widget_inline cke_widget_image cke_image_nocaption cke_widget_selected" data-cke-widget-wrapper="1" data-cke-filter="off" data-cke-display-name="图像" data-cke-widget-id="1"><img class="has cke_widget_element lazyload" alt="" width="1200" height="464" src="https://img-blog.csdnimg.cn/20200121091728646.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70" data-cke-widget-data="{&quot;hasCaption&quot;:false,&quot;src&quot;:&quot;https://img-blog.csdnimg.cn/20200121091728646.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70&quot;,&quot;alt&quot;:&quot;&quot;,&quot;width&quot;:&quot;1200&quot;,&quot;height&quot;:&quot;464&quot;,&quot;lock&quot;:true,&quot;align&quot;:&quot;none&quot;,&quot;classes&quot;:{&quot;has&quot;:1}}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="image" data-src="https://img-blog.csdnimg.cn/20200121091728646.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70"><span class="cke_reset cke_widget_drag_handler_container"><img class="cke_reset cke_widget_drag_handler lazyload" title="点击并拖拽以移动" alt="" width="15" height="15" data-cke-widget-drag-handler="1" data-src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw=="><span class="cke_image_resizer" title="点击并拖拽以改变尺寸"></span></span></span></p>
<p><span class="cke_widget_wrapper cke_widget_inline cke_widget_image cke_image_nocaption cke_widget_selected" data-cke-widget-wrapper="1" data-cke-filter="off" data-cke-display-name="图像" data-cke-widget-id="0"><img class="has cke_widget_element lazyload" alt="" width="730" height="401" src="https://img-blog.csdnimg.cn/20200121091617696.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70" data-cke-widget-data="{&quot;hasCaption&quot;:false,&quot;src&quot;:&quot;https://img-blog.csdnimg.cn/20200121091617696.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70&quot;,&quot;alt&quot;:&quot;&quot;,&quot;width&quot;:&quot;730&quot;,&quot;height&quot;:&quot;401&quot;,&quot;lock&quot;:true,&quot;align&quot;:&quot;none&quot;,&quot;classes&quot;:{&quot;has&quot;:1}}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="image" data-src="https://img-blog.csdnimg.cn/20200121091617696.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70"><span class="cke_reset cke_widget_drag_handler_container"><img class="cke_reset cke_widget_drag_handler lazyload" title="点击并拖拽以移动" alt="" width="15" height="15" data-cke-widget-drag-handler="1" data-src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw=="><span class="cke_image_resizer" title="点击并拖拽以改变尺寸"></span></span></span></p>
<h3>如果您觉得此文有帮助,可以小小打赏一下,持续更新更有动力哟!</h3>
<p><img alt="" width="218" height="274" data-src="https://img2020.cnblogs.com/blog/1501920/202004/1501920-20200402133129576-79870337.png"><img src="https://img2020.cnblogs.com/blog/1501920/202004/1501920-20200402133221455-2112921293.png"></p>
<p>谢谢观看,下回我们继续不见不散!</p><br><br>
来源:https://www.cnblogs.com/xulijun137/p/12222165.html
頁:
[1]