mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-07-23 23:45:08 +08:00
mp:完善【发布功能】的相关代码
This commit is contained in:
@ -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, "获得草稿列表失败,原因:{}");
|
||||
|
@ -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<PageResult<WxMpFreePublishItem>> 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<WxMpFreePublishItem> items) {
|
||||
// 1.1 获得 mediaId 数组
|
||||
Set<String> mediaIds = new HashSet<>();
|
||||
items.forEach(item -> item.getContent().getNewsItem().forEach(newsItem -> mediaIds.add(newsItem.getThumbMediaId())));
|
||||
if (CollUtil.isEmpty(mediaIds)) {
|
||||
return;
|
||||
}
|
||||
// 1.2 批量查询对应的 Media 素材
|
||||
Map<String, MpMaterialDO> 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<String> 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<Boolean> 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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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<MpMaterialDO> {
|
||||
|
||||
@ -21,4 +24,8 @@ public interface MpMaterialMapper extends BaseMapperX<MpMaterialDO> {
|
||||
.eqIfPresent(MpMaterialDO::getType, pageReqVO.getType()));
|
||||
}
|
||||
|
||||
default List<MpMaterialDO> selectListByMediaId(Collection<String> mediaIds) {
|
||||
return selectList(MpMaterialDO::getMediaId, mediaIds);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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<MpMaterialDO> getMaterialPage(MpMaterialPageReqVO pageReqVO);
|
||||
|
||||
/**
|
||||
* 获得素材列表
|
||||
*
|
||||
* @param mediaIds 素材 mediaId 列表
|
||||
* @return 素材列表
|
||||
*/
|
||||
List<MpMaterialDO> getMaterialListByMediaId(Collection<String> mediaIds);
|
||||
|
||||
}
|
||||
|
@ -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<MpMaterialDO> getMaterialListByMediaId(Collection<String> mediaIds) {
|
||||
return mpMaterialMapper.selectListByMediaId(mediaIds);
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载微信媒体文件的内容,并上传到文件服务
|
||||
*
|
||||
|
Reference in New Issue
Block a user