From 98ae77e66ed06563cfa327b64128fe5ef5298d4b Mon Sep 17 00:00:00 2001 From: Eden Date: Sat, 12 Mar 2022 23:32:06 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E5=B7=B2=E5=8F=91?= =?UTF-8?q?=E5=B8=83=E7=9A=84=E6=96=87=E7=AB=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 微信已经不在使用原来的永久素材了 --- pom.xml | 2 +- .../anyshare/service/ConfigServiceImpl.java | 12 +++++ .../com/anyshare/service/WeixinService.java | 4 ++ .../anyshare/service/WeixinServiceImpl.java | 48 +++++++++++++++++++ .../service/SearchContentServiceImplTest.java | 2 +- 5 files changed, 66 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 576b697..f01c15b 100644 --- a/pom.xml +++ b/pom.xml @@ -80,7 +80,7 @@ com.github.binarywang weixin-java-mp - 3.9.0 + 4.2.0 diff --git a/src/main/java/com/anyshare/service/ConfigServiceImpl.java b/src/main/java/com/anyshare/service/ConfigServiceImpl.java index eedda26..f466b5d 100644 --- a/src/main/java/com/anyshare/service/ConfigServiceImpl.java +++ b/src/main/java/com/anyshare/service/ConfigServiceImpl.java @@ -93,13 +93,25 @@ public void reindexSearchContent(OpenapiReindexSearchContentReq req) { } private WxMpService materialNewsSynchronizer(String appTag, WxMpConfigStorage wxMpConfigStorage) { + boolean fail = false; WxMpService wxMpService = new WxMpServiceImpl(); wxMpService.setWxMpConfigStorage(wxMpConfigStorage); + wxMpService.getDraftService(); + wxMpService.getFreePublishService(); WxMpMaterialService wxMpMaterialService = wxMpService.getMaterialService(); try { weixinService.materialNewsSynchronizer(appTag, wxMpMaterialService); } catch (WxErrorException we) { log.info(String.format("同步微信物料发生异常, %s", appTag), we); + fail = true; + } + try { + weixinService.freePublishSynchronizer(appTag, wxMpService.getFreePublishService()); + } catch (WxErrorException we) { + log.info(String.format("同步微信物料发生异常, %s", appTag), we); + fail = true; + } + if (fail) { throw new ServiceException("请检查下微信公众配置有误,请稍后重试!"); } return wxMpService; diff --git a/src/main/java/com/anyshare/service/WeixinService.java b/src/main/java/com/anyshare/service/WeixinService.java index 087b865..e37185d 100644 --- a/src/main/java/com/anyshare/service/WeixinService.java +++ b/src/main/java/com/anyshare/service/WeixinService.java @@ -1,6 +1,7 @@ package com.anyshare.service; import me.chanjar.weixin.common.error.WxErrorException; +import me.chanjar.weixin.mp.api.WxMpFreePublishService; import me.chanjar.weixin.mp.api.WxMpMaterialService; import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage; import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage; @@ -16,4 +17,7 @@ public interface WeixinService { void materialNewsSynchronizer(String appTag, WxMpMaterialService wxMpMaterialService) throws WxErrorException; void reindexEsContent(String appTag); + + void freePublishSynchronizer(String appTag, WxMpFreePublishService freePublishService) throws WxErrorException; + } diff --git a/src/main/java/com/anyshare/service/WeixinServiceImpl.java b/src/main/java/com/anyshare/service/WeixinServiceImpl.java index 4370579..df963da 100644 --- a/src/main/java/com/anyshare/service/WeixinServiceImpl.java +++ b/src/main/java/com/anyshare/service/WeixinServiceImpl.java @@ -11,7 +11,11 @@ import com.anyshare.service.eventdriven.event.ResourceUpdateEvent; import lombok.extern.slf4j.Slf4j; import me.chanjar.weixin.common.error.WxErrorException; +import me.chanjar.weixin.mp.api.WxMpFreePublishService; import me.chanjar.weixin.mp.api.WxMpMaterialService; +import me.chanjar.weixin.mp.bean.freepublish.WxMpFreePublishArticles; +import me.chanjar.weixin.mp.bean.freepublish.WxMpFreePublishItem; +import me.chanjar.weixin.mp.bean.freepublish.WxMpFreePublishList; import me.chanjar.weixin.mp.bean.material.WxMpMaterialCountResult; import me.chanjar.weixin.mp.bean.material.WxMpMaterialNewsBatchGetResult; import me.chanjar.weixin.mp.bean.material.WxMpNewsArticle; @@ -231,6 +235,50 @@ public void materialNewsSynchronizer(String appTag, WxMpMaterialService wxMpMate } } + /** + * 拉取永久素材中的图文 + */ + @Override + public void freePublishSynchronizer(String appTag, WxMpFreePublishService freePublishService) throws WxErrorException { + int startIndex = 0; + int size = 10; + Integer totalCount; + Integer itemCount; + do { + WxMpFreePublishList wxMpFreePublishList = freePublishService.getPublicationRecords(startIndex, size); + totalCount = wxMpFreePublishList.getTotalCount(); + itemCount = wxMpFreePublishList.getItemCount(); + log.info("appTag = {}, totalCount = {}, itemCount = {}", appTag, totalCount, itemCount); + List items = wxMpFreePublishList.getItems(); + if (CollectionUtils.isNotEmpty(items)) { + for (WxMpFreePublishItem item : items) { + if (item != null && item.getContent() != null && item.getContent().getNewsItem() != null) { + List freePublishArticles = item.getContent().getNewsItem(); + for (WxMpFreePublishArticles freePublishArticle : freePublishArticles) { + WxMpNewsArticlePO wxMpNewsArticle = WxMpNewsArticlePO.createDefault(WxMpNewsArticlePO.class); + wxMpNewsArticle.setUrl(freePublishArticle.getUrl()); + wxMpNewsArticle.setThumbMediaId(freePublishArticle.getThumbMediaId()); + wxMpNewsArticle.setAuthor(freePublishArticle.getAuthor()); + wxMpNewsArticle.setTitle(freePublishArticle.getTitle()); + wxMpNewsArticle.setContentSourceUrl(freePublishArticle.getContentSourceUrl()); + wxMpNewsArticle.setContent(freePublishArticle.getContent()); + wxMpNewsArticle.setDigest(freePublishArticle.getDigest()); + Integer showCoverPic = freePublishArticle.getShowCoverPic(); + wxMpNewsArticle.setShowCoverPic(showCoverPic != null && showCoverPic == 1); + wxMpNewsArticle.setUrl(freePublishArticle.getUrl()); + Integer needOpenComment = freePublishArticle.getNeedOpenComment(); + wxMpNewsArticle.setNeedOpenComment(needOpenComment != null && needOpenComment == 1); + Integer onlyFansCanComment = freePublishArticle.getOnlyFansCanComment(); + wxMpNewsArticle.setOnlyFansCanComment(onlyFansCanComment != null && onlyFansCanComment == 1); + wxMpNewsArticle.setAppTag(appTag); + wxMpNewsArticleService.insert(appTag, wxMpNewsArticle); + } + } + } + } + } while (startIndex > (totalCount + size)); + } + @Override public void reindexEsContent(String appTag) { if (reindexEsContentLock.tryLock()) { diff --git a/src/test/java/com/anyshare/service/SearchContentServiceImplTest.java b/src/test/java/com/anyshare/service/SearchContentServiceImplTest.java index 540cc80..7b4359d 100644 --- a/src/test/java/com/anyshare/service/SearchContentServiceImplTest.java +++ b/src/test/java/com/anyshare/service/SearchContentServiceImplTest.java @@ -102,7 +102,7 @@ void findByTitle() { @Test void findByTitleOrDigestOrContent() { - SearchHits searchHits = searchContentService.findByTitleOrDigestOrContent(AppTag.AnyShare.getCode(), "的"); + SearchHits searchHits = searchContentService.findByTitleOrDigestOrContent("huiqiqiu", "mybatis"); Assert.isTrue(CollectionUtils.isNotEmpty(searchHits.getSearchHits()), "findByTitleOrDigestOrContent"); } } \ No newline at end of file