李林
2024-02-28 780578502875aa0c339cb73f11b1e635df458c77
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
package com.yuanchu.mom.common;
 
import com.yuanchu.mom.annotation.ValueAuth;
import com.yuanchu.mom.mapper.AuthMapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
 
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Map;
 
@Component
public class AllController {
    @Resource
    WebApplicationContext applicationContext;
 
    @Resource
    private AuthMapper authMapper;
 
    public void addAllController() {
        RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
        Map<RequestMappingInfo, HandlerMethod> methodMap = mapping.getHandlerMethods();
        authMapper.deletePower();
        for (HandlerMethod value : methodMap.values()) {
            ApiOperation annotation = value.getMethodAnnotation(ApiOperation.class);
            ValueAuth valueAuth = value.getMethodAnnotation(ValueAuth.class);
            if (valueAuth==null){
                if (annotation != null) {
                    String remark = annotation.value();
                    String type = "修改";
                    if (remark.contains("获取")||remark.contains("查询")){
                        type = "查询";
                    } else if(remark.contains("删除")){
                        type = "删除";
                    } else if(remark.contains("添加")||remark.contains("新增")){
                        type = "添加";
                    } else if(remark.contains("导入")){
                        type = "导入";
                    } else if(remark.contains("导出")){
                        type = "导出";
                    }
                    try {
                        authMapper.insertPower(value.getMethod().getName(), remark, type);
                    }catch (Exception e){
                        System.err.println(value.getMethod());
                    }
                }
            }
        }
    }
 
    @PostConstruct
    public void pingStart() {
        addAllController();
    }
}