demand
1. When publishing articles on the local web page, you need to push articles to the corresponding service number.
2. Pictures can be uploaded to the wechat server in the rich text editor to obtain temporary files for sending. Can also be copied from the web image links directly click push also need to be pushed to WeChat service, this article is the picture of the local rich text, according to all resources to the local server first to WeChat push to image links within the article when WeChat server to obtain temporary files downloaded to a local media_id sent out again.
code
1. Service number article push interface implementation class
@Service
public class WxArticleSendTestServiceImpl {
private Logger logger = LoggerFactory.getLogger(WxArticleSendTestServiceImpl.class);
@Autowired
private RestTemplate restTemplate;
/***
* @Description: 服务号文章推送主方法,需改成面向接口编程
* @Param: [mesDataDT]
* @return: cn.thinkjoy.springboot.wxcenter.dto.wechat.MassSendResultDTO
* @Author: leichunhong
* @Date: 2021-03-05
*/
public MassSendResultDTO sendMsg(MesDataDTO mesDataDT) throws WxErrorException {
//获取appid 可以直接当参数传递
String appId =mesDataDT.getAppId();
//获取首页图片MediaId和替换文章内url
ArticlesDataDTO articlesDataDTO = newCentent(appId, mesDataDT.getArticlesData());
//上传图文消息素材
MassUploadResultDTO massUploadResultDTO = uploadNews(appId, articlesDataDTO);
String mediaId = massUploadResultDTO.getMedia_id();
//消息协议体
MassTagMessageDTO massTagMessageDTO = massTagInfo(mediaId);
//发送消息
MassSendResultDTO massSendResultDTO = sendAll(appId, massTagMessageDTO);
return massSendResultDTO;
}
/***
* @Description: 上传资源获取临时文件
* @Param: [appId, file]
* @return: me.chanjar.weixin.common.bean.result.WxMediaUploadResult
* @Author: leichunhong
* @Date: 2021-03-05
*/
public WxMediaUploadResult mediaUpload(String appId, File file) throws WxErrorException {
//封面图片
WxMediaUploadResult wxMediaUploadResult = WxMpConfiguration.getMpServices().get(appId).getMaterialService().mediaUpload(WxConsts.MassMsgType.IMAGE, file);
String wxMediaUploadResultstr = JSONObject.toJSONString(wxMediaUploadResult);
logger.info("首页图片返回mediaId:" + wxMediaUploadResultstr);
return wxMediaUploadResult;
}
/***
* @Description: 返回微信临时文件消息体的图片内容的url
* @Param: [appId, file]
* @return: me.chanjar.weixin.mp.bean.material.WxMediaImgUploadResult
* @Author: leichunhong
* @Date: 2021-03-05
*/
public WxMediaImgUploadResult mediaImgUpload(String appId, File file) throws WxErrorException {
//图文内容返回的url
WxMediaImgUploadResult wxMediaImgUploadResult = WxMpConfiguration.getMpServices().get(appId).getMaterialService().mediaImgUpload(file);
String wxMediaImgUploadResultstr = JSONObject.toJSONString(wxMediaImgUploadResult);
logger.info("文章内图片图片返回参数:" + wxMediaImgUploadResultstr);
return wxMediaImgUploadResult;
}
/***
* @Description: 上传临时素材
* @Param: [appId, articlesDataDTO]
* @return: cn.thinkjoy.springboot.wxcenter.dto.wechat.MassUploadResultDTO
* @Author: leichunhong
* @Date: 2021-03-05
*/
public MassUploadResultDTO uploadNews(String appId, ArticlesDataDTO articlesDataDTO) throws WxErrorException {
//上传图文消息素材
String uploadResult = WxMpConfiguration.getMpServices().get(appId).post("https://api.weixin.qq.com/cgi-bin/media/uploadnews", JSONObject.toJSONString(articlesDataDTO));
logger.info("图文素材:" + uploadResult);
return JSONObject.parseObject(uploadResult, MassUploadResultDTO.class);
}
public MassSendResultDTO sendAll(String appId, MassTagMessageDTO massTagMessageDTO) throws WxErrorException {
String responseContent = WxMpConfiguration.getMpServices().get(appId).post("https://api.weixin.qq.com/cgi-bin/message/mass/sendall", JSONObject.toJSONString(massTagMessageDTO));
logger.info("发送消息:" + responseContent);
return JSONObject.parseObject(responseContent, MassSendResultDTO.class);
}
/***
* @Description: 发送内
* @Param: [mediaId]
* @return: cn.thinkjoy.springboot.wxcenter.dto.wechat.MassTagMessageDTO
* @Author: leichunhong
* @Date: 2021-03-05
*/
public MassTagMessageDTO massTagInfo(String mediaId) {
//最终发消息实体
MassTagMessageDTO massTagMessageDTO = new MassTagMessageDTO();
FilterDTO filterDTO = new FilterDTO();
MpnewsDTO mpnewsDTO = new MpnewsDTO();
filterDTO.setIs_to_all(true);
mpnewsDTO.setMedia_id(mediaId);
massTagMessageDTO.setFilter(filterDTO);
massTagMessageDTO.setMpnews(mpnewsDTO);
massTagMessageDTO.setMsgtype("mpnews");
//转载的不能群发有广告
massTagMessageDTO.setSend_ignore_reprint(0);
return massTagMessageDTO;
}
/***
* @Description: 替换文章的链接
* @Param: [appId, articlesDataDTO]
* @return: cn.thinkjoy.springboot.wxcenter.dto.wechat.ArticlesDataDTO
* @Author: leichunhong
* @Date: 2021-03-05
*/
public ArticlesDataDTO newCentent(String appId, ArticlesDataDTO articlesDataDTO) throws WxErrorException {
WxMediaUploadResult wxMediaUploadResult = null;
List<ArticlesDTO> articlesDTOList = articlesDataDTO.getArticles();
if (articlesDTOList == null || articlesDTOList.size() == 0) {
logger.info("发送协议为空");
return null;
}
//文章内容
String content = "";
//文章内图片url集合
String[] imgurl = null;
//封面图片url
String coverImg = "";
for (ArticlesDTO articlesDTO : articlesDTOList) {
//获取首页图片MediaId
coverImg = articlesDTO.getImage();
//下载图片
File coverFile = downLoad(coverImg, appId);
//上传获取临时文件
wxMediaUploadResult = mediaUpload(appId, coverFile);
articlesDTO.setThumb_media_id(wxMediaUploadResult.getMediaId());
coverFile.delete(); //删除首页图片文件
//1.替换文章内img图片url
content = articlesDTO.getContent();
imgurl = StringUtils.substringsBetween(content, "src=\"", "\"");
content = getContent(imgurl, appId, content);
//2.替换背景图片不带双引号
imgurl = StringUtils.substringsBetween(content, "url(", ")");
content = getContent(imgurl, appId, content);
//3.替换背景图片带双引号
imgurl = StringUtils.substringsBetween(content, "url(\"", "\")");
//获取新的内容
content = getContent(imgurl, appId, content);
logger.info("替换的内容:" + content);
articlesDTO.setContent(content);
}
articlesDataDTO.setArticles(articlesDTOList);
return articlesDataDTO;
}
/***
* @Description: 下载资源
* @Param: [imgUrl, appId]
* @return: java.io.File
* @Author: leichunhong
* @Date: 2021-03-05
*/
public File downLoad(String imgUrl, String appId) {
ResponseEntity<byte[]> responseEntity = restTemplate.getForEntity(imgUrl, byte[].class);
File file = null;
try {
//本地临时文件存放图片
file = File.createTempFile(appId, "." + responseEntity.getHeaders().getContentType().getSubtype());
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
fos.write(responseEntity.getBody());
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
return file;
}
/***
* @Description: 替换新的内容
* @Param: [imgurl, appId, content]
* @return: java.lang.String
* @Author: leichunhong
* @Date: 2021-03-05
*/
public String getContent(String[] imgurl, String appId, String content) throws WxErrorException {
WxMediaImgUploadResult wxMediaImgUploadResult = null;
if (imgurl != null && imgurl.length > 0) {
for (String imgurlDTO : imgurl) {
File file = downLoad(imgurlDTO, appId);
wxMediaImgUploadResult = mediaImgUpload(appId, file);
String newImgUrl = wxMediaImgUploadResult.getUrl();
content = content.replaceAll(imgurlDTO, newImgUrl);
file.delete();
}
}
return content;
}
Copy the code
MesDataDTO is the body of the push article message, and the call interface directly calls the sendMsg() method to implement the push.
Wechat message entity
public class MesDataDTO { private String appId; private ArticlesDataDTO ArticlesData; public ArticlesDataDTO getArticlesData() { return ArticlesData; } public void setArticlesData(ArticlesDataDTO articlesData) { ArticlesData = articlesData; } public String getAppId() { return appId; } public void setAppId(String appId) { this.appId = appId; }}Copy the code
Multiple messages encapsulate entities
public class ArticlesDataDTO { private List<ArticlesDTO> articles; public List<ArticlesDTO> getArticles() { return articles; } public void setArticles(List<ArticlesDTO> articles) { this.articles = articles; }}Copy the code
The body of the sent message is consistent with the wechat developer document
public class ArticlesDTO { /** * thumb_media_id : qI6_Ze_6PtV7svjolgs-rN6stStuHIjs9_DidOHaj0Q-mwvBelOXCFZiq2OsIU-p * author : xxx * title : Happy Day * content_source_url : www.qq.com * content : content * digest : digest * show_cover_pic : 1 * need_open_comment : 1 * only_fans_can_comment : 1 */ private String thumb_media_id; private String author; private String title; private String content_source_url; private String content; private String digest; private Integer show_cover_pic; private Integer need_open_comment; private Integer only_fans_can_comment; private String image; public String getImage() { return image; } public void setImage(String image) { this.image = image; } public String getThumb_media_id() { return thumb_media_id; } public void setThumb_media_id(String thumb_media_id) { this.thumb_media_id = thumb_media_id; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent_source_url() { return content_source_url; } public void setContent_source_url(String content_source_url) { this.content_source_url = content_source_url; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getDigest() { return digest; } public void setDigest(String digest) { this.digest = digest; } public Integer getShow_cover_pic() { return show_cover_pic; } public void setShow_cover_pic(Integer show_cover_pic) { this.show_cover_pic = show_cover_pic; } public Integer getNeed_open_comment() { return need_open_comment; } public void setNeed_open_comment(Integer need_open_comment) { this.need_open_comment = need_open_comment; } public Integer getOnly_fans_can_comment() { return only_fans_can_comment; } public void setOnly_fans_can_comment(Integer only_fans_can_comment) { this.only_fans_can_comment = only_fans_can_comment; }}Copy the code
The results show
Local editor
Wechat service number