WxMediaController.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package com.ifast.wxmp.controller.mp;
  2. import com.ifast.common.base.AdminBaseController;
  3. import com.ifast.common.config.IFastProperties;
  4. import com.ifast.common.exception.IFastException;
  5. import com.ifast.common.type.EnumErrorCode;
  6. import com.ifast.common.utils.JSONUtils;
  7. import com.ifast.common.utils.Result;
  8. import com.ifast.wxmp.domain.MpArticleDO;
  9. import com.ifast.wxmp.service.MpArticleService;
  10. import com.ifast.wxmp.service.MpFansService;
  11. import com.ifast.wxmp.service.WeixinService;
  12. import lombok.extern.slf4j.Slf4j;
  13. import me.chanjar.weixin.common.api.WxConsts;
  14. import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
  15. import me.chanjar.weixin.common.error.WxErrorException;
  16. import me.chanjar.weixin.mp.bean.WxMpMassNews;
  17. import me.chanjar.weixin.mp.bean.WxMpMassOpenIdsMessage;
  18. import me.chanjar.weixin.mp.bean.material.WxMpMaterial;
  19. import me.chanjar.weixin.mp.bean.material.WxMpMaterialUploadResult;
  20. import me.chanjar.weixin.mp.bean.result.WxMpMassUploadResult;
  21. import org.apache.commons.io.FileUtils;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.web.bind.annotation.PathVariable;
  24. import org.springframework.web.bind.annotation.RequestMapping;
  25. import org.springframework.web.bind.annotation.RestController;
  26. import java.io.File;
  27. import java.io.IOException;
  28. import java.net.URL;
  29. /**
  30. * <pre>
  31. * </pre>
  32. *
  33. * @Author Aron
  34. * @Date 2018/5/3
  35. */
  36. @RestController
  37. @RequestMapping("/wx/mp/api/media")
  38. @Slf4j
  39. public class WxMediaController extends AdminBaseController {
  40. @Autowired
  41. private WeixinService wxService;
  42. @Autowired
  43. private MpArticleService mpArticleService;
  44. @Autowired
  45. private MpFansService mpFansService;
  46. @Autowired
  47. private IFastProperties properties;
  48. ///////////////
  49. // 图片
  50. ///////////////
  51. @RequestMapping("/image/sync/{articleId}")
  52. public Result imageSync(String appId, @PathVariable Long articleId) {
  53. MpArticleDO article = mpArticleService.selectById(articleId);
  54. log.info("素材同步");
  55. WxMpMaterialUploadResult wxMpMaterialUploadResult = null;
  56. try {
  57. File file = new File(properties.getTempDir() + System.currentTimeMillis() + ".jpg");
  58. FileUtils.copyURLToFile(new URL(article.getImgurl()), file);
  59. WxMpMaterial material = new WxMpMaterial();
  60. material.setName(file.getName());
  61. material.setFile(file);
  62. wxMpMaterialUploadResult = wxService.init().getMaterialService().materialFileUpload(WxConsts.MediaFileType.IMAGE, material);
  63. } catch (WxErrorException | IOException e) {
  64. e.printStackTrace();
  65. throw new IFastException(EnumErrorCode.wxmpMediaImageSyncError.getCodeStr());
  66. }
  67. log.debug(JSONUtils.beanToJson(wxMpMaterialUploadResult));
  68. article.setTid(wxMpMaterialUploadResult.getMediaId());
  69. mpArticleService.updateById(article);
  70. return Result.ok();
  71. }
  72. @RequestMapping("/image/groupsend/{articleId}")
  73. public Result imageGroupsend(String appId, @PathVariable Long articleId) {
  74. MpArticleDO article = mpArticleService.selectById(articleId);
  75. WxMpMassOpenIdsMessage message = new WxMpMassOpenIdsMessage();
  76. // TODO 群发对象
  77. message.addUser(mpFansService.selectById(1).getOpenid());
  78. message.addUser(mpFansService.selectById(1).getOpenid());
  79. message.setMsgType(article.getMsgtype());
  80. message.setMediaId(article.getTid());
  81. try {
  82. wxService.init().getMassMessageService().massOpenIdsMessageSend(message);
  83. } catch (WxErrorException e) {
  84. e.printStackTrace();
  85. throw new IFastException(EnumErrorCode.wxmpMediaGroupSendSyncError.getCodeStr());
  86. }
  87. return Result.ok();
  88. }
  89. ///////////////
  90. // 图文
  91. ///////////////
  92. @RequestMapping("/news/sync/{articleId}")
  93. public Result newsSync(String appId, @PathVariable Long articleId) throws IOException, WxErrorException {
  94. MpArticleDO article = mpArticleService.selectById(articleId);
  95. WeixinService weixinService = wxService.init();
  96. // 封面上传
  97. File file = new File(properties.getTempDir() + System.currentTimeMillis() + ".jpg");
  98. FileUtils.copyURLToFile(new URL(article.getImgurl()), file);
  99. WxMpMaterial material = new WxMpMaterial();
  100. material.setFile(file);
  101. material.setName(file.getName());
  102. WxMediaUploadResult wxMediaImgUploadResult = weixinService.getMaterialService().mediaUpload(WxConsts.MediaFileType.IMAGE, file);
  103. String mediaId = wxMediaImgUploadResult.getMediaId();
  104. log.info("素材同步");
  105. WxMpMassUploadResult wxMediaUploadResult;
  106. WxMpMassNews news = new WxMpMassNews();
  107. WxMpMassNews.WxMpMassNewsArticle art = new WxMpMassNews.WxMpMassNewsArticle();
  108. art.setTitle(article.getTitle());
  109. art.setContent(article.getContent());
  110. art.setDigest(article.getIntroduct());
  111. // TODO 作者
  112. art.setAuthor("Aron");
  113. // 原文链接
  114. art.setContentSourceUrl(article.getUrl());
  115. // 图片封面
  116. art.setThumbMediaId(mediaId);
  117. // TODO 显示封面
  118. art.setShowCoverPic(true);
  119. news.addArticle(art);
  120. try {
  121. wxMediaUploadResult = weixinService.getMassMessageService().massNewsUpload(news);
  122. } catch (WxErrorException e) {
  123. e.printStackTrace();
  124. throw new IFastException(EnumErrorCode.wxmpMediaImageSyncError.getCodeStr());
  125. }
  126. log.debug(JSONUtils.beanToJson(wxMediaUploadResult));
  127. article.setTid(wxMediaUploadResult.getMediaId());
  128. article.setThumbid(mediaId);
  129. mpArticleService.updateById(article);
  130. return Result.ok();
  131. }
  132. @RequestMapping("/news/groupsend/{articleId}")
  133. public Result newsGroupsend(String appId, @PathVariable Long articleId) {
  134. MpArticleDO article = mpArticleService.selectById(articleId);
  135. WxMpMassOpenIdsMessage message = new WxMpMassOpenIdsMessage();
  136. // TODO 群发对象
  137. message.addUser(mpFansService.selectById(1L).getOpenid());
  138. message.addUser(mpFansService.selectById(1L).getOpenid());
  139. message.setMsgType(convertMsgtype(article.getMsgtype()));
  140. message.setMediaId(article.getTid());
  141. try {
  142. wxService.init().getMassMessageService().massOpenIdsMessageSend(message);
  143. } catch (WxErrorException e) {
  144. e.printStackTrace();
  145. throw new IFastException(EnumErrorCode.wxmpMediaGroupSendSyncError.getCodeStr());
  146. }
  147. return Result.ok();
  148. }
  149. private String convertMsgtype(String msgType) {
  150. if (WxConsts.MaterialType.NEWS.equals(msgType)) {
  151. return WxConsts.MassMsgType.MPNEWS;
  152. }
  153. return msgType;
  154. }
  155. }