目标

  • 完成自媒体文章列表查询功能
  • 完成自媒体文章的发布功能
  • 完成自媒体文章的查询
  • 完成自媒体文章的删除功能
  • 完成自媒体文章的上下架功能功能

    1 自媒体文章列表查询

    1.1 需求分析

    1597905150395.png

    1.2 表结构和实体类

    wm_news 自媒体文章表
    1585849426205.png
    自媒体文章实体类 ``` package com.heima.model.media.pojos;

import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; import org.apache.ibatis.type.Alias;

import java.io.Serializable; import java.util.Date;

/**

  • 自媒体图文内容信息表
  • *
  • @author itheima */ @Data @TableName(“wm_news”) public class WmNews implements Serializable {

    private static final long serialVersionUID = 1L;

    /**

    • 主键 */ @TableId(value = “id”, type = IdType.AUTO) private Integer id;

      /**

    • 自媒体用户ID */ @TableField(“user_id”) private Integer userId;

      /**

    • 标题 */ @TableField(“title”) private String title;

      /**

    • 图文内容 */ @TableField(“content”) private String content;

      /**

    • 文章布局

      1. 0 无图文章
      2. 1 单图文章
      3. 3 多图文章

      */ @TableField(“type”) private Short type;

      /**

    • 图文频道ID */ @TableField(“channel_id”) private Integer channelId;

      @TableField(“labels”) private String labels;

      /**

    • 创建时间 */ @TableField(“created_time”) private Date createdTime;

      /**

    • 提交时间 */ @TableField(“submited_time”) private Date submitedTime;

      /**

    • 当前状态

      1. 0 草稿
      2. 1 提交(待审核)
      3. 2 审核失败
      4. 3 人工审核
      5. 4 人工审核通过
      6. 8 审核通过(待发布)
      7. 9 已发布

      */ @TableField(“status”) private Short status;

      /**

    • 定时发布时间,不定时则为空 */ @TableField(“publish_time”) private Date publishTime;

      /**

    • 拒绝理由 */ @TableField(“reason”) private String reason;

      /**

    • 发布库文章ID */ @TableField(“article_id”) private Integer articleId;

      /**

    • //图片用逗号分隔 */ @TableField(“images”) private String images;

      @TableField(“enable”) private Short enable;

      //状态枚举类 @Alias(“WmNewsStatus”) public enum Status{ NORMAL((short)0),SUBMIT((short)1),FAIL((short)2),ADMIN_AUTH((short)3),ADMIN_SUCCESS((short)4),SUCCESS((short)8),PUBLISHED((short)9); short code; Status(short code){

      1. this.code = code;

      } public short getCode(){

      1. return this.code;

      } }

}

  1. ### 1.3 接口定义
  2. 在接口工程heima-leadnews-apis中定义接口<br />com.heima.api.wemedia.WmNewsControllerApi

package com.heima.api.wemedia;

import com.heima.model.common.dtos.ResponseResult;

/**

  • 自媒体文章接口 */ public interface WmNewsControllerApi {

    /**

    • 分页带条件查询自媒体文章列表
    • @param wmNewsPageReqDto
    • @return */ public ResponseResult findAll(WmNewsPageReqDto wmNewsPageReqDto); }
  1. WmNewsPageReqDto

package com.heima.model.media.dtos;

import com.heima.model.common.dtos.PageRequestDto; import lombok.Data;

import java.util.Date;

@Data public class WmNewsPageReqDto extends PageRequestDto {

  1. private Short status;//状态
  2. private Date beginPubdate;//开始时间
  3. private Date endPubdate;//结束时间
  4. private Integer channelId;//所属频道ID
  5. private String keyWord;//关键字

}

  1. ### 1.4 mapper实现
  2. 创建com.heima.wemedia.mapper.WmNewsMapper

package com.heima.wemedia.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.heima.model.media.pojos.WmNews; import org.apache.ibatis.annotations.Mapper;

@Mapper public interface WmNewsMapper extends BaseMapper { }

  1. ### 1.5 service代码实现
  2. 定义WmNewsService接口:com.heima.wemedia.service.WmNewsService

package com.heima.wemedia.service;

import com.baomidou.mybatisplus.extension.service.IService; import com.heima.model.common.dtos.ResponseResult; import com.heima.model.media.dtos.WmNewsPageReqDto; import com.heima.model.media.pojos.WmNews;

public interface WmNewsService extends IService {

  1. /**
  2. * 查询所有自媒体文章
  3. * @return
  4. */
  5. public ResponseResult findAll(WmNewsPageReqDto wmNewsPageReqDto);

}

  1. 定义实现类 com.heima.wemedia.service.impl.WmNewsServiceImpl

package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.heima.model.common.dtos.PageResponseResult; import com.heima.model.common.dtos.ResponseResult; import com.heima.model.common.enums.AppHttpCodeEnum; import com.heima.model.wemedia.dtos.WmNewsPageReqDto; import com.heima.model.wemedia.pojos.WmNews; import com.heima.model.wemedia.pojos.WmUser; import com.heima.utils.threadlocal.WmThreadLocalUtils; import com.heima.wemedia.mapper.WmNewsMapper; import com.heima.wemedia.service.WmNewsService; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;

@Service @Transactional public class WmNewsServiceImpl extends ServiceImpl implements WmNewsService {

  1. @Value("${fdfs.url}")
  2. private String fileServerUrl;
  3. @Override
  4. public ResponseResult findAll(WmNewsPageReqDto dto) {
  5. //1.参数检查
  6. if(dto == null){
  7. return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
  8. }
  9. //分页参数检查
  10. dto.checkParam();
  11. //2.分页条件查询
  12. IPage pageParam = new Page(dto.getPage(),dto.getSize());
  13. LambdaQueryWrapper<WmNews> lambdaQueryWrapper = new LambdaQueryWrapper();
  14. //状态精确查询
  15. if(dto.getStatus() != null){
  16. lambdaQueryWrapper.eq(WmNews::getStatus,dto.getStatus());
  17. }
  18. //频道精确查询
  19. if(null != dto.getChannelId()){
  20. lambdaQueryWrapper.eq(WmNews::getChannelId,dto.getChannelId());
  21. }
  22. //时间范围查询
  23. if(dto.getBeginPubdate()!=null && dto.getEndPubdate()!=null){
  24. lambdaQueryWrapper.between(WmNews::getPublishTime,dto.getBeginPubdate(),dto.getEndPubdate());
  25. }
  26. //关键字模糊查询
  27. if(null != dto.getKeyword()){
  28. lambdaQueryWrapper.like(WmNews::getTitle,dto.getKeyword());
  29. }
  30. //查询当前登录用户的信息
  31. WmUser user = WmThreadLocalUtils.getUser();
  32. if(user==null){
  33. return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
  34. }
  35. lambdaQueryWrapper.eq(WmNews::getUserId,user.getId());
  36. //按照创建日期倒序
  37. lambdaQueryWrapper.orderByDesc(WmNews::getCreatedTime);
  38. IPage pageResult = page(pageParam, lambdaQueryWrapper);
  39. //3.结果封装返回
  40. PageResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),(int)pageResult.getTotal());
  41. responseResult.setData(pageResult.getRecords());
  42. responseResult.setHost(fileServerUrl);
  43. return responseResult;
  44. }

}

  1. ### 1.6 controller代码实现
  2. 在自媒体微服务中定义controllercom.heima.wemedia.controller.WmNewsController

package com.heima.wemedia.controller.v1;

import com.heima.apis.wemedia.WmNewsControllerApi; import com.heima.model.common.dtos.ResponseResult; import com.heima.model.wemedia.dtos.WmNewsPageReqDto; import com.heima.wemedia.service.WmNewsService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;

@RestController @RequestMapping(“/api/v1/news”) public class WmNewsController implements WmNewsControllerApi {

  1. @Autowired
  2. private WmNewsService wmNewsService;
  3. @PostMapping("/list")
  4. @Override
  5. public ResponseResult findAll(@RequestBody WmNewsPageReqDto wmNewsPageReqDto){
  6. return wmNewsService.findAll(wmNewsPageReqDto);
  7. }

}

  1. ### 1.7 测试
  2. 使用postman测试需要先登录,获取token后访问<br />![1596468902820.png](https://cdn.nlark.com/yuque/0/2021/png/12460132/1615432018764-141fea36-b118-41ce-a317-87493421f913.png)<br />或打开页面进行测试
  3. ## 2 频道列表展示
  4. ### 2.1需求分析
  5. ![1597904087446.png](https://cdn.nlark.com/yuque/0/2021/png/12460132/1615432018856-4bf8888d-655c-4161-9536-bf35553badb5.png)<br />文章展示列表页面打开的时候,默认自动加载频道列表数据进行展示,就是查询所有频道数据
  6. ### 2.2 接口定义
  7. AdChannelControllerApi接口中新增方法,查询所有频道的数据

/**

  1. * 查询所有频道
  2. * @return
  3. */

public ResponseResult findAll();

  1. ### 2.3 功能实现
  2. heima-leadnews-admin端,修改AdChannelController类,添加如下方法,mapperservice在之前已经定义

@GetMapping(“/channels”) @Override public ResponseResult findAll() { List list = adChannelService.list(); return ResponseResult.okResult(list); }

  1. ### 2.4 修改自媒体网关
  2. heima-leadnews-wemedia-gateway自媒体网关的yml配置文件中添加如下配置
  • id: admin uri: lb://leadnews-admin predicates:
  • Path=/admin/** filters:
  • StripPrefix= 1
  1. ### 2.5 测试
  2. 使用postman测试或者直接打开页面测试即可
  3. ## 3 自媒体文章-发布、修改,保存草稿
  4. ### 3.1 需求分析
  5. ![6uK2AnFDpy.png](https://cdn.nlark.com/yuque/0/2021/png/12460132/1615432018943-3af69681-ca16-4f98-8ac7-488d5dfb1384.png)<br />保存文章,除了需要wm_news表以外,还需要另外两张表<br />wm_material 素材表<br />![dLBfyc3ymp.png](https://cdn.nlark.com/yuque/0/2021/png/12460132/1615432019038-33e3fd10-54d3-4625-bb4d-7436a2d2237a.png)<br />wm_news_material 文章素材关系表<br />![AQk91v6mPe.png](https://cdn.nlark.com/yuque/0/2021/png/12460132/1615432019120-fb52235d-f3c2-484e-a5cc-88690420b26f.png)
  6. ### 3.2 思路分析
  7. -
  8. 该功能为保存、修改(是否有id)、保存草稿的共有方法
  9. -
  10. 如果有id修改文章,先删除所有素材关联关系
  11. -
  12. 如果没有id,保存文章
  13. -
  14. 关联内容中的图片与素材的关系
  15. -
  16. 关联封面中的图片与素材的关系
  17. -
  18. 封面图片如果选择是自动需要从内容中截图图片做为封面图片
  19. <br />截取规则为:内容图片的个数小于等于2 则为单图截图一张图,内容图片大于等于3,则为多图,截图三张图,内容中没有图片,则为无图

{ “title”:”黑马头条项目背景”, “type”:”1”,//这个 0 是无图 1 是单图 3 是多图 -1 是自动 “labels”:”黑马头条”, “publishTime”:”2020-03-14T11:35:49.000Z”, “channelId”:1, “images”:[ “http://192.168.200.130/group1/M00/00/00/wKjIgl5swbGATaSAAAEPfZfx6Iw790.png“ ], “status”:1, “content”:”[ { “type”:”text”, “value”:”随着智能手机的普及,人们更加习惯于通过手机来看新闻。由于生活节奏的加快,很多人只能利用碎片时间来获取信息,因此,对于移动资讯客户端的需求也越来越高。黑马头条项目正是在这样背景下开发出来。黑马头条项目采用当下火热的微服务+大数据技术架构实现。本项目主要着手于获取最新最热新闻资讯,通过大数据分析用户喜好精确推送咨询新闻” }, { “type”:”image”, “value”:”http://192.168.200.130/group1/M00/00/00/wKjIgl5swbGATaSAAAEPfZfx6Iw790.png“ } ]” }

  1. ### 3.3 接口定义
  2. 在接口中新增提交文章的方法:com.heima.api.wemedia.WmNewsControllerApi

/**

  1. * 提交文章
  2. * @param wmNews
  3. * @return
  4. */

ResponseResult summitNews(WmNewsDto wmNews);

  1. WmNewsDto

package com.heima.model.media.dtos;

import lombok.Data;

import java.util.Date; import java.util.List;

@Data public class WmNewsDto {

  1. private Integer id;
  2. /**
  3. * 标题
  4. */
  5. private String title;
  6. /**
  7. * 频道id
  8. */
  9. private Integer channelId;
  10. /**
  11. * 标签
  12. */
  13. private String labels;
  14. /**
  15. * 发布时间
  16. */
  17. private Date publishTime;
  18. /**
  19. * 文章内容
  20. */
  21. private String content;
  22. /**
  23. * 文章封面类型 0 无图 1 单图 3 多图 -1 自动
  24. */
  25. private Short type;
  26. /**
  27. * 是否上架 0 下架 1 上架
  28. */
  29. private Short enable;
  30. /**
  31. * 提交时间
  32. */
  33. private Date submitedTime;
  34. /**
  35. * 状态 提交为1 草稿为0
  36. */
  37. private Short status;
  38. /**
  39. * 拒绝理由
  40. */
  41. private String reason;
  42. /**
  43. * 封面图片列表 多张图以逗号隔开
  44. */
  45. private List<String> images;

}

  1. ### 3.4 mapper定义
  2. 修改WmNewsMaterialMapper,添加一个方法,用来批量添加数据,用于素材与文章关系做关联

void saveRelationsByContent(@Param(“materials”) List materials, @Param(“newsId”) Integer newId, @Param(“type”) int type);

  1. 新建resources\mapper\WmNewsMaterialMapper.xml文件

<?xml version=”1.0” encoding=”UTF-8”?> <!DOCTYPE mapper PUBLIC “-//mybatis.org//DTD Mapper 3.0//EN” “http://mybatis.org/dtd/mybatis-3-mapper.dtd">

insert into wm_news_material (material_id, news_id, type, ord) values (#{mid}, #{newsId}, #{type}, #{ord})
  1. ### 3.5 业务层代码
  2. 常量类准备:com.heima.common.constants.wemedia.WmMediaConstans

package com.heima.common.constans.wemedia;

public class WemediaContans {

  1. public static final Short COLLECT_MATERIAL = 1;//收藏
  2. public static final Short CANCEL_COLLECT_MATERIAL = 0;//收藏
  3. public static final String WM_NEWS_TYPE_IMAGE = "image";
  4. public static final Short WM_NEWS_NONE_IMAGE = 0;
  5. public static final Short WM_NEWS_SINGLE_IMAGE = 1;
  6. public static final Short WM_NEWS_MANY_IMAGE = 3;
  7. public static final Short WM_NEWS_TYPE_AUTO = -1;
  8. public static final Short WM_CONTENT_REFERENCE = 0;
  9. public static final Short WM_COVER_REFERENCE = 1;

}

  1. WmNewsService类中新增方法

/**

  • 自媒体文章发布
  • @param wmNews
  • @param isSubmit 是否为提交 1 为提交 0为草稿
  • @return */ ResponseResult saveNews(WmNewsDto wmNews, Short isSubmit);
  1. 实现类:

@Override public ResponseResult saveNews(WmNewsDto dto, Short isSubmit) { //1.检查参数 if (dto == null || StringUtils.isBlank(dto.getContent())) { return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID); }

  1. //2.保存或修改文章
  2. WmNews wmNews = new WmNews();
  3. BeanUtils.copyProperties(dto, wmNews);
  4. if (WemediaContans.WM_NEWS_AUTO_TYPE.equals(dto.getType())) {
  5. wmNews.setType(null);
  6. }
  7. if (dto.getImages() != null && dto.getImages().size() > 0) {
  8. //[dfjksdjfdfj.jpg,sdlkjfskld.jpg]
  9. wmNews.setImages(dto.getImages().toString().replace("[", "")
  10. .replace("]", "").replace(fileServerUrl, "")
  11. .replace(" ", ""));
  12. }
  13. //保存或修改文章
  14. saveWmNews(wmNews, isSubmit);
  15. //3.关联文章与素材的关系
  16. String content = dto.getContent();
  17. List<Map> list = JSON.parseArray(content, Map.class);
  18. List<String> materials = ectractUrlInfo(list);
  19. //3.1 关联内容中的图片与素材的关系
  20. if (isSubmit == WmNews.Status.SUBMIT.getCode() && materials.size() != 0) {
  21. ResponseResult responseResult = saveRelativeInfoForContent(materials, wmNews.getId());
  22. if (responseResult != null) {
  23. return responseResult;
  24. }
  25. }
  26. //3.2 关联封面中的图片与素材的关系,设置wm_news的type,自动
  27. if (isSubmit == WmNews.Status.SUBMIT.getCode()) {
  28. ResponseResult responseResult = saveRelativeInfoForCover(dto, materials, wmNews);
  29. if (responseResult != null) {
  30. return responseResult;
  31. }
  32. }
  33. return null;

}

/**

  1. * 设置封面图片与素材的关系
  2. *
  3. * @param dto
  4. * @param materials
  5. * @param wmNews
  6. * @return
  7. */

private ResponseResult saveRelativeInfoForCover(WmNewsDto dto, List materials, WmNews wmNews) { List images = dto.getImages(); //自动匹配封面 if (dto.getType().equals(WemediaContans.WM_NEWS_AUTO_TYPE)) { //内容中的图片数量小于等于2 设置为单图 if (materials.size() > 0 && materials.size() <= 2) { wmNews.setType(WemediaContans.WM_NEWS_SINGLE_TYPE); images = materials.stream().limit(1).collect(Collectors.toList()); } else if (materials.size() > 2) { //如果内容中的图片大于2 则设置为多图 wmNews.setType(WemediaContans.WM_NEWS_MANY_TYPE); images = materials.stream().limit(3).collect(Collectors.toList()); } else { //内容中没有图片,则是无图 wmNews.setType(WemediaContans.WM_NEWS_NONE_TYPE); } //修改文章信息 if (images != null && images.size() > 0) { wmNews.setImages(images.toString().replace(“[“, “”) .replace(“]”, “”).replace(fileServerUrl, “”) .replace(“ “, “”));

  1. }
  2. updateById(wmNews);
  3. }
  4. //保存封面图片与素材的关系
  5. if (images != null && images.size() > 0) {
  6. ResponseResult responseResult = saveRelativeInfoForImage(images, wmNews.getId());
  7. if (responseResult != null) {
  8. return responseResult;
  9. }
  10. }
  11. return null;

}

/**

  1. * @param images
  2. * @param newsId
  3. * @return
  4. */

private ResponseResult saveRelativeInfoForImage(List images, Integer newsId) { List materials = new ArrayList<>(); for (String image : images) { materials.add(image.replace(fileServerUrl,””)); }

  1. return saveRelativeInfo(materials,newsId,WemediaContans.WM_NEWS_COVER_REFERENCE);

}

/**

  1. * 保存素材与文章内容的关系
  2. *
  3. * @param materials
  4. * @param newsId
  5. * @return
  6. */

private ResponseResult saveRelativeInfoForContent(List materials, Integer newsId) { return saveRelativeInfo(materials, newsId, WemediaContans.WM_NEWS_CONTENT_REFERENCE); }

@Autowired private WmMaterialMapper wmMaterialMapper;

/**

  1. * 保存关系
  2. *
  3. * @param materials
  4. * @param newsId
  5. * @param type
  6. * @return
  7. */

private ResponseResult saveRelativeInfo(List materials, Integer newsId, Short type) { //1.获取数据库中的素材信息 LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.in(WmMaterial::getUrl, materials); lambdaQueryWrapper.eq(WmMaterial::getUserId, WmThreadLocalUtils.getUser().getId()); List dbMaterials = wmMaterialMapper.selectList(lambdaQueryWrapper); //2.通过图片的路径获取素材的id List materialsIds = new ArrayList<>(); if (dbMaterials != null && dbMaterials.size() > 0) { // Map uriIdMap = dbMaterials.stream().collect(Collectors.toMap(WmMaterial::getUrl, WmMaterial::getId)); for (String val : materials) { String materialId = String.valueOf(uriIdMap.get(val)); //没找到 if (“null”.equals(materialId)) { return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, “应用图片失效”); } //找到了 materialsIds.add(materialId); } }

  1. //3.批量保存数据
  2. wmNewsMaterialMapper.saveRelations(materialsIds, newsId, type);
  3. return null;

}

/**

  1. * 提取图片信息
  2. *
  3. * @param list
  4. * @return
  5. */

private List ectractUrlInfo(List list) { List materials = new ArrayList<>(); for (Map map : list) { if (map.get(“type”).equals(WemediaContans.WM_NEWS_TYPE_IMAGE)) { String imgUrl = (String) map.get(“value”); imgUrl = imgUrl.replace(fileServerUrl, “”); materials.add(imgUrl); } } return materials; }

@Autowired private WmNewsMaterialMapper wmNewsMaterialMapper;

/**

  1. * 保存或修改文章
  2. *
  3. * @param wmNews
  4. * @param isSubmit
  5. */

private void saveWmNews(WmNews wmNews, Short isSubmit) { wmNews.setStatus(isSubmit); wmNews.setUserId(WmThreadLocalUtils.getUser().getId()); wmNews.setCreatedTime(new Date()); wmNews.setSubmitedTime(new Date()); wmNews.setEnable((short) 1); if (wmNews.getId() == null) { save(wmNews); } else { //如果是修改,则先删除素材与文章的关系 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper(); queryWrapper.eq(WmNewsMaterial::getNewsId, wmNews.getId()); wmNewsMaterialMapper.delete(queryWrapper); updateById(wmNews); } }

  1. ### 3.6 控制层
  2. WmNewsController类中新增方法

@PostMapping(“/submit”) @Override public ResponseResult summitNews(@RequestBody WmNewsDto wmNews) { if(wmNews.getStatus()== WmNews.Status.SUBMIT.getCode()){ //提交文章 return wmNewsService.saveNews(wmNews, WmNews.Status.SUBMIT.getCode()); }else{ //保存草稿 return wmNewsService.saveNews(wmNews, WmNews.Status.NORMAL.getCode()); } }

  1. ### 3.7 测试
  2. postman工具测试<br />![1596469959479.png](https://cdn.nlark.com/yuque/0/2021/png/12460132/1615432019201-a4c70715-b069-4fde-a665-88d562881722.png)<br />打开前端工程联调
  3. ## 4 自媒体文章-根据id查询
  4. ### 4.1 需求分析
  5. ![1597836104065.png](https://cdn.nlark.com/yuque/0/2021/png/12460132/1615432019281-6ba08230-7cba-42ed-a0a3-91fa8d2dc756.png)<br />点击修改的时候,就是根据文章id查询,跳转至编辑页面进行展示<br />![1597836145040.png](https://cdn.nlark.com/yuque/0/2021/png/12460132/1615432019387-34845cb1-e0ba-46db-a048-5a535b8b93a2.png)
  6. ### 4.2 接口定义
  7. WmNewsControllerApi接口中新增方法:

/**

  1. * 根据id获取文章信息
  2. * @return
  3. */

ResponseResult findWmNewsById(Integer id);

  1. ### 4.3 业务层代码编写
  2. WmNewsService接口中新增方法 根据id查询文章的方法

/**

  1. * 根据文章id查询文章
  2. * @return
  3. */

ResponseResult findWmNewsById(Integer id);

  1. 实现类:

@Override public ResponseResult findWmNewsById(Integer id) { //1.参数检查 if(id == null){ return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,”文章Id不可缺少”); } //2.查询数据 WmNews wmNews = getById(id); if(wmNews == null){ return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,”文章不存在”); }

  1. //3.结果返回
  2. ResponseResult responseResult = ResponseResult.okResult(wmNews);
  3. responseResult.setHost(fileServerUrl);
  4. return responseResult;

}

  1. ### 4.4 控制层
  2. WmNewsController新增方法 查询文章

@GetMapping(“/one/{id}”) @Override public ResponseResult findWmNewsById(@PathVariable(“id”) Integer id) { return wmNewsService.findWmNewsById(id); }

  1. ### 4.5 测试
  2. ![1596470129311.png](https://cdn.nlark.com/yuque/0/2021/png/12460132/1615432019503-7ddf2fe9-965f-45ec-8dee-590f33ee0fe5.png)<br />打开页面进行测试
  3. ## 5 自媒体文章-删除
  4. ### 5.1 需求分析
  5. ![1597836189478.png](https://cdn.nlark.com/yuque/0/2021/png/12460132/1615432019581-81ae218f-4281-4c72-8698-dfbb8949bba5.png)
  6. - 当文章状态为9(已发布)且已上架则不能删除文章,下架状态可以删除,如果是其他状态可以删除
  7. - 删除文章之前需要先把素材与文章的关系删除掉
  8. ### 5.2 接口定义
  9. WmNewsControllerApi中添加删除方法

/**

  1. * 删除文章
  2. * @return
  3. */

ResponseResult delNews(Integer id);

  1. ### 5.3 业务层
  2. WmNewsService添加删除方法

/**

  1. * 删除文章
  2. * @return
  3. */

ResponseResult delNews(Integer id);

  1. 实现类:

@Override public ResponseResult delNews(Integer id) { //1.检查参数 if(id == null){ return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,”文章Id不可缺少”); } //2.获取数据 WmNews wmNews = getById(id); if(wmNews == null){ return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,”文章不存在”); }

  1. //3.判断当前文章的状态 status==9 enable == 1
  2. if(wmNews.getStatus().equals(WmNews.Status.PUBLISHED.getCode()) && wmNews.getEnable().equals(WemediaContans.WM_NEWS_ENABLE_UP)){
  3. return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"文章已发布,不能删除");
  4. }
  5. //4.去除素材与文章的关系
  6. wmNewsMaterialMapper.delete(Wrappers.<WmNewsMaterial>lambdaQuery().eq(WmNewsMaterial::getNewsId,wmNews.getId()));
  7. //5.删除文章
  8. removeById(wmNews.getId());
  9. return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

}

  1. ### 5.4 控制层
  2. WmNewsController中添加删除方法

@GetMapping(“/del_news/{id}”) @Override public ResponseResult delNews(@PathVariable(“id”) Integer id) { return wmNewsService.delNews(id); }

  1. ## 6 自媒体文章-上架、下架
  2. ### 6.1 需求分析
  3. ![1597836253759.png](https://cdn.nlark.com/yuque/0/2021/png/12460132/1615432019697-ed15e222-d5fc-4c4a-91e4-23582b22fb7c.png)<br />![1597836273736.png](https://cdn.nlark.com/yuque/0/2021/png/12460132/1615432019806-d909fb58-f66f-416a-93c7-e40c41df3afa.png)
  4. -
  5. 当前已经发布(状态为9)的文章可以上架(enable = 1),也可以下架(enable = 0
  6. -
  7. 在上架和下架操作的同时,需要同步app端的文章配置信息,暂时不做,后期讲到审核文章的时候再优化
  8. ### 6.2 接口定义
  9. WmNewsControllerApi中新增方法

/**

  1. * 上下架
  2. * @param dto
  3. * @return
  4. */

ResponseResult downOrUp(WmNewsDto dto);

  1. ### 6.3 业务层
  2. WmNewsService新增方法

/**

  1. * 上下架
  2. * @param dto
  3. * @return
  4. */

ResponseResult downOrUp(WmNewsDto dto);

  1. 实现类:

@Override public ResponseResult downOrUp(WmNewsDto dto) { //1.检查参数 if(dto == null || dto.getId() == null){ return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID); }

  1. //2.查询文章
  2. WmNews wmNews = getById(dto.getId());
  3. if(wmNews == null){
  4. return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"文章不存在");
  5. }
  6. //3.判断文章是否发布
  7. if(!wmNews.getStatus().equals(WmNews.Status.PUBLISHED.getCode())){
  8. return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"当前文章不是发布状态,不能上下架");
  9. }
  10. //4.修改文章状态,同步到app端(后期做)TODO
  11. if(dto.getEnable() != null && dto.getEnable() > -1 && dto.getEnable() < 2){
  12. update(Wrappers.<WmNews>lambdaUpdate().eq(WmNews::getId,dto.getId()).set(WmNews::getEnable,dto.getEnable()));
  13. }
  14. return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

}

  1. ### 6.4 控制层

@PostMapping(“/down_or_up”) @Override public ResponseResult downOrUp(@RequestBody WmNewsDto dto) { return wmNewsService.downOrUp(dto); }

```

6.5测试

1596470696717.png