资源列表 API 提供

This commit is contained in:
YunaiV 2019-03-01 00:34:30 +08:00
parent a88afbf9c0
commit 3b97b58df7
7 changed files with 337 additions and 45 deletions

View File

@ -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<List<AdminMenuTreeNodeVO>> adminMenuTree() {
@ -37,9 +39,9 @@ public class ResourceController {
// 创建 AdminMenuTreeNodeVO Map
Map<Integer, AdminMenuTreeNodeVO> 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) -> {
treeNodeMap.values().stream()
.filter(node -> !node.getPid().equals(ResourceConstants.PID_ROOT))
.forEach((childNode) -> {
// 获得父节点
AdminMenuTreeNodeVO parentNode = treeNodeMap.get(childNode.getPid());
if (parentNode.getChildren() == null) { // 初始化 children 数组
@ -49,9 +51,10 @@ public class ResourceController {
parentNode.getChildren().add(childNode);
});
// 获得到所有的根节点
List<AdminMenuTreeNodeVO> rootNodes = treeNodeMap.values().stream().filter(node -> {
return node.getPid() == 0; // TODO magic number
}).collect(Collectors.toList());
List<AdminMenuTreeNodeVO> 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<List<ResourceTreeNodeVO>> tree() {
List<ResourceBO> resources = resourceService.getResourcesByType(null);
// 创建 AdminMenuTreeNodeVO Map
Map<Integer, ResourceTreeNodeVO> 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<ResourceTreeNodeVO> 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<ResourceBO> 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<ResourceVO> 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<Boolean> 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<Boolean> delete(@RequestParam("id") Integer id) {
return resourceService.deleteResource(AdminSecurityContextHolder.getContext().getAdminId(), id);
}

View File

@ -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<ResourceVO> convert3(CommonResult<ResourceBO> resourceBO);
}

View File

@ -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<AdminMenuTreeNodeVO> 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<AdminMenuTreeNodeVO> 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;
}
}

View File

@ -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<ResourceTreeNodeVO> 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<ResourceTreeNodeVO> getChildren() {
return children;
}
public ResourceTreeNodeVO setChildren(List<ResourceTreeNodeVO> children) {
this.children = children;
return this;
}
}

View File

@ -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;
}
}

View File

@ -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);

View File

@ -29,8 +29,12 @@
SELECT
<include refid="FIELDS"/>
FROM resource
WHERE type = #{type}
<where>
<if test="type != null">
type = #{type}
</if>
AND deleted = 0
</where>
</select>
<select id="selectListByTypeAndRoleIds" resultType="ResourceDO">