diff --git a/yudao-module-mp/yudao-module-mp-api/src/main/java/cn/iocoder/yudao/module/mp/enums/ErrorCodeConstants.java b/yudao-module-mp/yudao-module-mp-api/src/main/java/cn/iocoder/yudao/module/mp/enums/ErrorCodeConstants.java index c1ee93128..d1c5b8da3 100644 --- a/yudao-module-mp/yudao-module-mp-api/src/main/java/cn/iocoder/yudao/module/mp/enums/ErrorCodeConstants.java +++ b/yudao-module-mp/yudao-module-mp-api/src/main/java/cn/iocoder/yudao/module/mp/enums/ErrorCodeConstants.java @@ -40,6 +40,7 @@ public interface ErrorCodeConstants { // ========== 公众号发布能力 1006006000============ ErrorCode FREE_PUBLISH_LIST_FAIL = new ErrorCode(1006006000, "获得已成功发布列表失败,原因:{}"); ErrorCode FREE_PUBLISH_SUBMIT_FAIL = new ErrorCode(1006006001, "提交发布失败,原因:{}"); + ErrorCode FREE_PUBLISH_DELETE_FAIL = new ErrorCode(1006006001, "删除发布失败,原因:{}"); // ========== 公众号草稿 1006007000============ ErrorCode DRAFT_LIST_FAIL = new ErrorCode(1006007000, "获得草稿列表失败,原因:{}"); diff --git a/yudao-module-mp/yudao-module-mp-biz/src/main/java/cn/iocoder/yudao/module/mp/controller/admin/news/MpFreePublishController.java b/yudao-module-mp/yudao-module-mp-biz/src/main/java/cn/iocoder/yudao/module/mp/controller/admin/news/MpFreePublishController.java index 10b7ad3c9..6938e3274 100644 --- a/yudao-module-mp/yudao-module-mp-biz/src/main/java/cn/iocoder/yudao/module/mp/controller/admin/news/MpFreePublishController.java +++ b/yudao-module-mp/yudao-module-mp-biz/src/main/java/cn/iocoder/yudao/module/mp/controller/admin/news/MpFreePublishController.java @@ -1,10 +1,14 @@ package cn.iocoder.yudao.module.mp.controller.admin.news; +import cn.hutool.core.collection.CollUtil; import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; import cn.iocoder.yudao.framework.common.util.object.PageUtils; import cn.iocoder.yudao.module.mp.controller.admin.news.vo.MpFreePublishPageReqVO; +import cn.iocoder.yudao.module.mp.dal.dataobject.material.MpMaterialDO; import cn.iocoder.yudao.module.mp.framework.mp.core.MpServiceFactory; +import cn.iocoder.yudao.module.mp.service.material.MpMaterialService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; @@ -13,16 +17,21 @@ import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.bean.freepublish.WxMpFreePublishItem; import me.chanjar.weixin.mp.bean.freepublish.WxMpFreePublishList; +import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; +import static cn.iocoder.yudao.framework.common.util.collection.MapUtils.findAndThen; import static cn.iocoder.yudao.module.mp.enums.ErrorCodeConstants.*; -// TODO 芋艿:权限 @Api(tags = "管理后台 - 公众号发布能力") @RestController @RequestMapping("/mp/free-publish") @@ -32,8 +41,12 @@ public class MpFreePublishController { @Resource private MpServiceFactory mpServiceFactory; + @Resource + private MpMaterialService mpMaterialService; + @GetMapping("/page") @ApiOperation("获得已发布的图文分页") + @PreAuthorize("@ss.hasPermission('mp:free-publish:query')") public CommonResult> getFreePublishPage(MpFreePublishPageReqVO reqVO) { // 从公众号查询已发布的图文列表 WxMpService mpService = mpServiceFactory.getRequiredMpService(reqVO.getAccountId()); @@ -44,12 +57,29 @@ public class MpFreePublishController { } catch (WxErrorException e) { throw exception(FREE_PUBLISH_LIST_FAIL, e.getError().getErrorMsg()); } - // todo 芋艿:需要查询对应的缩略图,不然前端无法展示 + // 查询对应的图片地址。目的:解决公众号的图片链接无法在我们后台展示 + setFreePublishThumbUrl(publicationRecords.getItems()); // 返回分页 return success(new PageResult<>(publicationRecords.getItems(), publicationRecords.getTotalCount().longValue())); } + private void setFreePublishThumbUrl(List items) { + // 1.1 获得 mediaId 数组 + Set mediaIds = new HashSet<>(); + items.forEach(item -> item.getContent().getNewsItem().forEach(newsItem -> mediaIds.add(newsItem.getThumbMediaId()))); + if (CollUtil.isEmpty(mediaIds)) { + return; + } + // 1.2 批量查询对应的 Media 素材 + Map materials = CollectionUtils.convertMap(mpMaterialService.getMaterialListByMediaId(mediaIds), + MpMaterialDO::getMediaId); + + // 2. 设置回 WxMpFreePublishItem 记录 + items.forEach(item -> item.getContent().getNewsItem().forEach(newsItem -> + findAndThen(materials, newsItem.getThumbMediaId(), material -> newsItem.setThumbUrl(material.getUrl())))); + } + @PostMapping("/submit") @ApiOperation("发布草稿") @ApiImplicitParams({ @@ -58,6 +88,7 @@ public class MpFreePublishController { @ApiImplicitParam(name = "mediaId", value = "要发布的草稿的 media_id", required = true, example = "2048", dataTypeClass = String.class) }) + @PreAuthorize("@ss.hasPermission('mp:free-publish:submit')") public CommonResult submitFreePublish(@RequestParam("accountId") Long accountId, @RequestParam("mediaId") String mediaId) { WxMpService mpService = mpServiceFactory.getRequiredMpService(accountId); @@ -69,5 +100,24 @@ public class MpFreePublishController { } } + @DeleteMapping("/delete") + @ApiOperation("删除草稿") + @ApiImplicitParams({ + @ApiImplicitParam(name = "accountId", value = "公众号账号的编号", required = true, + example = "1024", dataTypeClass = Long.class), + @ApiImplicitParam(name = "articleId", value = "发布记录的编号", required = true, + example = "2048", dataTypeClass = String.class) + }) + @PreAuthorize("@ss.hasPermission('mp:free-publish:delete')") + public CommonResult deleteFreePublish(@RequestParam("accountId") Long accountId, + @RequestParam("articleId") String articleId) { + WxMpService mpService = mpServiceFactory.getRequiredMpService(accountId); + try { + mpService.getFreePublishService().deletePushAllArticle(articleId); + return success(true); + } catch (WxErrorException e) { + throw exception(FREE_PUBLISH_DELETE_FAIL, e.getError().getErrorMsg()); + } + } } diff --git a/yudao-module-mp/yudao-module-mp-biz/src/main/java/cn/iocoder/yudao/module/mp/dal/mysql/material/MpMaterialMapper.java b/yudao-module-mp/yudao-module-mp-biz/src/main/java/cn/iocoder/yudao/module/mp/dal/mysql/material/MpMaterialMapper.java index 5ff70b753..ee6fc7e00 100644 --- a/yudao-module-mp/yudao-module-mp-biz/src/main/java/cn/iocoder/yudao/module/mp/dal/mysql/material/MpMaterialMapper.java +++ b/yudao-module-mp/yudao-module-mp-biz/src/main/java/cn/iocoder/yudao/module/mp/dal/mysql/material/MpMaterialMapper.java @@ -7,6 +7,9 @@ import cn.iocoder.yudao.module.mp.controller.admin.material.vo.MpMaterialPageReq import cn.iocoder.yudao.module.mp.dal.dataobject.material.MpMaterialDO; import org.apache.ibatis.annotations.Mapper; +import java.util.Collection; +import java.util.List; + @Mapper public interface MpMaterialMapper extends BaseMapperX { @@ -21,4 +24,8 @@ public interface MpMaterialMapper extends BaseMapperX { .eqIfPresent(MpMaterialDO::getType, pageReqVO.getType())); } + default List selectListByMediaId(Collection mediaIds) { + return selectList(MpMaterialDO::getMediaId, mediaIds); + } + } diff --git a/yudao-module-mp/yudao-module-mp-biz/src/main/java/cn/iocoder/yudao/module/mp/service/material/MpMaterialService.java b/yudao-module-mp/yudao-module-mp-biz/src/main/java/cn/iocoder/yudao/module/mp/service/material/MpMaterialService.java index 5f574f8b3..c50ae45b0 100644 --- a/yudao-module-mp/yudao-module-mp-biz/src/main/java/cn/iocoder/yudao/module/mp/service/material/MpMaterialService.java +++ b/yudao-module-mp/yudao-module-mp-biz/src/main/java/cn/iocoder/yudao/module/mp/service/material/MpMaterialService.java @@ -9,6 +9,8 @@ import me.chanjar.weixin.common.api.WxConsts; import javax.validation.Valid; import java.io.IOException; +import java.util.Collection; +import java.util.List; /** * 公众号素材 Service 接口 @@ -55,4 +57,12 @@ public interface MpMaterialService { */ PageResult getMaterialPage(MpMaterialPageReqVO pageReqVO); + /** + * 获得素材列表 + * + * @param mediaIds 素材 mediaId 列表 + * @return 素材列表 + */ + List getMaterialListByMediaId(Collection mediaIds); + } diff --git a/yudao-module-mp/yudao-module-mp-biz/src/main/java/cn/iocoder/yudao/module/mp/service/material/MpMaterialServiceImpl.java b/yudao-module-mp/yudao-module-mp-biz/src/main/java/cn/iocoder/yudao/module/mp/service/material/MpMaterialServiceImpl.java index af46134aa..d2f72dca9 100644 --- a/yudao-module-mp/yudao-module-mp-biz/src/main/java/cn/iocoder/yudao/module/mp/service/material/MpMaterialServiceImpl.java +++ b/yudao-module-mp/yudao-module-mp-biz/src/main/java/cn/iocoder/yudao/module/mp/service/material/MpMaterialServiceImpl.java @@ -27,6 +27,8 @@ import org.springframework.validation.annotation.Validated; import javax.annotation.Resource; import java.io.File; import java.io.IOException; +import java.util.Collection; +import java.util.List; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.module.mp.enums.ErrorCodeConstants.MATERIAL_UPLOAD_FAIL; @@ -146,6 +148,11 @@ public class MpMaterialServiceImpl implements MpMaterialService { return mpMaterialMapper.selectPage(pageReqVO); } + @Override + public List getMaterialListByMediaId(Collection mediaIds) { + return mpMaterialMapper.selectListByMediaId(mediaIds); + } + /** * 下载微信媒体文件的内容,并上传到文件服务 * diff --git a/yudao-ui-admin/src/api/mp/freePublish.js b/yudao-ui-admin/src/api/mp/freePublish.js index ba24213df..558a9493f 100644 --- a/yudao-ui-admin/src/api/mp/freePublish.js +++ b/yudao-ui-admin/src/api/mp/freePublish.js @@ -8,3 +8,11 @@ export function getFreePublishPage(query) { params: query }) } + +// 删除公众号素材 +export function deleteFreePublish(accountId, articleId) { + return request({ + url: '/mp/free-publish/delete?accountId=' + accountId + '&articleId=' + articleId, + method: 'delete' + }) +} diff --git a/yudao-ui-admin/src/views/mp/components/wx-material-select/main.vue b/yudao-ui-admin/src/views/mp/components/wx-material-select/main.vue index 456846e26..24b35db3e 100644 --- a/yudao-ui-admin/src/views/mp/components/wx-material-select/main.vue +++ b/yudao-ui-admin/src/views/mp/components/wx-material-select/main.vue @@ -1,6 +1,8 @@