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 f92fe38c8..409d32c88 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 @@ -47,6 +47,7 @@ public interface ErrorCodeConstants { ErrorCode DRAFT_LIST_FAIL = new ErrorCode(1006007000, "获得草稿列表失败,原因:{}"); ErrorCode DRAFT_CREATE_FAIL = new ErrorCode(1006007001, "创建草稿失败,原因:{}"); ErrorCode DRAFT_UPDATE_FAIL = new ErrorCode(1006007002, "更新草稿失败,原因:{}"); + ErrorCode DRAFT_DELETE_FAIL = new ErrorCode(1006007002, "删除草稿失败,原因:{}"); // TODO 要处理下 ErrorCode MENU_NOT_EXISTS = new ErrorCode(1006001002, "菜单不存在"); diff --git a/yudao-module-mp/yudao-module-mp-biz/src/main/java/cn/iocoder/yudao/module/mp/controller/admin/news/MpDraftController.java b/yudao-module-mp/yudao-module-mp-biz/src/main/java/cn/iocoder/yudao/module/mp/controller/admin/news/MpDraftController.java index ba2323716..148b5a5fe 100644 --- a/yudao-module-mp/yudao-module-mp-biz/src/main/java/cn/iocoder/yudao/module/mp/controller/admin/news/MpDraftController.java +++ b/yudao-module-mp/yudao-module-mp-biz/src/main/java/cn/iocoder/yudao/module/mp/controller/admin/news/MpDraftController.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.MpDraftPageReqVO; +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; @@ -12,17 +16,21 @@ import io.swagger.annotations.ApiOperation; import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.bean.draft.*; +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/draft") @@ -32,8 +40,12 @@ public class MpDraftController { @Resource private MpServiceFactory mpServiceFactory; + @Resource + private MpMaterialService mpMaterialService; + @GetMapping("/page") @ApiOperation("获得草稿分页") + @PreAuthorize("@ss.hasPermission('mp:draft:query')") public CommonResult> getDraftPage(MpDraftPageReqVO reqVO) { // 从公众号查询草稿箱 WxMpService mpService = mpServiceFactory.getRequiredMpService(reqVO.getAccountId()); @@ -43,16 +55,35 @@ public class MpDraftController { } catch (WxErrorException e) { throw exception(DRAFT_LIST_FAIL, e.getError().getErrorMsg()); } + // 查询对应的图片地址。目的:解决公众号的图片链接无法在我们后台展示 + setDraftThumbUrl(draftList.getItems()); // 返回分页 return success(new PageResult<>(draftList.getItems(), draftList.getTotalCount().longValue())); } + private void setDraftThumbUrl(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. 设置回 WxMpDraftItem 记录 + items.forEach(item -> item.getContent().getNewsItem().forEach(newsItem -> + findAndThen(materials, newsItem.getThumbMediaId(), material -> newsItem.setThumbUrl(material.getUrl())))); + } + @PostMapping("/create") @ApiOperation("创建草稿") @ApiImplicitParam(name = "accountId", value = "公众号账号的编号", required = true, example = "1024", dataTypeClass = Long.class) - public CommonResult createDraft(@RequestParam("accountId") Long accountId, + @PreAuthorize("@ss.hasPermission('mp:draft:create')") + public CommonResult deleteDraft(@RequestParam("accountId") Long accountId, @RequestBody WxMpAddDraft draft) { WxMpService mpService = mpServiceFactory.getRequiredMpService(accountId); try { @@ -71,7 +102,8 @@ public class MpDraftController { @ApiImplicitParam(name = "mediaId", value = "草稿素材的编号", required = true, example = "xxx", dataTypeClass = String.class), }) - public CommonResult createDraft(@RequestParam("accountId") Long accountId, + @PreAuthorize("@ss.hasPermission('mp:draft:update')") + public CommonResult deleteDraft(@RequestParam("accountId") Long accountId, @RequestParam("mediaId") String mediaId, @RequestBody List articles) { WxMpService mpService = mpServiceFactory.getRequiredMpService(accountId); @@ -86,4 +118,24 @@ public class MpDraftController { } } + @DeleteMapping("/delete") + @ApiOperation("删除草稿") + @ApiImplicitParams({ + @ApiImplicitParam(name = "accountId", value = "公众号账号的编号", required = true, + example = "1024", dataTypeClass = Long.class), + @ApiImplicitParam(name = "mediaId", value = "草稿素材的编号", required = true, + example = "xxx", dataTypeClass = String.class), + }) + @PreAuthorize("@ss.hasPermission('mp:draft:delete')") + public CommonResult deleteDraft(@RequestParam("accountId") Long accountId, + @RequestParam("mediaId") String mediaId) { + WxMpService mpService = mpServiceFactory.getRequiredMpService(accountId); + try { + mpService.getDraftService().delDraft(mediaId); + return success(true); + } catch (WxErrorException e) { + throw exception(DRAFT_DELETE_FAIL, e.getError().getErrorMsg()); + } + } + } diff --git a/yudao-ui-admin/src/api/mp/draft.js b/yudao-ui-admin/src/api/mp/draft.js index 9e2376f64..2358932dc 100644 --- a/yudao-ui-admin/src/api/mp/draft.js +++ b/yudao-ui-admin/src/api/mp/draft.js @@ -28,3 +28,11 @@ export function updateDraft(accountId, mediaId, articles) { data: articles }) } + +// 删除草稿 +export function deleteDraft(accountId, mediaId) { + return request({ + url: '/mp/draft/delete?accountId=' + accountId + '&mediaId=' + mediaId, + method: 'delete', + }) +} diff --git a/yudao-ui-admin/src/api/mp/freePublish.js b/yudao-ui-admin/src/api/mp/freePublish.js index 558a9493f..211a66a66 100644 --- a/yudao-ui-admin/src/api/mp/freePublish.js +++ b/yudao-ui-admin/src/api/mp/freePublish.js @@ -16,3 +16,11 @@ export function deleteFreePublish(accountId, articleId) { method: 'delete' }) } + +// 发布公众号素材 +export function submitFreePublish(accountId, mediaId) { + return request({ + url: '/mp/free-publish/submit?accountId=' + accountId + '&mediaId=' + mediaId, + method: 'post' + }) +} diff --git a/yudao-ui-admin/src/views/mp/draft/index.vue b/yudao-ui-admin/src/views/mp/draft/index.vue index ecb79ad8d..dcac01487 100644 --- a/yudao-ui-admin/src/views/mp/draft/index.vue +++ b/yudao-ui-admin/src/views/mp/draft/index.vue @@ -22,6 +22,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 芋道源码: ① 优化代码,和项目的代码保持一致 + ② 清理冗余代码,保证代码整洁 + ③ 增加注释,提升可读性 -->