目录
相关文档
业务流程
代码实现
引入依赖
yml参数
微信相关接口调用封装
嵌入业务
公众号获取access_token
https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html
新增永久素材(包括新增其它永久素材和新增永久图文素材等接口)
https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Adding_Permanent_Assets.html
群发接口
https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Batch_Sends_and_Originality_Checks.html
com.arronlong
httpclientutil
1.0.4
wechat:
appid: wx2c4d9cfa5767575f
secret: 9396f2ba7cdab92bad13c5ad69c76f88
# 获取token的url
access_token_url: https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential
# 保存永久图文素材url
save_permanent_image_text_material: https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=
# 保存永久其它素材url
save_permanent_other_material: https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=
# 群发素材到用户url
tag_send_material_url: https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=
# 生成图文素材的阅读原文地址
content_source_url: http://www.gacycu.cn/
package com.snow.management.plug.wechat;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.arronlong.httpclientutil.HttpClientUtil;
import com.arronlong.httpclientutil.common.HttpConfig;
import com.arronlong.httpclientutil.exception.HttpProcessException;
import com.snow.management.util.EhCacheUtil;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.util.*;
/**
* 微信公众号相关
* @ClassName Wechat
* @Author snow
* @Date 2020/10/19 14:38
*/
public class Wechat {
private String appId = "";
private String secret = "";
private String tokenUrl = "";
public Wechat(String appId, String secret, String tokenUrl) {
this.appId = appId;
this.secret = secret;
this.tokenUrl = tokenUrl;
}
/**
* 获取token
*/
public String getAccessToken() throws HttpProcessException {
String result = HttpClientUtil.get(HttpConfig.custom().url(tokenUrl + "&appid=" + appId + "&secret=" + secret));
JSONObject resultObj = JSON.parseObject(result);
if (!resultObj.containsKey("access_token")) {
throw new RuntimeException(result);
}
String token = resultObj.getString("access_token");
EhCacheUtil.getInstance().put(EhCacheUtil.LOGIN_CACHE, "access_token", token);
EhCacheUtil.getInstance().put(EhCacheUtil.LOGIN_CACHE, "access_token_time", System.currentTimeMillis());
return token;
}
/**
* 判断token是否有效
* @return 有效返回token
*/
private String validToken() {
String token = (String) EhCacheUtil.getInstance().get(EhCacheUtil.LOGIN_CACHE, "access_token");
if (token == null) {
return "";
}
long validTime = (long) EhCacheUtil.getInstance().get(EhCacheUtil.LOGIN_CACHE, "access_token_time");
if (validTime + 7000000 >= System.currentTimeMillis()) {
return token;
}
return "";
}
/**
* 新增永久图文素材
* @param imageTextMaterialUrl 新增素材的url
* @param title 标题
* @param thumbMediaId 封面图片的media_id
* @param digest 摘要
* @param showCoverPic 是否显示封面
* @param content 内容
* @param contentSourceUrl 阅读原文地址
* @return 图文素材的media_id
*/
public String savePermanentImageTextMaterial(String imageTextMaterialUrl, String title, String thumbMediaId, String digest
, int showCoverPic, String content, String contentSourceUrl) throws HttpProcessException {
String token = validToken();
if (token.isEmpty()) {
token = getAccessToken();
}
Map article = new LinkedHashMap<>(5);
// 标题
article.put("title", title);
// 图文消息的封面图片素材id(必须是永久mediaID)
article.put("thumb_media_id", thumbMediaId);
// 图文消息的摘要,仅有单图文消息才有摘要,多图文此处为空。如果本字段为没有填写,则默认抓取正文前64个字。
article.put("digest", digest);
// 是否显示封面,0为false,即不显示,1为true,即显示
article.put("show_cover_pic", showCoverPic);
// 图文消息的具体内容,支持HTML标签,必须少于2万字符,小于1M,且此处会去除JS,涉及图片url必须来源 "上传图文消息内的图片获取URL"接口获取。
// 外部图片url将被过滤。
article.put("content", content);
// 图文消息的原文地址,即点击“阅读原文”后的URL
article.put("content_source_url", contentSourceUrl);
List
/**
* 发送文章到公众号
* @param id 文章id
*/
private JSONObject shareWechat(String id) throws HttpProcessException {
if (DataOperateUtil.isNull(appid, secret, tokenUrl, imageTextMaterialUrl, otherMaterialUrl, tagSendMaterialUrl)) {
return fail("缺少参数配置");
}
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.eq("id", id);
queryWrapper.select("id", "title", "introduction", "type", "cover", "content", "media_id");
Article article = articleService.getOne(queryWrapper);
if (article == null) {
return fail("文章不存在");
}
if (article.getType() != 1) {
return fail("仅支持自行编辑的图文文章");
}
if (DataOperateUtil.isNull(article.getCover())) {
return fail("请上传封面用以公众号推送图文信息");
}
Wechat wechat = new Wechat(appid, secret, tokenUrl);
String materialMediaId = "";
// 判断文章是否已经上传到公众号素材库
if (DataOperateUtil.isNull(article.getMediaId())) {
String coverMediaId = wechat.savePermanentOtherMaterial(otherMaterialUrl, "image", fileUploadPath + article.getCover());
materialMediaId = wechat.savePermanentImageTextMaterial(imageTextMaterialUrl, article.getTitle(), coverMediaId, article.getIntroduction(), 1, article.getContent(), contentSourceUrl);
// 保存素材id到库,防止多次上传到公众号素材库
article.setMediaId(materialMediaId);
articleService.updateById(article);
} else {
materialMediaId = article.getMediaId();
}
String sendResult = wechat.sendImageTextMessage(tagSendMaterialUrl, materialMediaId);
if (sendResult.isEmpty()) {
return successNotData();
}
return fail(sendResult);
}