diff --git a/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/controller/ResourceController.java b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/controller/ResourceController.java index 4c71ca14e..109934095 100644 --- a/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/controller/ResourceController.java +++ b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/controller/ResourceController.java @@ -8,16 +8,17 @@ import cn.iocoder.mall.admin.api.dto.ResourceAddDTO; import cn.iocoder.mall.admin.api.dto.ResourceUpdateDTO; import cn.iocoder.mall.admin.application.convert.ResourceConvert; import cn.iocoder.mall.admin.application.vo.AdminMenuTreeNodeVO; +import cn.iocoder.mall.admin.application.vo.ResourceTreeNodeVO; +import cn.iocoder.mall.admin.application.vo.ResourceVO; import cn.iocoder.mall.admin.sdk.context.AdminSecurityContextHolder; import com.alibaba.dubbo.config.annotation.Reference; import io.swagger.annotations.Api; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.*; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.stream.Collectors; @RestController @@ -30,6 +31,7 @@ public class ResourceController { // =========== 当前管理员相关的资源 API =========== + @SuppressWarnings("Duplicates") @GetMapping("/admin_menu_tree") @ApiOperation(value = "获得当前登陆的管理员拥有的菜单权限", notes = "以树结构返回") public CommonResult> adminMenuTree() { @@ -37,21 +39,22 @@ public class ResourceController { // 创建 AdminMenuTreeNodeVO Map Map treeNodeMap = resources.stream().collect(Collectors.toMap(ResourceBO::getId, ResourceConvert.INSTANCE::convert)); // 处理父子关系 - treeNodeMap.values().stream().filter(node -> { - return node.getPid() != 0; // TODO magic number - }).forEach((childNode) -> { - // 获得父节点 - AdminMenuTreeNodeVO parentNode = treeNodeMap.get(childNode.getPid()); - if (parentNode.getChildren() == null) { // 初始化 children 数组 - parentNode.setChildren(new ArrayList<>()); - } - // 将自己添加到父节点中 - parentNode.getChildren().add(childNode); - }); + treeNodeMap.values().stream() + .filter(node -> !node.getPid().equals(ResourceConstants.PID_ROOT)) + .forEach((childNode) -> { + // 获得父节点 + AdminMenuTreeNodeVO parentNode = treeNodeMap.get(childNode.getPid()); + if (parentNode.getChildren() == null) { // 初始化 children 数组 + parentNode.setChildren(new ArrayList<>()); + } + // 将自己添加到父节点中 + parentNode.getChildren().add(childNode); + }); // 获得到所有的根节点 - List rootNodes = treeNodeMap.values().stream().filter(node -> { - return node.getPid() == 0; // TODO magic number - }).collect(Collectors.toList()); + List rootNodes = treeNodeMap.values().stream() + .filter(node -> node.getPid().equals(ResourceConstants.PID_ROOT)) + .sorted(Comparator.comparing(AdminMenuTreeNodeVO::getSort)) + .collect(Collectors.toList()); return CommonResult.success(rootNodes); } @@ -65,10 +68,44 @@ public class ResourceController { // =========== 资源管理 API =========== - // TODO 芋艿,注释 + @SuppressWarnings("Duplicates") + @GetMapping("/tree") + @ApiOperation(value = "获得所有资源,按照树形结构返回") + public CommonResult> tree() { + List resources = resourceService.getResourcesByType(null); + // 创建 AdminMenuTreeNodeVO Map + Map treeNodeMap = resources.stream().collect(Collectors.toMap(ResourceBO::getId, ResourceConvert.INSTANCE::convert2)); + // 处理父子关系 + treeNodeMap.values().stream() + .filter(node -> !node.getPid().equals(ResourceConstants.PID_ROOT)) + .forEach((childNode) -> { + // 获得父节点 + ResourceTreeNodeVO parentNode = treeNodeMap.get(childNode.getPid()); + if (parentNode.getChildren() == null) { // 初始化 children 数组 + parentNode.setChildren(new ArrayList<>()); + } + // 将自己添加到父节点中 + parentNode.getChildren().add(childNode); + }); + // 获得到所有的根节点 + List rootNodes = treeNodeMap.values().stream() + .filter(node -> node.getPid().equals(ResourceConstants.PID_ROOT)) + .sorted(Comparator.comparing(ResourceTreeNodeVO::getSort)) + .collect(Collectors.toList()); + return CommonResult.success(rootNodes); + } + @PostMapping("/add") @ApiOperation(value = "创建资源", notes = "例如说,菜单资源,Url 资源") - public CommonResult add(@RequestParam("name") String name, + @ApiImplicitParams({ + @ApiImplicitParam(name = "name", value = "资源名字(标识)", required = true, example = "admin/info"), + @ApiImplicitParam(name = "type", value = "资源类型。1 代表【菜单】;2 代表【Url】", required = true, example = "1"), + @ApiImplicitParam(name = "sort", value = "排序", required = true, example = "1"), + @ApiImplicitParam(name = "displayName", value = "菜单展示名", required = true, example = "商品管理"), + @ApiImplicitParam(name = "pid", value = "父级资源编号", required = true, example = "1"), + @ApiImplicitParam(name = "handler", value = "操作", required = true, example = "/order/list"), + }) + public CommonResult add(@RequestParam("name") String name, @RequestParam("type") Integer type, @RequestParam("sort") Integer sort, @RequestParam("displayName") String displayName, @@ -76,10 +113,19 @@ public class ResourceController { @RequestParam("handler") String handler) { ResourceAddDTO resourceAddDTO = new ResourceAddDTO().setName(name).setType(type).setSort(sort) .setDisplayName(displayName).setPid(pid).setHandler(handler); - return resourceService.addResource(AdminSecurityContextHolder.getContext().getAdminId(), resourceAddDTO); + return ResourceConvert.INSTANCE.convert3(resourceService.addResource(AdminSecurityContextHolder.getContext().getAdminId(), resourceAddDTO)); } @PostMapping("/update") + @ApiOperation(value = "更新资源") + @ApiImplicitParams({ + @ApiImplicitParam(name = "id", value = "资源编号", required = true, example = "1"), + @ApiImplicitParam(name = "name", value = "资源名字(标识)", required = true, example = "admin/info"), + @ApiImplicitParam(name = "sort", value = "排序", required = true, example = "1"), + @ApiImplicitParam(name = "displayName", value = "菜单展示名", required = true, example = "商品管理"), + @ApiImplicitParam(name = "pid", value = "父级资源编号", required = true, example = "1"), + @ApiImplicitParam(name = "handler", value = "操作", required = true, example = "/order/list"), + }) public CommonResult update(@RequestParam("id") Integer id, @RequestParam("name") String name, @RequestParam("sort") Integer sort, @@ -91,6 +137,8 @@ public class ResourceController { } @PostMapping("/delete") + @ApiOperation(value = "删除资源") + @ApiImplicitParam(name = "id", value = "资源编号", required = true, example = "1") public CommonResult delete(@RequestParam("id") Integer id) { return resourceService.deleteResource(AdminSecurityContextHolder.getContext().getAdminId(), id); } diff --git a/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/convert/ResourceConvert.java b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/convert/ResourceConvert.java index 71ac94cc6..777e5b27a 100644 --- a/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/convert/ResourceConvert.java +++ b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/convert/ResourceConvert.java @@ -1,7 +1,10 @@ package cn.iocoder.mall.admin.application.convert; +import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.mall.admin.api.bo.ResourceBO; import cn.iocoder.mall.admin.application.vo.AdminMenuTreeNodeVO; +import cn.iocoder.mall.admin.application.vo.ResourceTreeNodeVO; +import cn.iocoder.mall.admin.application.vo.ResourceVO; import org.mapstruct.Mapper; import org.mapstruct.Mappings; import org.mapstruct.factory.Mappers; @@ -14,4 +17,13 @@ public interface ResourceConvert { @Mappings({}) AdminMenuTreeNodeVO convert(ResourceBO resourceBO); -} + @Mappings({}) + ResourceTreeNodeVO convert2(ResourceBO resourceBO); + + @Mappings({}) + ResourceVO convert3(ResourceBO resourceBO); + + @Mappings({}) + CommonResult convert3(CommonResult resourceBO); + +} \ No newline at end of file diff --git a/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/vo/AdminMenuTreeNodeVO.java b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/vo/AdminMenuTreeNodeVO.java index 1c9741570..d61304377 100644 --- a/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/vo/AdminMenuTreeNodeVO.java +++ b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/vo/AdminMenuTreeNodeVO.java @@ -10,13 +10,17 @@ public class AdminMenuTreeNodeVO { @ApiModelProperty(value = "菜单编号", required = true, example = "1") private Integer id; - @ApiModelProperty(value = "菜单名", required = true, example = "商品管理") - private String name; +// @ApiModelProperty(value = "菜单名", required = true, example = "商品管理") +// private String name; @ApiModelProperty(value = "菜单操作", required = true, example = "/order/list") private String handler; @ApiModelProperty(value = "父菜单编号", required = true, example = "1", notes = "如果无父菜单,则值为 0") private Integer pid; - @ApiModelProperty(value = "子节点数组", example = "[1, 2, 3]") + @ApiModelProperty(value = "排序", required = true, example = "1") + private Integer sort; + @ApiModelProperty(value = "菜单展示名", required = true, example = "商品管理") + private String displayName; + @ApiModelProperty(value = "子节点数组") private List children; public Integer getId() { @@ -28,15 +32,6 @@ public class AdminMenuTreeNodeVO { return this; } - public String getName() { - return name; - } - - public AdminMenuTreeNodeVO setName(String name) { - this.name = name; - return this; - } - public String getHandler() { return handler; } @@ -46,6 +41,33 @@ public class AdminMenuTreeNodeVO { return this; } + public Integer getPid() { + return pid; + } + + public AdminMenuTreeNodeVO setPid(Integer pid) { + this.pid = pid; + return this; + } + + public Integer getSort() { + return sort; + } + + public AdminMenuTreeNodeVO setSort(Integer sort) { + this.sort = sort; + return this; + } + + public String getDisplayName() { + return displayName; + } + + public AdminMenuTreeNodeVO setDisplayName(String displayName) { + this.displayName = displayName; + return this; + } + public List getChildren() { return children; } @@ -55,12 +77,4 @@ public class AdminMenuTreeNodeVO { return this; } - public Integer getPid() { - return pid; - } - - public AdminMenuTreeNodeVO setPid(Integer pid) { - this.pid = pid; - return this; - } -} +} \ No newline at end of file diff --git a/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/vo/ResourceTreeNodeVO.java b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/vo/ResourceTreeNodeVO.java new file mode 100644 index 000000000..a9499f87f --- /dev/null +++ b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/vo/ResourceTreeNodeVO.java @@ -0,0 +1,112 @@ +package cn.iocoder.mall.admin.application.vo; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +import java.util.Date; +import java.util.List; + +@ApiModel("资源树结构 VO") +public class ResourceTreeNodeVO { + + @ApiModelProperty(value = "资源编号", required = true, example = "1") + private Integer id; + @ApiModelProperty(value = "资源名字(标识)", required = true, example = "商品管理") + private String name; + @ApiModelProperty(value = "资源类型", required = true, example = "1") + private Integer type; + @ApiModelProperty(value = "排序", required = true, example = "1") + private Integer sort; + @ApiModelProperty(value = "菜单展示名", required = true, example = "商品管理") + private String displayName; + @ApiModelProperty(value = "创建时间", required = true, example = "时间戳格式") + private Date createTime; + @ApiModelProperty(value = "父级资源编号", required = true, example = "1", notes = "如果无父资源,则值为 0") + private Integer pid; + @ApiModelProperty(value = "操作", required = true, example = "/order/list") + private String handler; + @ApiModelProperty(value = "子节点数组") + private List children; + + public Integer getId() { + return id; + } + + public ResourceTreeNodeVO setId(Integer id) { + this.id = id; + return this; + } + + public String getName() { + return name; + } + + public ResourceTreeNodeVO setName(String name) { + this.name = name; + return this; + } + + public Integer getType() { + return type; + } + + public ResourceTreeNodeVO setType(Integer type) { + this.type = type; + return this; + } + + public Integer getSort() { + return sort; + } + + public ResourceTreeNodeVO setSort(Integer sort) { + this.sort = sort; + return this; + } + + public String getDisplayName() { + return displayName; + } + + public ResourceTreeNodeVO setDisplayName(String displayName) { + this.displayName = displayName; + return this; + } + + public Date getCreateTime() { + return createTime; + } + + public ResourceTreeNodeVO setCreateTime(Date createTime) { + this.createTime = createTime; + return this; + } + + public Integer getPid() { + return pid; + } + + public ResourceTreeNodeVO setPid(Integer pid) { + this.pid = pid; + return this; + } + + public String getHandler() { + return handler; + } + + public ResourceTreeNodeVO setHandler(String handler) { + this.handler = handler; + return this; + } + + public List getChildren() { + return children; + } + + public ResourceTreeNodeVO setChildren(List children) { + this.children = children; + return this; + } + +} \ No newline at end of file diff --git a/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/vo/ResourceVO.java b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/vo/ResourceVO.java new file mode 100644 index 000000000..cf056e2e3 --- /dev/null +++ b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/vo/ResourceVO.java @@ -0,0 +1,100 @@ +package cn.iocoder.mall.admin.application.vo; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +import java.util.Date; + +@ApiModel("资源 VO") +public class ResourceVO { + + @ApiModelProperty(value = "资源编号", required = true, example = "1") + private Integer id; + @ApiModelProperty(value = "资源名字(标识)", required = true, example = "商品管理") + private String name; + @ApiModelProperty(value = "资源类型", required = true, example = "1") + private Integer type; + @ApiModelProperty(value = "排序", required = true, example = "1") + private Integer sort; + @ApiModelProperty(value = "菜单展示名", required = true, example = "商品管理") + private String displayName; + @ApiModelProperty(value = "创建时间", required = true, example = "时间戳格式") + private Date createTime; + @ApiModelProperty(value = "父级资源编号", required = true, example = "1", notes = "如果无父资源,则值为 0") + private Integer pid; + @ApiModelProperty(value = "操作", required = true, example = "/order/list") + private String handler; + + public Integer getId() { + return id; + } + + public ResourceVO setId(Integer id) { + this.id = id; + return this; + } + + public String getName() { + return name; + } + + public ResourceVO setName(String name) { + this.name = name; + return this; + } + + public Integer getType() { + return type; + } + + public ResourceVO setType(Integer type) { + this.type = type; + return this; + } + + public Integer getSort() { + return sort; + } + + public ResourceVO setSort(Integer sort) { + this.sort = sort; + return this; + } + + public String getDisplayName() { + return displayName; + } + + public ResourceVO setDisplayName(String displayName) { + this.displayName = displayName; + return this; + } + + public Date getCreateTime() { + return createTime; + } + + public ResourceVO setCreateTime(Date createTime) { + this.createTime = createTime; + return this; + } + + public Integer getPid() { + return pid; + } + + public ResourceVO setPid(Integer pid) { + this.pid = pid; + return this; + } + + public String getHandler() { + return handler; + } + + public ResourceVO setHandler(String handler) { + this.handler = handler; + return this; + } + +} \ No newline at end of file diff --git a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/ResourceServiceImpl.java b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/ResourceServiceImpl.java index f437159a0..af2209db9 100644 --- a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/ResourceServiceImpl.java +++ b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/ResourceServiceImpl.java @@ -71,6 +71,8 @@ public class ResourceServiceImpl implements ResourceService { ResourceDO resource = ResourceConvert.INSTANCE.convert(resourceAddDTO); if (ResourceConstants.PID_ROOT.equals(resourceAddDTO.getPid())) { // 根节点,必须没有操作 resource.setHandler(null); + } else if (!resource.getHandler().startsWith("/")) { + resource.setHandler("/" + resource.getHandler()); } resource.setCreateTime(new Date()); resource.setDeleted(BaseDO.DELETED_NO); diff --git a/admin/admin-service-impl/src/main/resources/mapper/ResourceMapper.xml b/admin/admin-service-impl/src/main/resources/mapper/ResourceMapper.xml index 2637df1fa..04ccd24e9 100644 --- a/admin/admin-service-impl/src/main/resources/mapper/ResourceMapper.xml +++ b/admin/admin-service-impl/src/main/resources/mapper/ResourceMapper.xml @@ -29,8 +29,12 @@ SELECT FROM resource - WHERE type = #{type} - AND deleted = 0 + + + type = #{type} + + AND deleted = 0 +