diff --git a/pom.xml b/pom.xml index e42488c..b2e4dac 100644 --- a/pom.xml +++ b/pom.xml @@ -34,7 +34,7 @@ 5.8.11 - 1.33.0 + 1.34.0 8.0.17 diff --git a/src/main/java/com/ydool/common/constant/UrlConstant.java b/src/main/java/com/ydool/common/constant/UrlConstant.java index acdf8a2..6d531f8 100644 --- a/src/main/java/com/ydool/common/constant/UrlConstant.java +++ b/src/main/java/com/ydool/common/constant/UrlConstant.java @@ -21,6 +21,9 @@ public interface UrlConstant { String DICT_DATA = API + "/dict_data"; + + String MENU = API + "/menu"; + String PERSON = API + "/person"; String COMPANY = API + "/company"; @@ -38,8 +41,7 @@ public interface UrlConstant { * 在实际的应用中,一定要为不同的项目或服务,自行生成公钥和私钥,并将 PRIVATE_KEY 的引用改为服务的配置项。 * 密钥的生成方式,com.ydool.common.util.RsaUtil类的main函数动态生成。 */ - String APP_PRIVATE_KEY = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAJwq/96CJLuyjMrcctY0lNrlTIvUikj0bXjmTrY6srwzGA9HiC6saPnwOryrwTqAy/QTCnKW/EeepyJl61r4wlITU0sPbCY7ugPbD2g3cowHPpofb4RJOQfV+EPlLOnDrrYbCP8Z6YhD6P6nltfRxL0kyP/NGX7CUaiMiXZshTMVAgMBAAECgYA6PO7ek7oPJS7MGBnbOBCq8QfyvXg0bLLGe1yvKISvGn7Gc+6dOT6MtsGwynxszMtCYjdrNFMwgkHh2hBDBLGyRM/C1orRpcpCK4CdJNI4GAF/jtIJ1lnrHp11Tt7ng0wGSC00i/oIBIFVRbRhk04SWtLb0C3KaiL9CpBFlFCkAQJBAMm01Opv5V18EZkA7G0Jn0axqNDqKb0OiiXPzzYJ1q3H+VaKYBulQ2V2vlwaO+VJ2any0zbtJrNN+ChiJXO3xzUCQQDGNDXj0NgzAPFGouGhhgjieMCIk1dGfcUT3tdEljqH6XxzgpgGk9WaDcKGi94gcBpw+k4S2K/2Z8BWm0o4VthhAkBElm30DfqTcFXXjj3W0ffMJSs/ZvCsbd5B/0j5mhza2O4LJGCL5gVl4hvW28DUNemC5aQ2wCtxuyiQKteLYsmlAkA9MBES635TAI97xvzw6HgFslF92D6xPFKC9+BtBBO9sY8B+ecE4M6oaGcNMlC4ouMGzb8cgliQMSdnlTkse2MBAkBpy+wkyUvJzLtBq2IPM5vlw9Oxn/6yP2YyBG5AP1dHFDwz3q/pkBcp/ZSmH2FgjXOvjFdSsNdRnOtNiLpFO19l"; - + String APP_PRIVATE_KEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAL5VDvlBzzvFZzMiLPLx83IH65f/1tQs8GMXfHAMDTsEczogqkt6W3KQ0G9B2L7Kvp470GHgYf+reTHL65Oblt90CogYdr4ulm0/K9+iMrdbmGu4B5qSMVzs6E+up6OOgdwp+zqItQ2fies5j2twqYcxJKxdv4xEN9ApBFwu8XcxAgMBAAECgYBwpd8K30xLz/XYaa24zEKNhuqFN8JqKX7M5yWbG0bpWyTnjlW7/lsJX3Q9pPcnVM+kzznx89+MdmxwcISpBDkyWh7MOVg3Y3F3ChSO6B/Gap33YeinYcUGn2PQ/q6yh4Wpj5BuGkSo7sMPeGuM+NrO0KwLOa39XJ+jkMNsFt3D0QJBAOFiyPl5BAW8tEdQJTfhyXhKhsU894BN7V5kfAzDd7/oQmKY6lIzgizhp3DVLOav6js6nLiQ3PO0m3TTgKpS0LsCQQDYL1+1+CbnMDE5Cth0MspI98O89j27Ira6ZuPMjjs8gGh0qPLRj5hBcqiA0hMAuSxQylQ/TAxAWWMcvb+LoT8DAkEA0Y8u/D5XzhnJqXDMWiwWTkFFE8j++w35wUU+zgNEqWuDpfc2D54S0DYzoEalTAyOhemWZElVF0d7L2GkBTmhFwJASzCe72Fx1nLyi85z3mZT6PETyUMz0USPnWFv1ZOakr5uljS0z6JL6GeCp9to9JVzDl5w0tsWX8M3L6XnWx8oFQJBAKPr9X/v9o/qpIg6wsJvtWVpjXMWdFxFDIXQpbmS5ovwjnIjSh2ExGnwzOexO3oZGGCOdz7+6eshliVXYMTDjw8="; } diff --git a/src/main/java/com/ydool/common/gen/Generator.java b/src/main/java/com/ydool/common/gen/Generator.java index d1aae59..daa5261 100644 --- a/src/main/java/com/ydool/common/gen/Generator.java +++ b/src/main/java/com/ydool/common/gen/Generator.java @@ -27,7 +27,7 @@ public class Generator { public static void main(String[] args) { //表名 - String tableName = "t_sys_dict_data"; + String tableName = "t_sys_menu"; //表前缀 String tablePrefix = "t_sys_"; //作者— diff --git a/src/main/java/com/ydool/system/controller/MenuController.java b/src/main/java/com/ydool/system/controller/MenuController.java new file mode 100644 index 0000000..4f8c7e4 --- /dev/null +++ b/src/main/java/com/ydool/system/controller/MenuController.java @@ -0,0 +1,141 @@ +package com.ydool.system.controller; + + +import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; +import com.github.xiaoymin.knife4j.annotations.ApiSupport; +import com.github.xiaoymin.knife4j.annotations.DynamicParameter; +import com.github.xiaoymin.knife4j.annotations.DynamicResponseParameters; +import com.ydool.common.base.BaseController; +import com.ydool.common.constant.UrlConstant; +import com.ydool.common.data.dto.AjaxResult; +import com.ydool.common.data.dto.OptionResult; +import com.ydool.system.entity.Menu; +import com.ydool.system.request.MenuRequest; +import com.ydool.system.service.IMenuService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiImplicitParams; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping(UrlConstant.MENU) +@Api(tags = "菜单", value = "菜单") +@ApiSupport(order = 8) +public class MenuController extends BaseController { + + @Autowired + private IMenuService menuService; + + /** + * 菜单列表 + * + * @param value + * @param column + * @param orderBy + * @return + */ + @GetMapping(value = "list") + @ApiOperation(value = "菜单列表(树形结构不带分页功能)") + @ApiImplicitParams({ + @ApiImplicitParam(name = "value", value = "搜索框中的内容(只支持字符串类型)"), + @ApiImplicitParam(name = "column", value = "排序字段 字段名"), + @ApiImplicitParam(name = "orderBy", value = "排序顺序 降序:desc,升序:asc"), + @ApiImplicitParam(name = "status", value = "启用状态", dataType = "Boolean"), + @ApiImplicitParam(name = "visible", value = "是否隐藏", dataType = "Boolean"), + }) + @ApiOperationSupport( + responses = @DynamicResponseParameters(properties = { + @DynamicParameter(name = "data", dataTypeClass = Menu.class) + }), order = 1 + ) + public AjaxResult menuList(String value, + String column, + String orderBy, + Boolean visible, + Boolean status) { + return menuService.menuList(value, column, orderBy, visible, status); + } + + + /** + * 菜单详情 + * + * @param id 菜单ID + * @return + */ + @GetMapping(value = "detail") + @ApiOperation(value = "菜单详情") + @ApiOperationSupport( + responses = @DynamicResponseParameters(properties = { + @DynamicParameter(name = "data", dataTypeClass = Menu.class) + }), order = 2 + ) + @ApiImplicitParam(name = "id", value = "菜单Id") + public AjaxResult menuDetail(String id) { + return AjaxResult.ok().data(menuService.menuDetail(id)); + } + + + /** + * 保存菜单 + * + * @param menuRequest + * @return + */ + @PostMapping(value = "save") + @ApiOperation(value = "保存菜单") + @ApiOperationSupport(order = 3) + public AjaxResult saveMenu(@Validated @RequestBody MenuRequest menuRequest) { + return menuService.saveMenu(menuRequest); + } + + /** + * 编辑菜单 + * + * @param id + * @param menuRequest + * @return + */ + @PutMapping(value = "edit") + @ApiOperation(value = "编辑菜单") + @ApiImplicitParam(name = "id", value = "菜单Id") + @ApiOperationSupport(order = 4) + public AjaxResult editMenu(String id, @Validated @RequestBody MenuRequest menuRequest) { + return menuService.editMenu(id, menuRequest); + } + + /** + * 删除菜单 + * + * @param id + * @return + */ + @DeleteMapping(value = "remove") + @ApiOperation(value = "删除菜单") + @ApiImplicitParam(name = "id", value = "菜单Id") + @ApiOperationSupport(order = 5) + public AjaxResult removeMenu(String id) { + return menuService.removeMenu(id); + } + + + /** + * 菜单树 + * + * @return + */ + @GetMapping("tree") + @ApiOperation("菜单树") + @ApiOperationSupport( + responses = @DynamicResponseParameters(properties = { + @DynamicParameter(name = "data", dataTypeClass = OptionResult.class) + }), order = 6 + ) + public AjaxResult menuTree() { + return AjaxResult.ok().data(menuService.menuTree()); + } + +} diff --git a/src/main/java/com/ydool/system/controller/RoleController.java b/src/main/java/com/ydool/system/controller/RoleController.java index e6a8806..5d6eea9 100644 --- a/src/main/java/com/ydool/system/controller/RoleController.java +++ b/src/main/java/com/ydool/system/controller/RoleController.java @@ -10,6 +10,7 @@ import com.ydool.common.data.dto.AjaxResult; import com.ydool.system.entity.Config; import com.ydool.system.entity.Role; import com.ydool.system.request.IdsRequest; +import com.ydool.system.request.RoleMenuRequest; import com.ydool.system.request.RoleRequest; import com.ydool.system.service.IRoleService; import io.swagger.annotations.Api; @@ -20,6 +21,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; +import java.util.List; + @RestController @RequestMapping(UrlConstant.ROLE) @Api(tags = "角色", value = "角色") @@ -134,4 +137,35 @@ public class RoleController extends BaseController { public AjaxResult removeConfigs(@RequestBody @Validated IdsRequest ids) { return roleService.removeRoles(ids); } + + /** + * 角色权限 + * + * @param id + * @return + */ + @GetMapping(value = "roleMenuList") + @ApiOperation(value = "角色权限") + @ApiOperationSupport( + responses = @DynamicResponseParameters(properties = { + @DynamicParameter(name = "data", dataTypeClass = List.class) + }), order = 7 + ) + @ApiImplicitParam(name = "id", value = "角色Id") + public AjaxResult roleMenuList(String id) { + return roleService.roleMenuList(id); + } + + /** + * 权限分配 + * @param roleMenuRequest + * @return + */ + @PutMapping(value = "editRoleMenu") + @ApiOperation(value = "权限分配") + @ApiOperationSupport(order = 8) + public AjaxResult editRoleMenu(@RequestBody @Validated RoleMenuRequest roleMenuRequest) { + return roleService.editRoleMenu(roleMenuRequest); + } + } diff --git a/src/main/java/com/ydool/system/entity/Menu.java b/src/main/java/com/ydool/system/entity/Menu.java new file mode 100644 index 0000000..0cf013b --- /dev/null +++ b/src/main/java/com/ydool/system/entity/Menu.java @@ -0,0 +1,59 @@ +package com.ydool.system.entity; + +import com.baomidou.mybatisplus.annotation.TableName; +import com.ydool.common.base.TreeEntity; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +/** + *

+ * 菜单权限表 + *

+ * + * @author ljq + * @since 2023-02-02 + */ +@Data +@TableName("t_sys_menu") +@ApiModel(value = "Menu对象", description = "菜单权限表") +public class Menu extends TreeEntity { + + private static final long serialVersionUID = 1L; + + + @ApiModelProperty(value = "菜单名称") + private String menuName; + + @ApiModelProperty(value = "路由地址") + private String path; + + @ApiModelProperty(value = "组件路径") + private String component; + + @ApiModelProperty(value = "菜单类型(M目录 C菜单 F按钮 O外链)") + private String menuType; + + @ApiModelProperty(value = "是否隐藏(0显示 1隐藏)") + private Boolean visible; + + @ApiModelProperty(value = "启用状态(0正常 1停用)") + private Boolean status; + + @ApiModelProperty(value = "权限标识") + private String perms; + + @ApiModelProperty(value = "菜单图标") + private String icon; + + @ApiModelProperty(value = "排序") + private Integer sortNo; + + @ApiModelProperty(value = "备注") + private String remarks; + + @Override + public String getNamed() { + return this.menuName; + } +} diff --git a/src/main/java/com/ydool/system/mapper/MenuMapper.java b/src/main/java/com/ydool/system/mapper/MenuMapper.java new file mode 100644 index 0000000..e98eab4 --- /dev/null +++ b/src/main/java/com/ydool/system/mapper/MenuMapper.java @@ -0,0 +1,18 @@ +package com.ydool.system.mapper; + +import com.ydool.system.entity.Menu; +import com.github.yulichang.base.MPJBaseMapper; +import org.apache.ibatis.annotations.Mapper; + +/** + *

+ * 菜单权限表 Mapper 接口 + *

+ * + * @author ljq + * @since 2023-02-02 + */ +@Mapper +public interface MenuMapper extends MPJBaseMapper { + +} diff --git a/src/main/java/com/ydool/system/request/MenuRequest.java b/src/main/java/com/ydool/system/request/MenuRequest.java new file mode 100644 index 0000000..34e8891 --- /dev/null +++ b/src/main/java/com/ydool/system/request/MenuRequest.java @@ -0,0 +1,50 @@ +package com.ydool.system.request; + +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; + +@Data +public class MenuRequest { + @ApiModelProperty(value = "上级菜单id") + @NotBlank(message = "上级菜单id不能为空") + private String pid; + + @ApiModelProperty(value = "菜单名称") + @NotBlank(message = "菜单名称不能为空") + private String menuName; + + @ApiModelProperty(value = "路由地址") + private String path; + + @ApiModelProperty(value = "组件路径") + private String component; + + @ApiModelProperty(value = "菜单类型(M目录 C菜单 F按钮 O外链)") + @NotBlank(message = "菜单类型不能为空") + private String menuType; + + @ApiModelProperty(value = "是否隐藏(0显示 1隐藏)") + @NotNull(message = "是否隐藏不能为空") + private Boolean visible; + + @ApiModelProperty(value = "启用状态(0正常 1停用)") + @NotNull(message = "启用状态不能为空") + private Boolean status; + + @ApiModelProperty(value = "权限标识") + @NotBlank(message = "权限标识不能为空") + private String perms; + + @ApiModelProperty(value = "菜单图标") + private String icon; + + @ApiModelProperty(value = "排序") + @NotNull(message = "排序不能为空") + private Integer sortNo; + + @ApiModelProperty(value = "备注") + private String remarks; +} diff --git a/src/main/java/com/ydool/system/request/RoleMenuRequest.java b/src/main/java/com/ydool/system/request/RoleMenuRequest.java new file mode 100644 index 0000000..fdca6cf --- /dev/null +++ b/src/main/java/com/ydool/system/request/RoleMenuRequest.java @@ -0,0 +1,16 @@ +package com.ydool.system.request; + +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import javax.validation.constraints.NotBlank; +import java.util.List; + +@Data +public class RoleMenuRequest { + @ApiModelProperty(value = "角色Id") + @NotBlank(message = "角色Id不能为空") + private String roleId; + @ApiModelProperty(value = "菜单Id集合字符串") + private String menuIds; +} diff --git a/src/main/java/com/ydool/system/service/IMenuService.java b/src/main/java/com/ydool/system/service/IMenuService.java new file mode 100644 index 0000000..07e0e74 --- /dev/null +++ b/src/main/java/com/ydool/system/service/IMenuService.java @@ -0,0 +1,63 @@ +package com.ydool.system.service; + +import com.ydool.common.data.dto.AjaxResult; +import com.ydool.system.request.MenuRequest; + +/** + *

+ * 菜单权限表 服务类 + *

+ * + * @author ljq + * @since 2023-02-02 + */ +public interface IMenuService { + /** + * 保存菜单 + * @param menuRequest + * @return + */ + AjaxResult saveMenu(MenuRequest menuRequest); + + /** + * 编辑菜单 + * @param id + * @param menuRequest + * @return + */ + AjaxResult editMenu(String id,MenuRequest menuRequest); + + /** + * 删除菜单 + * @param id + * @return + */ + AjaxResult removeMenu(String id); + + /** + * 菜单详情 + * @param id + * @return + */ + AjaxResult menuDetail(String id); + + /** + * 菜单列表 + * @param value + * @param column + * @param orderBy + * @param visible + * @param status + * @return + */ + AjaxResult menuList(String value, String column, String orderBy,Boolean visible,Boolean status); + + /** + * 菜单树 + * @return + */ + AjaxResult menuTree(); + + +} + diff --git a/src/main/java/com/ydool/system/service/IRoleService.java b/src/main/java/com/ydool/system/service/IRoleService.java index ea9428d..ea9fbe0 100644 --- a/src/main/java/com/ydool/system/service/IRoleService.java +++ b/src/main/java/com/ydool/system/service/IRoleService.java @@ -3,6 +3,7 @@ package com.ydool.system.service; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.ydool.common.data.dto.AjaxResult; import com.ydool.system.request.IdsRequest; +import com.ydool.system.request.RoleMenuRequest; import com.ydool.system.request.RoleRequest; /** @@ -66,6 +67,21 @@ public interface IRoleService { AjaxResult roleList(Page page, String value, String column, String orderBy, Boolean status); + /** + * 查询角色拥有的权限 + * @param id + * @return + */ + AjaxResult roleMenuList(String id); + + /** + * 权限分配 + * @param roleMenuRequest + * @return + */ + AjaxResult editRoleMenu(RoleMenuRequest roleMenuRequest); + + } diff --git a/src/main/java/com/ydool/system/service/impl/MenuServiceImpl.java b/src/main/java/com/ydool/system/service/impl/MenuServiceImpl.java new file mode 100644 index 0000000..5fcbfab --- /dev/null +++ b/src/main/java/com/ydool/system/service/impl/MenuServiceImpl.java @@ -0,0 +1,242 @@ +package com.ydool.system.service.impl; + +import cn.hutool.core.bean.BeanUtil; +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.StrUtil; +import cn.hutool.json.JSONArray; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.ydool.common.base.BaseTreeService; +import com.ydool.common.data.dto.AjaxResult; +import com.ydool.common.data.dto.OptionResult; +import com.ydool.common.utils.TreeMapUtil; +import com.ydool.common.utils.TreeSorterUtil; +import com.ydool.system.entity.Menu; +import com.ydool.system.mapper.MenuMapper; +import com.ydool.system.request.MenuRequest; +import com.ydool.system.service.IMenuService; +import org.springframework.stereotype.Service; + +import java.util.*; + +/** + *

+ * 菜单权限表 服务类 + *

+ * + * @author ljq + * @since 2023-02-02 + */ +@Service +public class MenuServiceImpl extends BaseTreeService implements IMenuService { + + /** + * 保存菜单 + * + * @param menuRequest + * @return + */ + @Override + public AjaxResult saveMenu(MenuRequest menuRequest) { + String message = checkMenu("", menuRequest); + if (StrUtil.isNotBlank(message)) return AjaxResult.fail(message); + Menu menu = BeanUtil.copyProperties(menuRequest, Menu.class); + + boolean flag = save(menu); + return flag ? AjaxResult.ok().msg("保存成功") : AjaxResult.fail("保存失败"); + } + + /** + * 编辑菜单 + * + * @param id + * @param menuRequest + * @return + */ + @Override + public AjaxResult editMenu(String id, MenuRequest menuRequest) { + if (StrUtil.isBlank(id)) return AjaxResult.fail("id不能为空"); + Menu menu = getById(id); + if (ObjectUtil.isNull(menu)) return AjaxResult.fail("该菜单不存在"); + String message = checkMenu(id, menuRequest); + if (StrUtil.isNotBlank(message)) return AjaxResult.fail(message); + BeanUtil.copyProperties(menuRequest, menu); + + boolean flag = updateById(menu); + return flag ? AjaxResult.ok().msg("编辑成功") : AjaxResult.fail("编辑失败"); + } + + /** + * 删除菜单 + * + * @param id + * @return + */ + @Override + public AjaxResult removeMenu(String id) { + if (StrUtil.isBlank(id)) return AjaxResult.fail("id不能为空"); + Menu menu = getById(id); + if (ObjectUtil.isNull(menu)) return AjaxResult.fail("该菜单不存在"); + int count = count(new QueryWrapper().lambda().eq(Menu::getPid, menu.getId())); + if (count > 0) return AjaxResult.fail("该菜单下还有子节点不允许删除"); + return removeById(id) ? AjaxResult.ok().msg("删除成功") : AjaxResult.fail("删除失败"); + } + + /** + * 菜单详情 + * + * @param id + * @return + */ + @Override + public AjaxResult menuDetail(String id) { + if (StrUtil.isBlank(id)) return AjaxResult.fail("id不能为空"); + Menu menu = getById(id); + if (ObjectUtil.isNull(menu)) return AjaxResult.fail("该菜单不存在"); + return AjaxResult.ok().data(menu); + } + + /** + * 菜单列表 + * + * @param value + * @param column + * @param orderBy + * @param visible + * @param status + * @return + */ + @Override + public AjaxResult menuList(String value, String column, String orderBy, Boolean visible, Boolean status) { + //获取符合条件的菜单 + QueryWrapper qw = new QueryWrapper(); + if (StrUtil.isNotBlank(value)) { + qw.lambda().like(Menu::getMenuName, value) + .or().like(Menu::getRemarks, value) + .or().like(Menu::getPath, value) + .or().like(Menu::getComponent, value); + } + qw.lambda().eq(ObjectUtil.isNotNull(visible), Menu::getVisible, visible) + .eq(ObjectUtil.isNotNull(status), Menu::getStatus, status); + + if ("desc".equals(orderBy) && StrUtil.isNotBlank(column)) { + qw.orderByDesc(StrUtil.toUnderlineCase(column)); + } + + if ("asc".equals(orderBy) && StrUtil.isNotBlank(column)) { + qw.orderByAsc(StrUtil.toUnderlineCase(column)); + } + + List> menuMap = listMaps(qw); + + + //树结构下搜索,如果子级符合条件,就把父级也放进去,确保其能显示 + Iterator> iterator = menuMap.iterator(); + List> parentList = new ArrayList<>(); + + + QueryWrapper wrapper = new QueryWrapper(); + + if ("desc".equals(orderBy) && StrUtil.isNotBlank(column)) { + wrapper.orderByDesc(StrUtil.toUnderlineCase(column)); + } + + if ("asc".equals(orderBy) && StrUtil.isNotBlank(column)) { + wrapper.orderByAsc(StrUtil.toUnderlineCase(column)); + } + + //获取所有菜单 + List> allMenuMap = listMaps(wrapper); + Map allMap = new HashMap(); + + allMenuMap.forEach(item -> { + HashMap map = (HashMap) item; + allMap.put(map.get("id").toString(), map); + }); + + while (iterator.hasNext()) { + Map map = (HashMap) iterator.next(); + String pid = map.get("pid").toString(); + if (!"0".equals(pid)) { + for (; ; ) { + Map pMap = (HashMap) allMap.get(pid); + if (pMap != null && !menuMap.contains(pMap) && !parentList.contains(pMap)) { + parentList.add(pMap); + pid = pMap.get("pid").toString(); + } else { + break; + } + } + } + } + menuMap.addAll(parentList); + TreeMapUtil treeMapUtil = new TreeMapUtil(); + treeMapUtil.tree(menuMap); + //转换为集合对象 + JSONArray jsonArray = new JSONArray(); + jsonArray.addAll(menuMap); + List menus = jsonArray.toList(Menu.class); + return AjaxResult.ok().data(menus); + } + + /** + * 菜单树 + * + * @return + */ + @Override + public AjaxResult menuTree() { + QueryWrapper qw = new QueryWrapper(); + qw.lambda().eq(Menu::getStatus, true).eq(Menu::getVisible, false).orderByAsc(Menu::getSortNo); + List treeList = list(qw); + TreeSorterUtil.tree(treeList); + List optionResultList = TreeSorterUtil.optionTree(treeList); + return AjaxResult.ok().data(optionResultList); + } + + /** + * 校验菜单 + * + * @param id + * @param menuRequest + * @return + */ + private String checkMenu(String id, MenuRequest menuRequest) { + if ("M".equals(menuRequest.getMenuType())) { + if (StrUtil.isBlank(menuRequest.getPath())) return "目录路由路径不能为空"; + } + + if ("C".equals(menuRequest.getMenuType())) { + if (StrUtil.isBlank(menuRequest.getPath())) return "菜单路由路径不能为空"; + if (StrUtil.isBlank(menuRequest.getComponent())) return "菜单组件路径不能为空"; + } + + + if ("O".equals(menuRequest.getMenuType())) { + if (StrUtil.isBlank(menuRequest.getPath())) return "外链路由路径不能为空"; + } + + Menu menu = getOne(new QueryWrapper().lambda() + .eq(Menu::getPath, menuRequest.getPath()) + .ne(StrUtil.isNotBlank(id), Menu::getId, id) + .last("limit 1") + ); + if (ObjectUtil.isNotNull(menu)) return "路由路径不能重复"; + + menu = getOne(new QueryWrapper().lambda() + .eq(Menu::getComponent, menuRequest.getComponent()) + .ne(StrUtil.isNotBlank(id), Menu::getId, id) + .last("limit 1") + ); + if (ObjectUtil.isNotNull(menu)) return "组件路径不能重复"; + + menu = getOne(new QueryWrapper().lambda() + .eq(Menu::getPerms, menuRequest.getPerms()) + .ne(StrUtil.isNotBlank(id), Menu::getId, id) + .last("limit 1") + ); + if (ObjectUtil.isNotNull(menu)) return "权限字符不能重复"; + + return null; + } +} + diff --git a/src/main/java/com/ydool/system/service/impl/RoleServiceImpl.java b/src/main/java/com/ydool/system/service/impl/RoleServiceImpl.java index 332bb73..3736251 100644 --- a/src/main/java/com/ydool/system/service/impl/RoleServiceImpl.java +++ b/src/main/java/com/ydool/system/service/impl/RoleServiceImpl.java @@ -10,10 +10,13 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.ydool.common.base.BaseService; import com.ydool.common.data.dto.AjaxResult; import com.ydool.system.entity.Role; +import com.ydool.system.mapper.MenuMapper; import com.ydool.system.mapper.RoleMapper; import com.ydool.system.request.IdsRequest; +import com.ydool.system.request.RoleMenuRequest; import com.ydool.system.request.RoleRequest; import com.ydool.system.service.IRoleService; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Arrays; @@ -30,6 +33,9 @@ import java.util.List; @Service public class RoleServiceImpl extends BaseService implements IRoleService { + @Autowired + private MenuMapper menuMapper; + /** * 保存角色 * @@ -112,6 +118,7 @@ public class RoleServiceImpl extends BaseService implements IR /** * 角色列表 + * * @param page * @param value * @param column @@ -143,6 +150,39 @@ public class RoleServiceImpl extends BaseService implements IR return AjaxResult.ok().data(pageList); } + /** + * 查询角色拥有的权限 + * + * @param id + * @return + */ + @Override + public AjaxResult roleMenuList(String id) { + if (StrUtil.isBlank(id)) return AjaxResult.fail("id不能为空"); + Role role = getById(id); + if (ObjectUtil.isNull(role)) return AjaxResult.fail("该角色不存在"); + if (StrUtil.isNotBlank(role.getMenus())) { + List menuId = Arrays.asList(role.getMenus().split(",")); + return AjaxResult.ok().data(menuId); + } + return AjaxResult.ok(); + } + + /** + * 权限分配 + * + * @param roleMenuRequest + * @return + */ + @Override + public AjaxResult editRoleMenu(RoleMenuRequest roleMenuRequest) { + Role role = getById(roleMenuRequest.getRoleId()); + if (ObjectUtil.isNull(role)) return AjaxResult.fail("该角色不存在"); + role.setMenus(roleMenuRequest.getMenuIds()); + boolean flag = updateById(role); + return flag ? AjaxResult.ok().msg("权限分配成功") : AjaxResult.fail("权限分配失败"); + } + /** * 校验角色 *