首页>代码>spring+spring boot+mybatis+shiro+thymeleaf实现社区党务后台管理系统>/PartyAffairs/Backstage/src/main/java/com/dai/party/controller/ArticleManage.java
package com.dai.party.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dai.party.pojo.Article;
import com.dai.party.pojo.Result;
import com.dai.party.service.ArticleService;
import com.dai.party.service.CategoryService;
import com.dai.party.tool.verification.ValidateUtil;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.HtmlUtils;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.*;
import java.io.IOException;

/**
 * @program: PartyAffairs
 * @description: 后台文章管理
 * @author: Dai Yuanchuan
 * @create: 2019-01-15 13:39
 **/
@RestController
@Slf4j
@Api(value = "后台文章管理", tags = {"后台文章管理"})
@EnableSwagger2
@RequestMapping(value = "/article", method = {RequestMethod.POST})
public class ArticleManage {

    /**
     * 小程序文章信息注入
     */
    @Autowired
    private ArticleService articleService;

    /**
     * 小程序分类服务注入
     */
    @Autowired
    private CategoryService categoryService;

    @ApiOperation(value = "新增文章信息", notes = "参数:文章信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "此项填0", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "此项填0:党建的类别(1:主体活动 2:党务知识)", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "categoryID", value = "文章所属分类ID", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "mark", value = "小程序标识(0:党务 1:党建)", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/increase", produces = "application/json;charset=UTF-8")
    public Result increase(@Valid VerificationArticle verificationArticle, BindingResult bindingResult, String mark, String type,
                           String categoryID, HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        if (bindingResult.hasErrors()) {
            return Result.fail("4170", bindingResult.getFieldError().getDefaultMessage());
        }

        boolean isMark = !ValidateUtil.isNumeric(mark) || (!mark.equals(Result.SUCCESS) && !mark.equals(Result.FAIL));

        if (isMark) {
            return Result.fail("错误的标识");
        }

        // 构建轮播图实体
        Article category = Article.builder()
                .author(verificationArticle.getAuthor())
                .commentNum(Integer.valueOf(verificationArticle.getCommentNum()))
                .content(HtmlUtils.htmlEscape(ValidateUtil.filterEmoji(verificationArticle.getContent())))
                .cover(verificationArticle.getCover())
                .isShowIcon(Integer.valueOf(verificationArticle.getIsShowIcon()))
                .createTime(System.currentTimeMillis())
                .isShow(Integer.valueOf(verificationArticle.getIsShow()))
                .like(Long.valueOf(verificationArticle.getLike()))
                .reading(Long.valueOf(verificationArticle.getReading()))
                .articleAttribute(Integer.valueOf(verificationArticle.getArticleAttribute()))
                .link(verificationArticle.getLink())
                .title(ValidateUtil.filterEmoji(verificationArticle.getTitle()))
                .categoryID(Integer.valueOf(categoryID))
                .miniProgramMark(Integer.valueOf(mark))
                .type(Integer.valueOf(type))
                .build();

        return articleService.increase(category) ? Result.ok() : Result.fail("");
    }

    @ApiOperation(value = "获取文章信息列表", notes = "参数:根据mark、文章类型、页码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mark", value = "小程序标识", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "党建的类别(0:不限制 1:主题活动 2:党务知识)", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/getArticleList", produces = "application/json;charset=UTF-8")
    public Result getArticleList(String mark, String type, String page, String pageSize,
                                 HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        boolean isMark = !ValidateUtil.isNumeric(mark) || (!(Result.SUCCESS).equals(mark) && !(Result.FAIL).equals(mark));

        if (isMark) {
            return Result.fail("错误的标识");
        }
        if (StringUtils.isBlank(type) || !ValidateUtil.isNumeric(type)) {
            return Result.fail("错误的状态");
        }
        if (StringUtils.isBlank(page) || !ValidateUtil.isNumeric(page)) {
            return Result.fail("错误的页码");
        }
        if (StringUtils.isBlank(pageSize) || !ValidateUtil.isNumeric(pageSize)) {
            return Result.fail("错误的页码大小");
        }

        // 文章列表
        JSONArray lookupAccordingToPage = Article.fromPojo(articleService.lookupAccordingToPage(Integer.valueOf(mark),
                Integer.valueOf(type),
                Integer.valueOf(page),
                Integer.valueOf(pageSize)));
        // 总数
        Integer total = articleService.total(" where `mini_program_mark` = "+mark+" and `type` = "+type+" ");
        return Result.ok(JSON.parseObject("{\"article\":" + lookupAccordingToPage + ",\"total\":" + total + "}"));
    }

    @ApiOperation(value = "获取文章详细信息", notes = "参数:文章ID")
    @ApiImplicitParam(name = "id", value = "文章ID", required = true, dataType = "String", paramType = "query")
    @RequestMapping(value = "/getArticleInfo", produces = "application/json;charset=UTF-8")
    public Result getArticleInfo(String id, HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        if (!ValidateUtil.isNumeric(id)) {
            return Result.fail("错误的文章ID");
        }
        Article article = articleService.lookupToID(Integer.valueOf(id));
        if (article == null) {
            return Result.fail("轮播信息不存在");
        }
        JSONObject lookupToID = Article.fromPojo(article);
        return Result.ok(lookupToID);
    }

    @ApiOperation(value = "根据标题搜索", notes = "参数:keyword、页码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "关键字", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/search", produces = "application/json;charset=UTF-8")
    public Result Search(String keyword, String page, String pageSize, HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        if (StringUtils.isBlank(page) || !ValidateUtil.isNumeric(page)) {
            return Result.fail("错误的页码");
        }
        if (StringUtils.isBlank(pageSize) || !ValidateUtil.isNumeric(pageSize)) {
            return Result.fail("错误的页码大小");
        }
        if (StringUtils.isBlank(keyword)) {
            return Result.fail("关键字不能为空");
        }
        JSONArray search = Article.fromPojo(articleService
                .searchBackstage(keyword, Integer.valueOf(page), Integer.valueOf(pageSize)));
        // 总数
        Integer total = articleService.total(" where LOCATE(" + keyword + ", `title`)>0 ");
        return Result.ok(JSON.parseObject("{\"article\":" + search + ",\"total\":" + total + "}"));
    }

    @ApiOperation(value = "修改文章信息", notes = "参数:文章信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mark", value = "小程序标识", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "categoryID", value = "分类ID", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/setArticleInfo", produces = "application/json;charset=UTF-8")
    public Result setArticleInfo(@Valid VerificationArticle verificationArticle, BindingResult bindingResult,String mark,String categoryID, HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        if (bindingResult.hasErrors()) {
            return Result.fail("4170", bindingResult.getFieldError().getDefaultMessage());
        }

        if(!ValidateUtil.isNumeric(categoryID)){
            return Result.fail("错误的分类");
        }

        if (articleService.lookupToID(Integer.valueOf(verificationArticle.getId())) == null) {
            return Result.fail("文章信息不存在");
        }

        if (categoryService.lookupToID(Integer.valueOf(categoryID)) == null) {
            return Result.fail("分类信息不存在");
        }

        boolean isMark = !ValidateUtil.isNumeric(mark) || (!(Result.SUCCESS).equals(mark) && !(Result.FAIL).equals(mark));

        if (isMark) {
            return Result.fail("错误的标识");
        }

        // 构建分类实体
        Article article = Article.builder()
                .id(Integer.valueOf(verificationArticle.getId()))
                .author(verificationArticle.getAuthor())
                .commentNum(Integer.valueOf(verificationArticle.getCommentNum()))
                .categoryID(Integer.valueOf(categoryID))
                .content(verificationArticle.getContent())
                .cover(verificationArticle.getCover())
                .isShowIcon(Integer.valueOf(verificationArticle.getIsShowIcon()))
                .isShow(Integer.valueOf(verificationArticle.getIsShow()))
                .like(Long.valueOf(verificationArticle.getLike()))
                .reading(Long.valueOf(verificationArticle.getReading()))
                .articleAttribute(Integer.valueOf(verificationArticle.getArticleAttribute()))
                .link(verificationArticle.getLink())
                .title(ValidateUtil.filterEmoji(verificationArticle.getTitle()))
                .miniProgramMark(Integer.valueOf(mark))
                .build();

        // 修改
        articleService.modify(article);
        return Result.ok("修改成功");
    }

    @ApiOperation(value = "删除文章信息", notes = "参数:文章ID")
    @ApiImplicitParam(name = "id", value = "文章ID", required = true, dataType = "String", paramType = "query")
    @RequestMapping(value = "/delArticle", produces = "application/json;charset=UTF-8")
    public Result delArticle(String id, HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        if (!ValidateUtil.isNumeric(id)) {
            return Result.fail("文章ID必须为数字");
        }

        Article article = articleService.lookupToID(Integer.valueOf(id));
        if (article == null) {
            return Result.fail("轮播信息不存在");
        }
        // 刪除数据
        return articleService.delete(article.getId()) ? Result.ok() : Result.fail("删除失败");
    }
}

@NoArgsConstructor
@AllArgsConstructor
@Builder
@Data
@ApiModel(value = "文章信息校验")
@JsonInclude(JsonInclude.Include.NON_NULL)
class VerificationArticle {

    @NotBlank(message = "文章ID不能为空")
    @Pattern(regexp = "[0-9]*", message = "文章ID必须为数字")
    @ApiModelProperty(value = "文章ID")
    private String id;

    @Size(max = 200, message = "作者名称过长")
    @ApiModelProperty(value = "文章作者")
    private String author;

    @Size(max = 200, message = "评论人数过长")
    @NotBlank(message = "评论人数不能为空")
    @ApiModelProperty(value = "文章评论人数")
    private String commentNum;

    @NotBlank(message = "文章内容不能为空")
    @ApiModelProperty(value = "文章内容")
    private String content;

    @Size(max = 250, message = "封面图链接过长")
    @Pattern(regexp = "^(http|https|ftp)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]", message = "缩略图格式错误")
    @ApiModelProperty(value = "文章封面缩略图链接")
    private String cover;

    @Pattern(regexp = "[0-9]*", message = "错误的标识")
    @Max(value = 1, message = "错误的标识")
    @Min(value = 0, message = "错误的标识")
    @NotBlank(message = "isShowIcon必填")
    @ApiModelProperty(value = "是否显示缩略图")
    private String isShowIcon;

    @Pattern(regexp = "[0-9]*", message = "错误的标识")
    @Max(value = 1, message = "错误的标识")
    @Min(value = 0, message = "错误的标识")
    @NotBlank(message = "isShow必填")
    @ApiModelProperty(value = "是否显示")
    private String isShow;

    @Pattern(regexp = "[0-9]*", message = "点赞数必须为数字")
    @NotBlank(message = "点赞数必填")
    @ApiModelProperty(value = "点赞数")
    private String like;

    @Pattern(regexp = "[0-9]*", message = "阅读数必须为数字")
    @NotBlank(message = "阅读数必填")
    @ApiModelProperty(value = "文章阅读数")
    private String reading;

    @Pattern(regexp = "[0-9]*", message = "错误的标识")
    @Max(value = 1, message = "错误的标识")
    @Min(value = 0, message = "错误的标识")
    @NotBlank(message = "外链属性必填")
    @ApiModelProperty(value = "文章外链属性(0:图文、1:视频)")
    private String articleAttribute;

    @Size(max = 250, message = "URL过长")
    @Pattern(regexp = "^(http|https|ftp)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]", message = "外链格式错误")
    @ApiModelProperty(value = "文章外链")
    private String link;

    @NotBlank(message = "标题不能为空")
    @Size(max = 200, message = "标题过长")
    @ApiModelProperty(value = "文章标题")
    private String title;
}
最近下载更多
小温ggggg  LV6 2023年10月24日
爱吃鱼的猫Vital  LV6 2023年7月31日
suyuquan  LV2 2023年7月31日
happyMrLi  LV5 2023年7月31日
yunYUN123  LV1 2023年2月26日
taowufeng2  LV8 2022年12月9日
翰昌之杰2  LV6 2022年11月30日
1025490081  LV2 2022年11月9日
wylwylqq  LV6 2022年11月6日
wuying8208  LV15 2022年10月31日
最近浏览更多
刘孟飞  LV19 4月14日
泓鼎168  LV19 3月27日
wbw123  LV5 3月10日
try8023  LV18 1月22日
廖业贵  LV18 1月5日
WBelong  LV7 2023年12月27日
hhh12345  LV7 2023年12月23日
可乐123123  LV15 2023年12月20日
小芳同学  LV1 2023年12月8日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友