【resource】整理resource模块的字段和业务

pull/3/head
fengshuonan 2020-12-19 17:22:03 +08:00
parent 20e7581c98
commit e54482b4ad
8 changed files with 93 additions and 118 deletions

View File

@ -20,6 +20,16 @@ public class ResourceDefinition implements Serializable {
*/
private String appCode;
/**
*
*/
private String resourceCode;
/**
*
*/
private String resourceName;
/**
* 使spring.application.name
* <p>
@ -50,14 +60,9 @@ public class ResourceDefinition implements Serializable {
private String modularName;
/**
*
* ip
*/
private String code;
/**
*
*/
private String name;
private String ipAddress;
/**
*
@ -69,30 +74,15 @@ public class ResourceDefinition implements Serializable {
*/
private String httpMethod;
/**
* true-false-
*/
private Boolean menuFlag;
/**
*
*/
private Boolean requiredLogin;
private Boolean requiredLoginFlag;
/**
*
*/
private Boolean requiredPermission;
/**
*
*/
private Date createTime;
/**
* ip
*/
private String ipAddress;
private Boolean requiredPermissionFlag;
/**
*
@ -102,11 +92,16 @@ public class ResourceDefinition implements Serializable {
/**
*
*/
private Set<FieldMetadata> paramFieldMetadata;
private Set<FieldMetadata> paramFieldDescriptions;
/**
*
*/
private Set<FieldMetadata> responseFieldMetadata;
private Set<FieldMetadata> responseFieldDescriptions;
/**
*
*/
private Date createTime;
}

View File

@ -206,9 +206,9 @@ public class ApiResourceScanner implements BeanPostProcessor {
// 如果没有填写code则用"模块名称_方法名称"为默认的标识
String code = invokeAnnotationMethod(apiResource, "code", String.class);
if (StrUtil.isEmpty(code)) {
resourceDefinition.setCode(resourceDefinition.getAppCode() + scannerProperties.getLinkSymbol() + StrUtil.toUnderlineCase(modular) + scannerProperties.getLinkSymbol() + StrUtil.toUnderlineCase(method.getName()));
resourceDefinition.setResourceCode(resourceDefinition.getAppCode() + scannerProperties.getLinkSymbol() + StrUtil.toUnderlineCase(modular) + scannerProperties.getLinkSymbol() + StrUtil.toUnderlineCase(method.getName()));
} else {
resourceDefinition.setCode(resourceDefinition.getAppCode() + scannerProperties.getLinkSymbol() + StrUtil.toUnderlineCase(modular) + scannerProperties.getLinkSymbol() + StrUtil.toUnderlineCase(code));
resourceDefinition.setResourceCode(resourceDefinition.getAppCode() + scannerProperties.getLinkSymbol() + StrUtil.toUnderlineCase(modular) + scannerProperties.getLinkSymbol() + StrUtil.toUnderlineCase(code));
}
// 填充其他属性
@ -219,10 +219,9 @@ public class ApiResourceScanner implements BeanPostProcessor {
Boolean requiredLogin = invokeAnnotationMethod(apiResource, "requiredLogin", Boolean.class);
Boolean requiredPermission = invokeAnnotationMethod(apiResource, "requiredPermission", Boolean.class);
resourceDefinition.setRequiredLogin(requiredLogin);
resourceDefinition.setRequiredPermission(requiredPermission);
resourceDefinition.setMenuFlag(menuFlag);
resourceDefinition.setName(name);
resourceDefinition.setRequiredLoginFlag(requiredLogin);
resourceDefinition.setRequiredPermissionFlag(requiredPermission);
resourceDefinition.setResourceName(name);
resourceDefinition.setUrl(getControllerClassRequestPath(clazz, path[0]));
StringBuilder methodNames = new StringBuilder();
for (RequestMethod requestMethod : requestMethods) {
@ -257,15 +256,15 @@ public class ApiResourceScanner implements BeanPostProcessor {
// @ApiResource注解上标识了responseClass属性则用responseClass的值为准否则按真实方法的返回值class
Class<?> responseClass = invokeAnnotationMethod(apiResource, "responseClass", Class.class);
if (!Void.class.equals(responseClass)) {
resourceDefinition.setResponseFieldMetadata(ClassReflectUtil.getClassFieldDescription(responseClass));
resourceDefinition.setResponseFieldDescriptions(ClassReflectUtil.getClassFieldDescription(responseClass));
} else {
Class<?> methodReturnClass = MethodReflectUtil.getMethodReturnClass(method);
resourceDefinition.setResponseFieldMetadata(ClassReflectUtil.getClassFieldDescription(methodReturnClass));
resourceDefinition.setResponseFieldDescriptions(ClassReflectUtil.getClassFieldDescription(methodReturnClass));
}
// 填充方法的请求参数字段的详细信息
Class<?> firstParamClass = MethodReflectUtil.getMethodFirstParamClass(method);
resourceDefinition.setParamFieldMetadata(ClassReflectUtil.getClassFieldDescription(firstParamClass));
resourceDefinition.setParamFieldDescriptions(ClassReflectUtil.getClassFieldDescription(firstParamClass));
return resourceDefinition;
}

View File

@ -39,25 +39,25 @@ public class DefaultResourceCollector implements ResourceCollectorApi {
public void collectResources(List<ResourceDefinition> apiResource) {
if (apiResource != null && apiResource.size() > 0) {
for (ResourceDefinition resourceDefinition : apiResource) {
ResourceDefinition alreadyFlag = resourceDefinitions.get(resourceDefinition.getCode());
ResourceDefinition alreadyFlag = resourceDefinitions.get(resourceDefinition.getResourceCode());
if (alreadyFlag != null) {
throw new RuntimeException("资源扫描过程中存在重复资源!\n已存在资源" + alreadyFlag + "\n新资源为 " + resourceDefinition);
}
resourceDefinitions.put(resourceDefinition.getCode(), resourceDefinition);
resourceDefinitions.put(resourceDefinition.getResourceCode(), resourceDefinition);
urlDefineResources.put(resourceDefinition.getUrl(), resourceDefinition);
// 存储模块资源
Map<String, ResourceDefinition> modularResources = modularResourceDefinitions.get(StrUtil.toUnderlineCase(resourceDefinition.getModularCode()));
if (modularResources == null) {
modularResources = new HashMap<>();
modularResources.put(resourceDefinition.getCode(), resourceDefinition);
modularResources.put(resourceDefinition.getResourceCode(), resourceDefinition);
modularResourceDefinitions.put(StrUtil.toUnderlineCase(resourceDefinition.getModularCode()), modularResources);
} else {
modularResources.put(resourceDefinition.getCode(), resourceDefinition);
modularResources.put(resourceDefinition.getResourceCode(), resourceDefinition);
}
// 添加资源code-中文名称字典
this.bindResourceName(resourceDefinition.getCode(), resourceDefinition.getName());
this.bindResourceName(resourceDefinition.getResourceCode(), resourceDefinition.getResourceName());
}
}
}

View File

@ -2,13 +2,17 @@ package cn.stylefeng.roses.kernel.resource.modular.controller;
import cn.stylefeng.roses.kernel.resource.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.resource.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.resource.api.pojo.resource.ResourceDefinition;
import cn.stylefeng.roses.kernel.resource.modular.entity.SysResource;
import cn.stylefeng.roses.kernel.resource.modular.pojo.ResourceTreeNode;
import cn.stylefeng.roses.kernel.resource.modular.service.SysResourceService;
import cn.stylefeng.roses.kernel.rule.pojo.request.BaseRequest;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.system.pojo.resource.request.ResourceRequest;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@ -50,4 +54,28 @@ public class ResourceController {
return new SuccessResponseData(menuResourceList);
}
/**
*
*
* @author fengshuonan
* @date 2020/12/18 15:50
*/
@GetResource(name = "获取资源树列表,用于接口文档页面", path = "/resource/getTree", requiredLogin = false, requiredPermission = false)
public ResponseData getTree() {
List<ResourceTreeNode> resourceTree = sysResourceService.getResourceTree();
return new SuccessResponseData(resourceTree);
}
/**
*
*
* @author fengshuonan
* @date 2020/12/18 15:50
*/
@GetResource(name = "获取接口详情", path = "/resource/getDetail", requiredLogin = false, requiredPermission = false)
public ResponseData getResourceDetail(@Validated(BaseRequest.detail.class) ResourceRequest resourceRequest) {
ResourceDefinition resourceDetail = sysResourceService.getResourceDetail(resourceRequest);
return new SuccessResponseData(resourceDetail);
}
}

View File

@ -21,8 +21,8 @@ public class SysResource extends BaseEntity {
/**
* id
*/
@TableId("id")
private String id;
@TableId("resource_id")
private Long resourceId;
/**
*
@ -33,14 +33,14 @@ public class SysResource extends BaseEntity {
/**
*
*/
@TableField("code")
private String code;
@TableField("resource_code")
private String resourceCode;
/**
*
*/
@TableField("name")
private String name;
@TableField("resource_name")
private String resourceName;
/**
*
@ -91,19 +91,13 @@ public class SysResource extends BaseEntity {
private String httpMethod;
/**
* (Y-N-API)
*/
@TableField("menu_flag")
private String menuFlag;
/**
* (Y-N-)
* Y-N-
*/
@TableField("required_login_flag")
private String requiredLoginFlag;
/**
* (Y-N-)
* Y-N-
*/
@TableField("required_permission_flag")
private String requiredPermissionFlag;

View File

@ -30,21 +30,15 @@ public class ResourceFactory {
public static SysResource createResource(ResourceDefinition resourceDefinition) {
SysResource resource = new SysResource();
BeanUtils.copyProperties(resourceDefinition, resource);
resource.setId(resourceDefinition.getCode());
resource.setResourceCode(resourceDefinition.getResourceCode());
if (resourceDefinition.getMenuFlag()) {
resource.setMenuFlag(YesOrNotEnum.Y.name());
} else {
resource.setMenuFlag(YesOrNotEnum.N.name());
}
if (resourceDefinition.getRequiredLogin()) {
if (resourceDefinition.getRequiredLoginFlag()) {
resource.setRequiredLoginFlag(YesOrNotEnum.Y.name());
} else {
resource.setRequiredLoginFlag(YesOrNotEnum.N.name());
}
if (resourceDefinition.getRequiredPermission()) {
if (resourceDefinition.getRequiredPermissionFlag()) {
resource.setRequiredPermissionFlag(YesOrNotEnum.Y.name());
} else {
resource.setRequiredPermissionFlag(YesOrNotEnum.N.name());
@ -56,13 +50,13 @@ public class ResourceFactory {
}
// 转化接口参数的字段描述
if (ObjectUtil.isNotEmpty(resourceDefinition.getParamFieldMetadata())) {
resource.setParamFieldDescriptions(JSON.toJSONString(resourceDefinition.getParamFieldMetadata(), SerializerFeature.WriteClassName));
if (ObjectUtil.isNotEmpty(resourceDefinition.getParamFieldDescriptions())) {
resource.setParamFieldDescriptions(JSON.toJSONString(resourceDefinition.getParamFieldDescriptions(), SerializerFeature.WriteClassName));
}
// 转化接口返回结果的字段描述
if (ObjectUtil.isNotEmpty(resourceDefinition.getResponseFieldMetadata())) {
resource.setResponseFieldDescriptions(JSON.toJSONString(resourceDefinition.getResponseFieldMetadata(), SerializerFeature.WriteClassName));
if (ObjectUtil.isNotEmpty(resourceDefinition.getResponseFieldDescriptions())) {
resource.setResponseFieldDescriptions(JSON.toJSONString(resourceDefinition.getResponseFieldDescriptions(), SerializerFeature.WriteClassName));
}
return resource;
@ -82,10 +76,10 @@ public class ResourceFactory {
BeanUtil.copyProperties(sysResource, resourceDefinition, CopyOptions.create().ignoreError());
// 设置是否需要登录标识Y为需要登录
resourceDefinition.setRequiredLogin(YesOrNotEnum.Y.name().equals(sysResource.getRequiredLoginFlag()));
resourceDefinition.setRequiredLoginFlag(YesOrNotEnum.Y.name().equals(sysResource.getRequiredLoginFlag()));
// 设置是否需要权限认证标识Y为需要权限认证
resourceDefinition.setRequiredPermission(YesOrNotEnum.Y.name().equals(sysResource.getRequiredPermissionFlag()));
resourceDefinition.setRequiredPermissionFlag(YesOrNotEnum.Y.name().equals(sysResource.getRequiredPermissionFlag()));
// 转化校验组
if (ObjectUtil.isNotEmpty(sysResource.getValidateGroups())) {
@ -94,12 +88,12 @@ public class ResourceFactory {
// 转化接口参数的字段描述
if (ObjectUtil.isNotEmpty(sysResource.getParamFieldDescriptions())) {
resourceDefinition.setParamFieldMetadata(JSON.parseObject(sysResource.getParamFieldDescriptions(), Set.class, Feature.SupportAutoType));
resourceDefinition.setParamFieldDescriptions(JSON.parseObject(sysResource.getParamFieldDescriptions(), Set.class, Feature.SupportAutoType));
}
// 转化接口返回结果的字段描述
if (ObjectUtil.isNotEmpty(sysResource.getResponseFieldDescriptions())) {
resourceDefinition.setResponseFieldMetadata(JSON.parseObject(sysResource.getResponseFieldDescriptions(), Set.class, Feature.SupportAutoType));
resourceDefinition.setResponseFieldDescriptions(JSON.parseObject(sysResource.getResponseFieldDescriptions(), Set.class, Feature.SupportAutoType));
}
return resourceDefinition;

View File

@ -2,27 +2,4 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.stylefeng.roses.kernel.resource.modular.mapper.SysResourceMapper">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="cn.stylefeng.roses.kernel.resource.modular.entity.SysResource">
<id column="id" property="id"/>
<result column="app_code" property="appCode"/>
<result column="code" property="code"/>
<result column="name" property="name"/>
<result column="project_code" property="projectCode"/>
<result column="class_name" property="className"/>
<result column="method_name" property="methodName"/>
<result column="modular_code" property="modularCode"/>
<result column="modular_name" property="modularName"/>
<result column="ip_address" property="ipAddress"/>
<result column="url" property="url"/>
<result column="http_method" property="httpMethod"/>
<result column="menu_flag" property="menuFlag"/>
<result column="required_login_flag" property="requiredLoginFlag"/>
<result column="required_permission_flag" property="requiredPermissionFlag"/>
<result column="create_user" property="createUser"/>
<result column="create_time" property="createTime"/>
<result column="update_user" property="updateUser"/>
<result column="update_time" property="updateTime"/>
</resultMap>
</mapper>

View File

@ -60,18 +60,15 @@ public class SysResourceServiceImpl extends ServiceImpl<SysResourceMapper, SysRe
LambdaQueryWrapper<SysResource> wrapper = createWrapper(resourceRequest);
// 查询为菜单的
wrapper.eq(SysResource::getMenuFlag, YesOrNotEnum.Y.getCode());
// 只查询code和name
wrapper.select(SysResource::getCode, SysResource::getName);
wrapper.select(SysResource::getResourceCode, SysResource::getResourceName);
List<SysResource> menuResourceList = this.list(wrapper);
// 增加返回虚拟菜单的情况
SysResource sysResource = new SysResource();
sysResource.setCode("");
sysResource.setName("虚拟目录(空)");
sysResource.setResourceCode("");
sysResource.setResourceName("虚拟目录(空)");
menuResourceList.add(0, sysResource);
return menuResourceList;
@ -89,7 +86,7 @@ public class SysResourceServiceImpl extends ServiceImpl<SysResourceMapper, SysRe
// 1. 获取所有的资源
LambdaQueryWrapper<SysResource> sysResourceLambdaQueryWrapper = new LambdaQueryWrapper<>();
sysResourceLambdaQueryWrapper.select(SysResource::getAppCode, SysResource::getModularCode, SysResource::getModularName, SysResource::getCode, SysResource::getUrl, SysResource::getName);
sysResourceLambdaQueryWrapper.select(SysResource::getAppCode, SysResource::getModularCode, SysResource::getModularName, SysResource::getResourceCode, SysResource::getUrl, SysResource::getResourceName);
List<SysResource> allResource = this.list(sysResourceLambdaQueryWrapper);
// 2. 按应用和模块编码设置map
@ -102,7 +99,7 @@ public class SysResourceServiceImpl extends ServiceImpl<SysResourceMapper, SysRe
@Override
public ResourceDefinition getResourceDetail(ResourceRequest resourceRequest) {
LambdaQueryWrapper<SysResource> sysResourceLambdaQueryWrapper = new LambdaQueryWrapper<>();
sysResourceLambdaQueryWrapper.eq(SysResource::getCode, resourceRequest.getResourceCode());
sysResourceLambdaQueryWrapper.eq(SysResource::getResourceCode, resourceRequest.getResourceCode());
SysResource sysResource = this.getOne(sysResourceLambdaQueryWrapper);
if (sysResource != null) {
return ResourceFactory.createResourceDefinition(sysResource);
@ -161,15 +158,11 @@ public class SysResourceServiceImpl extends ServiceImpl<SysResourceMapper, SysRe
// 获取是否需要登录的标记, 判断是否需要登录如果是则设置为true,否则为false
String requiredLoginFlag = resource.getRequiredLoginFlag();
resourceDefinition.setRequiredLogin(YesOrNotEnum.Y.name().equals(requiredLoginFlag));
resourceDefinition.setRequiredLoginFlag(YesOrNotEnum.Y.name().equals(requiredLoginFlag));
// 获取请求权限的标记判断是否有权限如果有则设置为true,否则为false
String requiredPermissionFlag = resource.getRequiredPermissionFlag();
resourceDefinition.setRequiredPermission(YesOrNotEnum.Y.name().equals(requiredPermissionFlag));
// 获取是否是菜单的flag如果是则设置为true,否则为false
String menuFlag = resource.getMenuFlag();
resourceDefinition.setMenuFlag(YesOrNotEnum.Y.name().equals(menuFlag));
resourceDefinition.setRequiredPermissionFlag(YesOrNotEnum.Y.name().equals(requiredPermissionFlag));
return resourceDefinition;
}
@ -187,7 +180,7 @@ public class SysResourceServiceImpl extends ServiceImpl<SysResourceMapper, SysRe
// 拼接in条件
LambdaQueryWrapper<SysResource> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(SysResource::getId, resourceIds);
queryWrapper.in(SysResource::getResourceId, resourceIds);
// 获取资源详情
List<SysResource> list = this.list(queryWrapper);
@ -210,7 +203,7 @@ public class SysResourceServiceImpl extends ServiceImpl<SysResourceMapper, SysRe
// 拼接in条件
LambdaQueryWrapper<SysResource> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(SysResource::getId, resourceIds);
queryWrapper.in(SysResource::getResourceId, resourceIds);
queryWrapper.select(SysResource::getUrl);
// 获取资源详情
@ -235,12 +228,7 @@ public class SysResourceServiceImpl extends ServiceImpl<SysResourceMapper, SysRe
// 根据资源名称
if (ObjectUtil.isNotEmpty(resourceRequest.getResourceName())) {
queryWrapper.like(SysResource::getName, resourceRequest.getResourceName());
}
// 根据是否是菜单查询
if (ObjectUtil.isNotEmpty(resourceRequest.getMenuFlag())) {
queryWrapper.like(SysResource::getMenuFlag, resourceRequest.getMenuFlag());
queryWrapper.like(SysResource::getResourceName, resourceRequest.getResourceName());
}
}
@ -276,8 +264,8 @@ public class SysResourceServiceImpl extends ServiceImpl<SysResourceMapper, SysRe
// 将当前资源放入资源集合
ResourceTreeNode resourceTreeNode = new ResourceTreeNode();
resourceTreeNode.setResourceFlag(true);
resourceTreeNode.setNodeName(sysResource.getUrl() + "(" + sysResource.getName() + ")");
resourceTreeNode.setCode(sysResource.getCode());
resourceTreeNode.setNodeName(sysResource.getUrl() + "(" + sysResource.getResourceName() + ")");
resourceTreeNode.setCode(sysResource.getResourceCode());
resourceTreeNode.setParentCode(sysResource.getModularCode());
resourceTreeNodes.add(resourceTreeNode);