微信公众号开发之群发消息预览接口(十五)
<h3 id="预览接口【订阅号与服务号认证后均可用】">预览接口【订阅号与服务号认证后均可用】</h3><p>开发者可通过该接口发送消息给指定用户,在手机端查看消息的样式和排版。为了满足第三方平台开发者的需求,在保留对openID预览能力的同时,增加了对指定微信号发送预览的能力,但该能力每日调用次数有限制(100次),请勿滥用。</p>
<p>接口调用请求说明</p>
<p>http请求方式: POST https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=ACCESS_TOKEN</p>
<p>POST数据说明</p>
<p>POST数据示例如下:</p>
<p>图文消息(其中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="17">
<pre class="has cke_widget_element" data-cke-widget-data="{&quot;code&quot;:&quot;{\n \&quot;touser\&quot;:\&quot;OPENID\&quot;, \n \&quot;mpnews\&quot;:{ \n \&quot;media_id\&quot;:\&quot;123dsdajkasd231jhksad\&quot; \n },\n \&quot;msgtype\&quot;:\&quot;mpnews\&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":"OPENID",
"mpnews":{
"media_id":"123dsdajkasd231jhksad"
},
"msgtype":"mpnews"
}
</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="16">
<pre class="has cke_widget_element" data-cke-widget-data="{&quot;code&quot;:&quot;{ \n \&quot;touser\&quot;:\&quot;OPENID\&quot;,\n \&quot;text\&quot;:{ \n \&quot;content\&quot;:\&quot;CONTENT\&quot; \n }, \n \&quot;msgtype\&quot;:\&quot;text\&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":"OPENID",
"text":{
"content":"CONTENT"
},
"msgtype":"text"
}
</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>语音(其中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="15">
<pre class="has cke_widget_element" data-cke-widget-data="{&quot;code&quot;:&quot;{\n \&quot;touser\&quot;:\&quot;OPENID\&quot;,\n \&quot;voice\&quot;:{ \n \&quot;media_id\&quot;:\&quot;123dsdajkasd231jhksad\&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":"OPENID",
"voice":{
"media_id":"123dsdajkasd231jhksad"
},
"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>图片(其中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="14">
<pre class="has cke_widget_element" data-cke-widget-data="{&quot;code&quot;:&quot;{\n \&quot;touser\&quot;:\&quot;OPENID\&quot;,\n \&quot;image\&quot;:{ \n \&quot;media_id\&quot;:\&quot;123dsdajkasd231jhksad\&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":"OPENID",
"image":{
"media_id":"123dsdajkasd231jhksad"
},
"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>视频(其中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="13">
<pre class="has cke_widget_element" data-cke-widget-data="{&quot;code&quot;:&quot;{\n \&quot;touser\&quot;:\&quot;OPENID\&quot;,\n \&quot;mpvideo\&quot;:{\&quot;media_id\&quot;:\&quot;IhdaAQXuvJtGzwwc0abfXnzeezfO0NgPK6AQYShD8RQYMTtfzbLdBIQkQziv2XJc\&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":"OPENID",
"mpvideo":{"media_id":"IhdaAQXuvJtGzwwc0abfXnzeezfO0NgPK6AQYShD8RQYMTtfzbLdBIQkQziv2XJc",
},
"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="12">
<pre class="has cke_widget_element" data-cke-widget-data="{&quot;code&quot;:&quot;{ \&quot;touser\&quot;:\&quot;OPENID\&quot;, \n\&quot;wxcard\&quot;:{ \n \&quot;card_id\&quot;:\&quot;123dsdajkasd231jhksad\&quot;,\n \&quot;card_ext\&quot;: \&quot;{\&quot;code\&quot;:\&quot;\&quot;,\&quot;openid\&quot;:\&quot;\&quot;,\&quot;timestamp\&quot;:\&quot;1402057159\&quot;,\&quot;signature\&quot;:\&quot;017bb17407c8e0058a66d72dcc61632b70f511ad\&quot;}\&quot; \n }, \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":"OPENID",
"wxcard":{
"card_id":"123dsdajkasd231jhksad",
"card_ext": "{"code":"","openid":"","timestamp":"1402057159","signature":"017bb17407c8e0058a66d72dcc61632b70f511ad"}"
},
"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>
<p>请注意,上述JSON数据中的touser字段都可以改为towxname,这样就可以针对微信号进行预览(而非openID),towxname和touser同时赋值时,以towxname优先。修改后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="11">
<pre class="has cke_widget_element" data-cke-widget-data="{&quot;code&quot;:&quot;{\n \&quot;towxname\&quot;:\&quot;示例的微信号\&quot;, \n \&quot;mpnews\&quot;:{ \n \&quot;media_id\&quot;:\&quot;123dsdajkasd231jhksad\&quot; \n },\n \&quot;msgtype\&quot;:\&quot;mpnews\&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">{
"towxname":"示例的微信号",
"mpnews":{
"media_id":"123dsdajkasd231jhksad"
},
"msgtype":"mpnews"
}
</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>touser</td>
<td>接收消息用户对应该公众号的openid,该字段也可以改为towxname,以实现对微信号的预览</td>
</tr>
<tr>
<td>msgtype</td>
<td>群发的消息类型,图文消息为mpnews,文本消息为text,语音为voice,音乐为music,图片为image,视频为video,卡券为wxcard</td>
</tr>
<tr>
<td>media_id</td>
<td>用于群发的消息的media_id</td>
</tr>
<tr>
<td>content</td>
<td>发送文本消息时文本的内容</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="10">
<pre class="has cke_widget_element" data-cke-widget-data="{&quot;code&quot;:&quot;{\n \&quot;errcode\&quot;:0,\n \&quot;errmsg\&quot;:\&quot;preview success\&quot;,\n \&quot;msg_id\&quot;:34182\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":"preview success",
"msg_id":34182
}
</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>errcode</td>
<td>错误码</td>
</tr>
<tr>
<td>errmsg</td>
<td>错误信息</td>
</tr>
<tr>
<td>msg_id</td>
<td>消息ID</td>
</tr>
</tbody>
</table>
<p><strong> 一、我们测试一下图文素材预览群发</strong></p>
<p><strong>【重要说明!!!】</strong><strong>1、这里发现一个文档bug,预览群发图文素材时候touser至少需要两个openid以上</strong></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="9"><img class="has cke_widget_element lazyload" alt="" width="1101" height="758" src="https://img-blog.csdnimg.cn/20200121151804435.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/20200121151804435.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;1101&quot;,&quot;height&quot;:&quot;758&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/20200121151804435.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><strong>2、图文素材预览接口测试账号暂时没有权限</strong></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="8"><img class="has cke_widget_element lazyload" alt="" width="1197" height="564" src="https://img-blog.csdnimg.cn/20200121152328912.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/20200121152328912.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;1197&quot;,&quot;height&quot;:&quot;564&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/20200121152328912.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="7"><img class="has cke_widget_element lazyload" alt="" width="1200" height="788" src="https://img-blog.csdnimg.cn/20200121152438637.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/20200121152438637.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;788&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/20200121152438637.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="6">
<pre class="has cke_widget_element" data-cke-widget-data="{&quot;lang&quot;:&quot;html&quot;,&quot;code&quot;:&quot;package com.xu.wemall.components.weixin;\n\nimport com.alibaba.fastjson.JSONArray;\nimport com.alibaba.fastjson.JSONObject;\nimport com.xu.wemall.commons.constants.URIConstant;\nimport lombok.extern.slf4j.Slf4j;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.stereotype.Component;\nimport org.springframework.web.client.RestTemplate;\n\nimport java.util.List;\n\n/**\n * 预览接口【订阅号与服务号认证后均可用】\n */\n@Slf4j\n@Component\npublic class PreviewUtil {\n\n @Autowired\n private RestTemplate restTemplate;\n\n @Autowired\n private AccessTokenUtil accessTokenUtil;\n\n private String createPreviewString(List&lt;String&gt; openIdList, String mediaId, String content, String msgtype){\n\n JSONObject data = new JSONObject();\n\n JSONArray touser = new JSONArray();\n touser.addAll(openIdList);\n data.put(\&quot;touser\&quot;,touser);\n\n JSONObject type = new JSONObject();\n if(mediaId != null){\n type.put(\&quot;media_id\&quot;,mediaId);\n }\n\n //图文消息\n if(msgtype.equalsIgnoreCase(\&quot;mpnews\&quot;)){\n data.put(\&quot;mpnews\&quot;,type);\n }else if(msgtype.equalsIgnoreCase(\&quot;text\&quot;)){\n data.put(\&quot;text\&quot;,type); //文本\n type.put(\&quot;content\&quot;,content);\n }else if(msgtype.equalsIgnoreCase(\&quot;voice\&quot;)){\n data.put(\&quot;voice\&quot;,type); //声音\n }else if(msgtype.equalsIgnoreCase(\&quot;image\&quot;)){\n data.put(\&quot;image\&quot;,type); //图片\n }else if(msgtype.equalsIgnoreCase(\&quot;mpvideo\&quot;)){\n data.put(\&quot;mpvideo\&quot;,type); //声音\n }else if(msgtype.equalsIgnoreCase(\&quot;wxcard\&quot;)) {\n data.put(\&quot;wxcard\&quot;, type); //卡券\n }\n\n if(msgtype !=null){\n data.put(\&quot;msgtype\&quot;,msgtype);\n }\n return data.toJSONString();\n\n }\n\n\n /**\n *预览接口\n */\n public String preview(List&lt;String&gt; openIdList, String mediaId, String content, String msgtype) {\n\n String accessToken = accessTokenUtil.getAccessToken();\n if (accessToken != null) {\n log.info(\&quot;URL{}\&quot;, URIConstant.PREVIEW_URL);\n String url = URIConstant.PREVIEW_URL.replace(\&quot;ACCESS_TOKEN\&quot;, accessToken);\n log.info(\&quot;PREVIEW_URL:{}\&quot;, url);\n\n //将菜单对象转换成JSON字符串\n String dataString = this.createPreviewString(openIdList, mediaId, content, msgtype);\n log.info(\&quot;dataString:{}\&quot;,dataString);\n\n //发起POST请求创建菜单\n String jsonObject = restTemplate.postForObject(url, dataString,String.class);\n\n return jsonObject;\n }\n return null;\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.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 PreviewUtil {
@Autowired
private RestTemplate restTemplate;
@Autowired
private AccessTokenUtil accessTokenUtil;
private String createPreviewString(List<span class="hljs-tag"><<span class="hljs-name">String> openIdList, String mediaId, String content, 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);
}else if(msgtype.equalsIgnoreCase("text")){
data.put("text",type); //文本
type.put("content",content);
}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 preview(List<span class="hljs-tag"><<span class="hljs-name">String> openIdList, String mediaId, String content, String msgtype) {
String accessToken = accessTokenUtil.getAccessToken();
if (accessToken != null) {
log.info("URL{}", URIConstant.PREVIEW_URL);
String url = URIConstant.PREVIEW_URL.replace("ACCESS_TOKEN", accessToken);
log.info("PREVIEW_URL:{}", url);
//将菜单对象转换成JSON字符串
String dataString = this.createPreviewString(openIdList, mediaId, content, msgtype);
log.info("dataString:{}",dataString);
//发起POST请求创建菜单
String jsonObject = restTemplate.postForObject(url, dataString,String.class);
return jsonObject;
}
return null;
}
}
</span></span></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>在swagger中添加测试方法</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="5"><img class="has cke_widget_element lazyload" alt="" width="1200" height="700" src="https://img-blog.csdnimg.cn/20200121152539227.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/20200121152539227.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;700&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/20200121152539227.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.PreviewUtil;\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 * 类名称: previewController\n * 类描述: 预览API\n */\n@Slf4j\n@RestController\n@Api(tags = \&quot;预览接口\&quot;)\n@RequestMapping(value = \&quot;/preview\&quot;)\npublic class PreviewController {\n\n @Autowired\n private PreviewUtil previewUtil;\n\n /**\n * 根据标签进行预览\n */\n @ApiOperation(value = \&quot;预览群发接口\&quot;)\n @RequestMapping(value = \&quot;/preview\&quot;, method = RequestMethod.POST)\n @ApiImplicitParams({\n //@ApiImplicitParam(name=\&quot;openId\&quot;,value=\&quot;接收消息用户对应该公众号的openid,该字段也可以改为towxname,以实现对微信号的预览\&quot;, paramType=\&quot;query\&quot;,dataType=\&quot;String\&quot;),\n @ApiImplicitParam(name=\&quot;mediaId\&quot;,value=\&quot;用于群发的消息的media_id\&quot;),\n @ApiImplicitParam(name=\&quot;content\&quot;,value=\&quot;发送文本消息时文本的内容\&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 preview(@RequestParam(value = \&quot;touser\&quot;) List&lt;String&gt; touser, String mediaId, String content, String msgtype) {\n\n String tempString = previewUtil.preview(touser, mediaId,content, 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.PreviewUtil;
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;
/**
* 类名称: previewController
* 类描述: 预览API
*/
@Slf4j
@RestController
@Api(tags = "预览接口")
@RequestMapping(value = "/preview")
public class PreviewController {
@Autowired
private PreviewUtil previewUtil;
/**
* 根据标签进行预览
*/
@ApiOperation(value = "预览群发接口")
@RequestMapping(value = "/preview", method = RequestMethod.POST)
@ApiImplicitParams({
//@ApiImplicitParam(name="openId",value="接收消息用户对应该公众号的openid,该字段也可以改为towxname,以实现对微信号的预览", paramType="query",dataType="String"),
@ApiImplicitParam(name="mediaId",value="用于群发的消息的media_id"),
@ApiImplicitParam(name="content",value="发送文本消息时文本的内容"),
@ApiImplicitParam(name="msgtype",value="群发的消息类型,图文消息为mpnews,文本消息为text,语音为voice,音乐为music,图片为image,视频为video,卡券为wxcard", paramType="query",dataType="Integer")
})
public Object preview(@RequestParam(value = "touser") List<span class="hljs-tag"><<span class="hljs-name">String> touser, String mediaId, String content, String msgtype) {
String tempString = previewUtil.preview(touser, mediaId,content, 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>swagger测试文本消息预览,填写正确的参数</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="1200" height="888" src="https://img-blog.csdnimg.cn/20200121152829291.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/20200121152829291.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;888&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/20200121152829291.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>发送成功,返回了一个msg_id</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="741" src="https://img-blog.csdnimg.cn/20200121153115322.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/20200121153115322.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;741&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/20200121153115322.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="1"><img class="has cke_widget_element lazyload" alt="" width="597" height="869" src="https://img-blog.csdnimg.cn/20200121153212644.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/20200121153212644.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;597&quot;,&quot;height&quot;:&quot;869&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/20200121153212644.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><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="1147" height="574" src="https://img-blog.csdnimg.cn/20200121154528784.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/20200121154528784.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;1147&quot;,&quot;height&quot;:&quot;574&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/20200121154528784.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>
<blockquote>
<h3 id="使用-clientmsgid-参数,避免重复推送">使用 clientmsgid 参数,避免重复推送</h3>
<p>一、群发接口新增 clientmsgid 参数,开发者调用群发接口时可以主动设置 clientmsgid 参数,避免重复推送。</p>
<p>群发时,微信后台将对 24 小时内的群发记录进行检查,如果该 clientmsgid 已经存在一条群发记录,则会拒绝本次群发请求,返回已存在的群发msgid,开发者可以调用“查询群发消息发送状态”接口查看该条群发的状态。</p>
<p>二、新增返回码</p>
<table class=" cke_show_border">
<thead>
<tr><th>返回码</th><th>结果</th></tr>
</thead>
<tbody>
<tr>
<td>45065</td>
<td>相同 clientmsgid 已存在群发记录,返回数据中带有已存在的群发任务的 msgid</td>
</tr>
<tr>
<td>45066</td>
<td>相同 clientmsgid 重试速度过快,请间隔1分钟重试</td>
</tr>
<tr>
<td>45067</td>
<td>clientmsgid 长度超过限制</td>
</tr>
</tbody>
</table>
</blockquote>
<p> 微信公众号还提供了<strong>设置和查询群发速度接口</strong>,更多细节请读者自行仔细阅读微信公众号开发文档更多详细内容,谢谢观看,我们下回再见!</p>
<p>===============================================================================</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/12222174.html
頁:
[1]