mitao
2025-03-06 01d6fa48a0de7a21988e89f71721b6b85e53b517
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.ruoyi.article.controller.management;
 
 
import com.alibaba.excel.EasyExcel;
import com.ruoyi.article.controller.management.dto.MgtSensitiveWordsDTO;
import com.ruoyi.article.controller.management.dto.MgtSensitiveWordsQuery;
import com.ruoyi.article.controller.management.vo.MgtSensitiveWordsVO;
import com.ruoyi.article.service.ISensitiveWordsService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.page.CollUtils;
import com.ruoyi.common.core.utils.page.PageDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.io.IOException;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
 
/**
 * <p>
 * 敏感词管理 前端控制器
 * </p>
 *
 * @author mitao
 * @since 2024-05-16
 */
@RestController
@RequestMapping("/mgt/sensitive-words")
@RequiredArgsConstructor
@Api(value = "管理后台-敏感词管理相关接口", tags = "管理后台-敏感词管理相关接口")
public class MgtSensitiveWordsController {
 
    private final ISensitiveWordsService sensitiveWordsService;
 
    /**
     * 获取敏感词列表的分页数据
     *
     * @param query 管理后台-敏感词查询对象
     * @return PageDTO<MgtSensitiveWordsVO>
     */
    @ApiOperation("获取敏感词列表的分页数据")
    @PostMapping("/page")
    public R<PageDTO<MgtSensitiveWordsVO>> getSensitiveWordsPage(
            @Validated @RequestBody MgtSensitiveWordsQuery query) {
        return R.ok(sensitiveWordsService.getSensitiveWordsPage(query));
    }
 
    /**
     * 添加敏感词
     *
     * @param dto 敏感词数据传输对象
     */
    @ApiOperation("添加敏感词")
    @PostMapping("/save")
    public R<?> saveSensitiveWords(@Validated @RequestBody MgtSensitiveWordsDTO dto) {
        sensitiveWordsService.saveSensitiveWords(dto);
        return R.ok();
    }
 
    /**
     * 上传敏感词
     *
     * @param id 敏感词id
     */
    @ApiOperation("删除敏感词")
    @DeleteMapping("/{id}")
    public R<?> delSensitiveWords(
            @ApiParam(name = "id", value = "敏感词id", required = true) @PathVariable("id") Long id) {
        sensitiveWordsService.removeById(id);
        return R.ok();
    }
 
    /**
     * 下载导入模板
     *
     * @param response
     */
    @ApiOperation("下载导入模板")
    @GetMapping("/exportTemplate")
    public void exportSensitiveWordsTemplate(HttpServletResponse response) {
        // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
        response.setContentType(
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        try {
            String fileName = URLEncoder.encode("敏感词导入模板", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition",
                    "attachment;filename*=utf-8''" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), MgtSensitiveWordsDTO.class)
                    .sheet("敏感词导入模板")
                    .doWrite(CollUtils.emptyList());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
 
    /**
     * 敏感词导入
     *
     * @param file 导入文件
     * @return
     */
    @ApiOperation("敏感词导入")
    @PostMapping("/importSensitiveWords")
    public R<?> importSensitiveWords(@RequestPart("file") MultipartFile file) {
        try {
            sensitiveWordsService.importSensitiveWords(file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return R.ok();
    }
}