Commit 4119f87d authored by 赵啸非's avatar 赵啸非

添加资源扫描接口

parent 89ff7a09
...@@ -9,7 +9,9 @@ ...@@ -9,7 +9,9 @@
package com.mortals.xhx.base.system.resource.service; package com.mortals.xhx.base.system.resource.service;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException; import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.service.ICRUDService; import com.mortals.framework.service.ICRUDService;
import com.mortals.xhx.base.system.resource.model.ResourceEntity; import com.mortals.xhx.base.system.resource.model.ResourceEntity;
...@@ -63,4 +65,8 @@ public interface ResourceService extends ICRUDService<ResourceEntity,Long> { ...@@ -63,4 +65,8 @@ public interface ResourceService extends ICRUDService<ResourceEntity,Long> {
* @param userType * @param userType
*/ */
List<ResourceEntity> findAll(int userType); List<ResourceEntity> findAll(int userType);
Rest<String> refreshResourceUrl(String packageName, Context context);
} }
\ No newline at end of file
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
package com.mortals.xhx.base.system.resource.service.impl; package com.mortals.xhx.base.system.resource.service.impl;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException; import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context; import com.mortals.framework.model.Context;
import com.mortals.framework.service.ICacheService; import com.mortals.framework.service.ICacheService;
...@@ -23,14 +24,13 @@ import com.mortals.xhx.base.system.role.model.RoleAuthEntity; ...@@ -23,14 +24,13 @@ import com.mortals.xhx.base.system.role.model.RoleAuthEntity;
import com.mortals.xhx.base.system.role.model.RoleAuthQuery; import com.mortals.xhx.base.system.role.model.RoleAuthQuery;
import com.mortals.xhx.base.system.role.service.RoleAuthService; import com.mortals.xhx.base.system.role.service.RoleAuthService;
import com.mortals.xhx.common.key.RedisKey; import com.mortals.xhx.common.key.RedisKey;
import com.mortals.xhx.common.utils.ControllerScanUtil;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import java.util.Arrays; import java.util.*;
import java.util.HashSet; import java.util.stream.Collectors;
import java.util.List;
import java.util.Set;
import static com.mortals.xhx.common.utils.MenuEncodeUtil.generateMenuUrlCode; import static com.mortals.xhx.common.utils.MenuEncodeUtil.generateMenuUrlCode;
...@@ -39,6 +39,7 @@ import static com.mortals.xhx.common.utils.MenuEncodeUtil.generateMenuUrlCode; ...@@ -39,6 +39,7 @@ import static com.mortals.xhx.common.utils.MenuEncodeUtil.generateMenuUrlCode;
* <p>Description: ResourceServiceImpl service接口 </p> * <p>Description: ResourceServiceImpl service接口 </p>
* <p>Copyright: Copyright &reg; </p> * <p>Copyright: Copyright &reg; </p>
* <p>Company: </p> * <p>Company: </p>
*
* @author * @author
* @version 1.0.0 * @version 1.0.0
*/ */
...@@ -88,6 +89,74 @@ public class ResourceServiceImpl extends AbstractCRUDServiceImpl<ResourceDao, Re ...@@ -88,6 +89,74 @@ public class ResourceServiceImpl extends AbstractCRUDServiceImpl<ResourceDao, Re
return dao.getAll(userType); return dao.getAll(userType);
} }
@Override
public Rest<String> refreshResourceUrl(String packageName, Context context) {
List<Class<?>> classList = ControllerScanUtil.getAllClassByPackageName(packageName);
//System.out.println(classList); //获取到了所有的类
List<ResourceEntity> newResourcelist = ControllerScanUtil.getAnnotationInfo(classList);
Map<String, List<ResourceEntity>> localResourceMap = this.find(new ResourceQuery()).stream().collect(Collectors.groupingBy(x -> x.getName()));
Map<String, List<ResourceEntity>> newResourceMap = newResourcelist.stream().collect(Collectors.groupingBy(x -> x.getName()));
//更新 与新增 新加的;
newResourceMap.entrySet().forEach(item -> {
List<ResourceEntity> resourceEntities = item.getValue();
if (ObjectUtils.isEmpty(resourceEntities)) return;
if (resourceEntities.size() == 1) {
ResourceEntity resourceEntity = resourceEntities.get(0);
saveUpdateResourceEntity(context, localResourceMap, resourceEntity);
} else if (resourceEntities.size() > 1) {
//原始扫描 有多个资源列表针对一个名称的
for (ResourceEntity resourceEntity : resourceEntities) {
saveUpdateResourceEntity(context, localResourceMap, resourceEntity);
}
}
});
return Rest.ok();
}
private void saveUpdateResourceEntity(Context context, Map<String, List<ResourceEntity>> localResourceMap, ResourceEntity resourceEntity) {
//查找 本地是否已经存在了
List<ResourceEntity> tempResourceList = localResourceMap.getOrDefault(resourceEntity.getName(), new ArrayList<>());
if (tempResourceList.size() == 0) {
//新增 resource;
resourceEntity.setCreateUserId(this.getContextUserId(context));
resourceEntity.setCreateTime(new Date());
this.save(resourceEntity, context);
} else if (tempResourceList.size() == 1) {
//更新
ResourceEntity tempResource = tempResourceList.get(0);
Set<String> setUrl = Arrays.stream(resourceEntity.getUrl().split(",")).collect(Collectors.toSet());
Arrays.stream(tempResource.getUrl().split(",")).forEach(i -> {
setUrl.add(i);
});
resourceEntity.setUrl(setUrl.stream().collect(Collectors.joining(",")));
this.update(resourceEntity, context);
} else if (tempResourceList.size() > 1) {
//找到多个同名的 资源配置
for (ResourceEntity tempResource : tempResourceList) {
//模糊匹配到路径有一个存在的
Set<String> setUrl = Arrays.stream(resourceEntity.getUrl().split(",")).collect(Collectors.toSet());
String[] splitUrl = tempResource.getUrl().split(",");
Boolean bool = false;
for (int i = 0; i < splitUrl.length; i++) {
if (setUrl.contains(splitUrl[i])) {
bool = true;
break;
}
}
if (bool) {
//匹配到了,更新当前这个资源
Arrays.stream(tempResource.getUrl().split(",")).forEach(i -> {
setUrl.add(i);
});
resourceEntity.setUrl(setUrl.stream().collect(Collectors.joining(",")));
this.update(resourceEntity, context);
}
}
}
}
@Override @Override
protected void updateAfter(ResourceEntity entity, Context context) throws AppException { protected void updateAfter(ResourceEntity entity, Context context) throws AppException {
......
...@@ -2,7 +2,9 @@ package com.mortals.xhx.base.system.resource.web; ...@@ -2,7 +2,9 @@ package com.mortals.xhx.base.system.resource.web;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.common.IBaseEnum; import com.mortals.framework.common.IBaseEnum;
import com.mortals.framework.common.Rest;
import com.mortals.framework.common.code.UserType; import com.mortals.framework.common.code.UserType;
import com.mortals.framework.model.Context; import com.mortals.framework.model.Context;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController; import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
...@@ -13,6 +15,7 @@ import com.mortals.xhx.common.code.SourceType; ...@@ -13,6 +15,7 @@ import com.mortals.xhx.common.code.SourceType;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap; import java.util.HashMap;
...@@ -27,9 +30,9 @@ import java.util.Map; ...@@ -27,9 +30,9 @@ import java.util.Map;
@Slf4j @Slf4j
@RestController @RestController
@RequestMapping("resource") @RequestMapping("resource")
public class ResourceController extends BaseCRUDJsonBodyMappingController<ResourceService,ResourceEntity,Long> { public class ResourceController extends BaseCRUDJsonBodyMappingController<ResourceService, ResourceEntity, Long> {
public ResourceController(){ public ResourceController() {
super.setModuleDesc("资源信息"); super.setModuleDesc("资源信息");
} }
...@@ -60,4 +63,24 @@ public class ResourceController extends BaseCRUDJsonBodyMappingController<Resour ...@@ -60,4 +63,24 @@ public class ResourceController extends BaseCRUDJsonBodyMappingController<Resour
return ret.toJSONString(); return ret.toJSONString();
} }
/**
* 资源路径刷新
*/
@PostMapping(value = "refreshUrl")
@UnAuth
public Rest<String> refreshUrl(@RequestParam(name = "packageName", defaultValue = "com.mortals.xhx") String packageName) {
log.info("刷新资源路径,packageName", packageName);
String busiDesc = this.getModuleDesc() + "资源路径刷新";
Rest<String> rest = Rest.ok(busiDesc + " 【成功】");
try {
this.service.refreshResourceUrl(packageName, getContext());
recordSysLog(request, busiDesc + " 【成功】");
} catch (Exception e) {
log.error(busiDesc, e);
rest = Rest.fail(super.convertException(e));
}
return rest;
}
} }
\ No newline at end of file
...@@ -89,7 +89,7 @@ import static com.mortals.xhx.common.key.RedisKey.KEY_TOKEN_API_CACHE; ...@@ -89,7 +89,7 @@ import static com.mortals.xhx.common.key.RedisKey.KEY_TOKEN_API_CACHE;
*/ */
@RestController @RestController
@Slf4j @Slf4j
@RequestMapping("/api") @RequestMapping("api")
@Tag(name ="设备api") @Tag(name ="设备api")
public class DeviceApiController { public class DeviceApiController {
@Autowired @Autowired
......
...@@ -40,7 +40,7 @@ import static com.mortals.xhx.common.key.Constant.MESSAGETYPE_NOTIFY_REFRESH; ...@@ -40,7 +40,7 @@ import static com.mortals.xhx.common.key.Constant.MESSAGETYPE_NOTIFY_REFRESH;
@RestController @RestController
@Slf4j @Slf4j
@RequestMapping("/notify") @RequestMapping("notify")
public class DeviceSendMsgController { public class DeviceSendMsgController {
@Autowired @Autowired
private DeviceService deviceService; private DeviceService deviceService;
......
...@@ -40,7 +40,7 @@ import java.util.stream.Collectors; ...@@ -40,7 +40,7 @@ import java.util.stream.Collectors;
*/ */
@RestController @RestController
@Slf4j @Slf4j
@RequestMapping("/test") @RequestMapping("test")
public class TestSendMsgController { public class TestSendMsgController {
@Autowired @Autowired
protected ILogService logService = FileLogServiceImpl.getInstance(); protected ILogService logService = FileLogServiceImpl.getInstance();
......
package com.mortals.xhx.common.utils; package com.mortals.xhx.common.utils;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.annotation.UnAuth; import com.mortals.framework.annotation.UnAuth;
import com.mortals.xhx.base.system.resource.model.ResourceEntity; import com.mortals.xhx.base.system.resource.model.ResourceEntity;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
...@@ -66,24 +64,20 @@ public class ControllerScanUtil { ...@@ -66,24 +64,20 @@ public class ControllerScanUtil {
} catch (Exception e) { } catch (Exception e) {
} }
if (ObjectUtils.isEmpty(result)) {
boolean tagExits = cls.isAnnotationPresent(Tag.class);
if (tagExits) {
Tag annotation = cls.getAnnotation(Tag.class);
result = annotation.name();
} else {
//自定义的controller 没有名称
result = "自定义api模块" + RandomUtil.randomInt(100);
}
resourceViewEntity.setName(result + "-查看");
resourceEditEntity.setName(result + "-维护");
}
System.out.println(result);
RequestMapping requestMappingCls = cls.getAnnotation(RequestMapping.class); RequestMapping requestMappingCls = cls.getAnnotation(RequestMapping.class);
String prefix = ""; String prefix = "";
if (requestMappingCls != null) { if (requestMappingCls != null) {
prefix = "/" + requestMappingCls.value()[0]; prefix = "/" + requestMappingCls.value()[0];
} }
if (ObjectUtils.isEmpty(result)) {
if (requestMappingCls != null) {
result = requestMappingCls.value()[0].toUpperCase() + "接口模块";
}
resourceViewEntity.setName(result + "-查看");
resourceEditEntity.setName(result + "-维护");
}
Set<String> urlSet = new HashSet<>(); Set<String> urlSet = new HashSet<>();
//获取类中的所有的方法 //获取类中的所有的方法
Method[] methods = cls.getDeclaredMethods(); Method[] methods = cls.getDeclaredMethods();
...@@ -100,7 +94,6 @@ public class ControllerScanUtil { ...@@ -100,7 +94,6 @@ public class ControllerScanUtil {
String url = prefix + "/" + requestMapping.value()[0]; String url = prefix + "/" + requestMapping.value()[0];
url = url.replaceAll("/+", "/"); url = url.replaceAll("/+", "/");
urlSet.add(url); urlSet.add(url);
// System.out.println("/" + prefix + "/" + requestMapping.value()[0]);
} }
boolean mExits1 = method.isAnnotationPresent(GetMapping.class); boolean mExits1 = method.isAnnotationPresent(GetMapping.class);
...@@ -109,7 +102,6 @@ public class ControllerScanUtil { ...@@ -109,7 +102,6 @@ public class ControllerScanUtil {
String url = prefix + "/" + getMapping.value()[0]; String url = prefix + "/" + getMapping.value()[0];
url = url.replaceAll("/+", "/"); url = url.replaceAll("/+", "/");
urlSet.add(url); urlSet.add(url);
// System.out.println("/" + prefix + "/" + getMapping.value()[0]);
} }
boolean mExits2 = method.isAnnotationPresent(PostMapping.class); boolean mExits2 = method.isAnnotationPresent(PostMapping.class);
...@@ -118,7 +110,6 @@ public class ControllerScanUtil { ...@@ -118,7 +110,6 @@ public class ControllerScanUtil {
String url = prefix + "/" + postMapping.value()[0]; String url = prefix + "/" + postMapping.value()[0];
url = url.replaceAll("/+", "/"); url = url.replaceAll("/+", "/");
urlSet.add(url); urlSet.add(url);
// System.out.println("/" + prefix + "/" + postMapping.value()[0]);
} }
} }
} }
...@@ -127,19 +118,16 @@ public class ControllerScanUtil { ...@@ -127,19 +118,16 @@ public class ControllerScanUtil {
Method[] superMethods = cls.getSuperclass().getDeclaredMethods(); Method[] superMethods = cls.getSuperclass().getDeclaredMethods();
if (superMethods != null && superMethods.length > 0) { if (superMethods != null && superMethods.length > 0) {
for (Method method : superMethods) { for (Method method : superMethods) {
boolean unAuth = method.isAnnotationPresent(UnAuth.class); boolean unAuth = method.isAnnotationPresent(UnAuth.class);
if (unAuth) { if (unAuth) {
continue; continue;
} }
boolean mExits = method.isAnnotationPresent(RequestMapping.class); boolean mExits = method.isAnnotationPresent(RequestMapping.class);
if (mExits) { if (mExits) {
RequestMapping requestMapping = method.getAnnotation(RequestMapping.class); RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
String url = prefix + "/" + requestMapping.value()[0]; String url = prefix + "/" + requestMapping.value()[0];
url = url.replaceAll("/+", "/"); url = url.replaceAll("/+", "/");
urlSet.add(url); urlSet.add(url);
// System.out.println("super /" + prefix + "/" + requestMapping.value()[0]);
} }
boolean mExits1 = method.isAnnotationPresent(GetMapping.class); boolean mExits1 = method.isAnnotationPresent(GetMapping.class);
...@@ -148,7 +136,6 @@ public class ControllerScanUtil { ...@@ -148,7 +136,6 @@ public class ControllerScanUtil {
String url = prefix + "/" + getMapping.value()[0]; String url = prefix + "/" + getMapping.value()[0];
url = url.replaceAll("/+", "/"); url = url.replaceAll("/+", "/");
urlSet.add(url); urlSet.add(url);
//System.out.println("super /" + prefix + "/" + getMapping.value()[0]);
} }
boolean mExits2 = method.isAnnotationPresent(PostMapping.class); boolean mExits2 = method.isAnnotationPresent(PostMapping.class);
...@@ -157,7 +144,6 @@ public class ControllerScanUtil { ...@@ -157,7 +144,6 @@ public class ControllerScanUtil {
String url = prefix + "/" + postMapping.value()[0]; String url = prefix + "/" + postMapping.value()[0];
url = url.replaceAll("/+", "/"); url = url.replaceAll("/+", "/");
urlSet.add(url); urlSet.add(url);
// System.out.println("super /" + prefix + "/" + postMapping.value()[0]);
} }
} }
} }
...@@ -196,8 +182,6 @@ public class ControllerScanUtil { ...@@ -196,8 +182,6 @@ public class ControllerScanUtil {
} }
public static List<Class<?>> getAllClassByPackageName(String packageName) { public static List<Class<?>> getAllClassByPackageName(String packageName) {
// String packageName = pkg.getName();
// 获取当前包下以及子包下所以的类 // 获取当前包下以及子包下所以的类
List<Class<?>> returnClassList = getClasses(packageName); List<Class<?>> returnClassList = getClasses(packageName);
return returnClassList; return returnClassList;
......
...@@ -54,6 +54,10 @@ Accept: application/json ...@@ -54,6 +54,10 @@ Accept: application/json
GET {{baseUrl}}/test/one/49 GET {{baseUrl}}/test/one/49
Accept: application/json Accept: application/json
###controoler 测试
POST {{baseUrl}}/resource/refreshUrl
Accept: application/json
###站点树 测试 ###站点树 测试
GET {{baseUrl}}/sitestat/siteTree GET {{baseUrl}}/sitestat/siteTree
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment