Skip to content

Commit

Permalink
feat: 支持已发布的文章
Browse files Browse the repository at this point in the history
- 微信已经不在使用原来的永久素材了
  • Loading branch information
eden2f committed Mar 12, 2022
1 parent 07d0775 commit 98ae77e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>3.9.0</version>
<version>4.2.0</version>
</dependency>

<dependency>
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/anyshare/service/ConfigServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/anyshare/service/WeixinService.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

}
48 changes: 48 additions & 0 deletions src/main/java/com/anyshare/service/WeixinServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<WxMpFreePublishItem> items = wxMpFreePublishList.getItems();
if (CollectionUtils.isNotEmpty(items)) {
for (WxMpFreePublishItem item : items) {
if (item != null && item.getContent() != null && item.getContent().getNewsItem() != null) {
List<WxMpFreePublishArticles> 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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void findByTitle() {

@Test
void findByTitleOrDigestOrContent() {
SearchHits<SearchContentPO> searchHits = searchContentService.findByTitleOrDigestOrContent(AppTag.AnyShare.getCode(), "");
SearchHits<SearchContentPO> searchHits = searchContentService.findByTitleOrDigestOrContent("huiqiqiu", "mybatis");
Assert.isTrue(CollectionUtils.isNotEmpty(searchHits.getSearchHits()), "findByTitleOrDigestOrContent");
}
}

0 comments on commit 98ae77e

Please sign in to comment.