peng
2026-03-18 e59a0201057ba67cad425fed804c82ff4ba0c6f1
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
package com.tievd.cube.modules.system.controller;
 
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.http.Header;
import cn.hutool.json.JSONArray;
import com.tievd.cube.commons.base.Result;
import com.tievd.cube.modules.system.service.ICommonService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.HandlerMapping;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
 
/**
 * 通用API
 *
 * @author 杨欣武
 * @version 2.5.2
 * @apiNote 提供常用API接口(错误提示页面,上传接口)
 * @since 2022-08-11
 */
@Slf4j
@Tag(name = "通用接口")
@RestController
@RequestMapping("/sys/common")
public class CommonController {
 
    @Autowired
    private ICommonService commonService;
 
    /**
     * 403
     *
     * @apiNote 没有权限,则会提示该消息
     */
    @GetMapping("/403")
    @Operation(summary = "403")
    public Result<?> noAuth() {
        return Result.error("没有权限,请联系管理员授权!");
    }
 
    /**
     * 全局默认异常返回数据
     */
    @GetMapping("/error")
    @Operation(summary = "全局默认异常返回数据")
    public Result<?> error() {
        return Result.error("默认异常错误,请联系管理员!");
    }
 
    /**
     * 文件上传
     *
     * @param biz 上传目录
     * @return 存储路径
     * @apiNote 上传到系统指定文件目录
     */
    @PostMapping("/upload")
    @Operation(summary = "文件上传")
    public Result<?> upload(@Parameter(name = "上传目录") @RequestParam(required = false) String biz,
                            @Parameter(name = "文件") @RequestParam(required = false) MultipartFile file) throws IOException {
        if (file == null) {
            return Result.error("没有上传文件!");
        }
        String dbPath = commonService.saveFileToLocal(file, biz);
        return Result.ok(dbPath);
    }
 
    /**
     * 静态资源访问
     */
    @GetMapping("/static/**")
    @Operation(summary = "静态资源访问")
    public void view(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
        String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
        String filePath = new AntPathMatcher().extractPathWithinPattern(bestMatchPattern, path);
        File file = commonService.viewStaticFile(filePath);
        if (file == null) {
            response.setStatus(404);
            response.setContentType("text/plain");
            response.setCharacterEncoding("UTF-8");
            IoUtil.write(response.getOutputStream(), true, ("该文件不存在!").getBytes(StandardCharsets.UTF_8));
            return;
        }
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        response.addHeader(Header.CONTENT_DISPOSITION.getValue(), "attachment;fileName=" + file.getName());
        FileUtil.writeToStream(file, response.getOutputStream());
    }
 
    /**
     * 获取枚举类的值列表
     *
     * @param enumClassPath 枚举类的类路径
     * @throws ClassNotFoundException 找不到这个类
     */
    @GetMapping("/enumValueList")
    @Operation(summary = "获取枚举类的值列表")
    public Result<?> getEnumValueList(@Parameter(name = "类路径") String enumClassPath) throws ClassNotFoundException {
        JSONArray dataArray = commonService.getEnumValueList(enumClassPath);
        return Result.ok(dataArray);
    }
}