sinake
8 天以前 4a2244dc29d72e2a994d2288e3731d29ecb7fead
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
package com.ruoyi.web.controller.common;
 
import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.project.domain.BaseAnnextype;
import com.ruoyi.project.domain.GiApi;
import com.ruoyi.project.domain.dto.ApiDTO;
import com.ruoyi.project.service.IApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
@Slf4j
@Api(description = "api服务")
@RestController
@RequestMapping("/api")
public class ApiController {
    @Autowired
    private IApiService  service;
    @ApiOperation("API->api查询列表")
    @PostMapping("/{path}")
    public AjaxResult getApiList(@PathVariable("path") String path, @RequestBody ApiDTO DTO) {
        List<Map<String, Object>> lists = new ArrayList<>();
        if (ObjectUtils.isNotEmpty(path)) {
            GiApi tempSql = service.getCode(path);
            if (tempSql != null && ObjectUtils.isNotEmpty(tempSql.getApiSql())) {
                String SQL_REG_EXP = ".*(\\b(insert|into|update|delete|trancate" +
                        "|drop|execute|grant|use)\\b).*";
                if (tempSql.getApiSql().toLowerCase().matches(SQL_REG_EXP)) {
                    return AjaxResult.error("sql中有非法字符,只应许select");
                }
                lists = service.getListSql(tempSql.getApiSql(), DTO);
                return AjaxResult.success(JSON.toJSON(lists));
            } else
                return AjaxResult.error("sql代码没有对应的sql请核对");
        } else {
            return AjaxResult.error("表名不能为空");
        }
    }
 
    @ApiOperation("API->api查询object")
    @PostMapping("/s/{path}")
    public AjaxResult getStringSql(@PathVariable("path") String path, @RequestBody ApiDTO DTO){
        List<Map<String,Object>> lists=new ArrayList<>();
        if(ObjectUtils.isNotEmpty(path)) {
            GiApi tempSql= service.getCode(path);
            if(tempSql!=null&&ObjectUtils.isNotEmpty(tempSql.getApiSql())) {
                String SQL_REG_EXP = ".*(\\b(insert|into|update|delete|trancate" +
                        "|drop|execute|grant|use)\\b).*";
                if (tempSql.getApiSql().toLowerCase().matches(SQL_REG_EXP)) {
                    return AjaxResult.error("sql中有非法字符,只应许select");
                }
 
                    return AjaxResult.success(service.getStringSql(tempSql.getApiSql(), DTO));
 
            }else
                return AjaxResult.error("sql代码没有对应的sql请核对");
        }else {
            return AjaxResult.error("表名不能为空");
        }
    }
 
}