package com.yuanchu.mom.common; import com.yuanchu.mom.annotation.ValueAuth; import com.yuanchu.mom.annotation.ValueClassify; import com.yuanchu.mom.mapper.AuthMapper; import io.swagger.annotations.ApiOperation; import org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration; 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.lang.reflect.Method; 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 methodMap = mapping.getHandlerMethods(); authMapper.deletePower(); for (HandlerMethod value : methodMap.values()) { ApiOperation annotation = value.getMethodAnnotation(ApiOperation.class); ValueAuth valueAuth = value.getMethodAnnotation(ValueAuth.class); ValueClassify valueClassify = value.getMethodAnnotation(ValueClassify.class); if (valueAuth == null) { if (valueClassify !=null) { String type = valueClassify.value(); String remark = annotation.value(); authMapper.insertPower(value.getMethod().getName(),remark,type); }else{ 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("新增") || 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()); e.printStackTrace(); } } } } } System.out.println("********************权限菜单完成********************"); } @PostConstruct public void pingStart() { addAllController(); } }