Skip to content

Commit 98ae77e

Browse files
committed
feat: 支持已发布的文章
- 微信已经不在使用原来的永久素材了
1 parent 07d0775 commit 98ae77e

File tree

5 files changed

+66
-2
lines changed

5 files changed

+66
-2
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
<dependency>
8181
<groupId>com.github.binarywang</groupId>
8282
<artifactId>weixin-java-mp</artifactId>
83-
<version>3.9.0</version>
83+
<version>4.2.0</version>
8484
</dependency>
8585

8686
<dependency>

src/main/java/com/anyshare/service/ConfigServiceImpl.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,25 @@ public void reindexSearchContent(OpenapiReindexSearchContentReq req) {
9393
}
9494

9595
private WxMpService materialNewsSynchronizer(String appTag, WxMpConfigStorage wxMpConfigStorage) {
96+
boolean fail = false;
9697
WxMpService wxMpService = new WxMpServiceImpl();
9798
wxMpService.setWxMpConfigStorage(wxMpConfigStorage);
99+
wxMpService.getDraftService();
100+
wxMpService.getFreePublishService();
98101
WxMpMaterialService wxMpMaterialService = wxMpService.getMaterialService();
99102
try {
100103
weixinService.materialNewsSynchronizer(appTag, wxMpMaterialService);
101104
} catch (WxErrorException we) {
102105
log.info(String.format("同步微信物料发生异常, %s", appTag), we);
106+
fail = true;
107+
}
108+
try {
109+
weixinService.freePublishSynchronizer(appTag, wxMpService.getFreePublishService());
110+
} catch (WxErrorException we) {
111+
log.info(String.format("同步微信物料发生异常, %s", appTag), we);
112+
fail = true;
113+
}
114+
if (fail) {
103115
throw new ServiceException("请检查下微信公众配置有误,请稍后重试!");
104116
}
105117
return wxMpService;

src/main/java/com/anyshare/service/WeixinService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.anyshare.service;
22

33
import me.chanjar.weixin.common.error.WxErrorException;
4+
import me.chanjar.weixin.mp.api.WxMpFreePublishService;
45
import me.chanjar.weixin.mp.api.WxMpMaterialService;
56
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
67
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
@@ -16,4 +17,7 @@ public interface WeixinService {
1617
void materialNewsSynchronizer(String appTag, WxMpMaterialService wxMpMaterialService) throws WxErrorException;
1718

1819
void reindexEsContent(String appTag);
20+
21+
void freePublishSynchronizer(String appTag, WxMpFreePublishService freePublishService) throws WxErrorException;
22+
1923
}

src/main/java/com/anyshare/service/WeixinServiceImpl.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
import com.anyshare.service.eventdriven.event.ResourceUpdateEvent;
1212
import lombok.extern.slf4j.Slf4j;
1313
import me.chanjar.weixin.common.error.WxErrorException;
14+
import me.chanjar.weixin.mp.api.WxMpFreePublishService;
1415
import me.chanjar.weixin.mp.api.WxMpMaterialService;
16+
import me.chanjar.weixin.mp.bean.freepublish.WxMpFreePublishArticles;
17+
import me.chanjar.weixin.mp.bean.freepublish.WxMpFreePublishItem;
18+
import me.chanjar.weixin.mp.bean.freepublish.WxMpFreePublishList;
1519
import me.chanjar.weixin.mp.bean.material.WxMpMaterialCountResult;
1620
import me.chanjar.weixin.mp.bean.material.WxMpMaterialNewsBatchGetResult;
1721
import me.chanjar.weixin.mp.bean.material.WxMpNewsArticle;
@@ -231,6 +235,50 @@ public void materialNewsSynchronizer(String appTag, WxMpMaterialService wxMpMate
231235
}
232236
}
233237

238+
/**
239+
* 拉取永久素材中的图文
240+
*/
241+
@Override
242+
public void freePublishSynchronizer(String appTag, WxMpFreePublishService freePublishService) throws WxErrorException {
243+
int startIndex = 0;
244+
int size = 10;
245+
Integer totalCount;
246+
Integer itemCount;
247+
do {
248+
WxMpFreePublishList wxMpFreePublishList = freePublishService.getPublicationRecords(startIndex, size);
249+
totalCount = wxMpFreePublishList.getTotalCount();
250+
itemCount = wxMpFreePublishList.getItemCount();
251+
log.info("appTag = {}, totalCount = {}, itemCount = {}", appTag, totalCount, itemCount);
252+
List<WxMpFreePublishItem> items = wxMpFreePublishList.getItems();
253+
if (CollectionUtils.isNotEmpty(items)) {
254+
for (WxMpFreePublishItem item : items) {
255+
if (item != null && item.getContent() != null && item.getContent().getNewsItem() != null) {
256+
List<WxMpFreePublishArticles> freePublishArticles = item.getContent().getNewsItem();
257+
for (WxMpFreePublishArticles freePublishArticle : freePublishArticles) {
258+
WxMpNewsArticlePO wxMpNewsArticle = WxMpNewsArticlePO.createDefault(WxMpNewsArticlePO.class);
259+
wxMpNewsArticle.setUrl(freePublishArticle.getUrl());
260+
wxMpNewsArticle.setThumbMediaId(freePublishArticle.getThumbMediaId());
261+
wxMpNewsArticle.setAuthor(freePublishArticle.getAuthor());
262+
wxMpNewsArticle.setTitle(freePublishArticle.getTitle());
263+
wxMpNewsArticle.setContentSourceUrl(freePublishArticle.getContentSourceUrl());
264+
wxMpNewsArticle.setContent(freePublishArticle.getContent());
265+
wxMpNewsArticle.setDigest(freePublishArticle.getDigest());
266+
Integer showCoverPic = freePublishArticle.getShowCoverPic();
267+
wxMpNewsArticle.setShowCoverPic(showCoverPic != null && showCoverPic == 1);
268+
wxMpNewsArticle.setUrl(freePublishArticle.getUrl());
269+
Integer needOpenComment = freePublishArticle.getNeedOpenComment();
270+
wxMpNewsArticle.setNeedOpenComment(needOpenComment != null && needOpenComment == 1);
271+
Integer onlyFansCanComment = freePublishArticle.getOnlyFansCanComment();
272+
wxMpNewsArticle.setOnlyFansCanComment(onlyFansCanComment != null && onlyFansCanComment == 1);
273+
wxMpNewsArticle.setAppTag(appTag);
274+
wxMpNewsArticleService.insert(appTag, wxMpNewsArticle);
275+
}
276+
}
277+
}
278+
}
279+
} while (startIndex > (totalCount + size));
280+
}
281+
234282
@Override
235283
public void reindexEsContent(String appTag) {
236284
if (reindexEsContentLock.tryLock()) {

src/test/java/com/anyshare/service/SearchContentServiceImplTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ void findByTitle() {
102102

103103
@Test
104104
void findByTitleOrDigestOrContent() {
105-
SearchHits<SearchContentPO> searchHits = searchContentService.findByTitleOrDigestOrContent(AppTag.AnyShare.getCode(), "");
105+
SearchHits<SearchContentPO> searchHits = searchContentService.findByTitleOrDigestOrContent("huiqiqiu", "mybatis");
106106
Assert.isTrue(CollectionUtils.isNotEmpty(searchHits.getSearchHits()), "findByTitleOrDigestOrContent");
107107
}
108108
}

0 commit comments

Comments
 (0)