diff --git a/src/main/java/com/cm/serviceusercenter/controller/apis/system/role/RoleController.java b/src/main/java/com/cm/serviceusercenter/controller/apis/system/role/RoleController.java index d482064..aa1b5f8 100644 --- a/src/main/java/com/cm/serviceusercenter/controller/apis/system/role/RoleController.java +++ b/src/main/java/com/cm/serviceusercenter/controller/apis/system/role/RoleController.java @@ -172,8 +172,6 @@ public class RoleController extends BaseController { @PathVariable("roleId") String roleId, @PathVariable("type") String type) throws SearchException, ParamsException { Map params = requestParams(); - params.put("menuParentId", menuParentId); - params.put("roleId", roleId); if (!IRoleService.AUTH_TYPE_INSERT.equals(type) && !IRoleService.AUTH_TYPE_DELETE.equals(type) && !IRoleService.AUTH_TYPE_UPDATE.equals(type) && @@ -181,11 +179,7 @@ public class RoleController extends BaseController { !IRoleService.AUTH_TYPE_MENU.equals(type)) { throw new ParamsException("授权类型错误"); } - params.put("type", type); - if (IRoleService.AUTH_TYPE_MENU.equals(type)) { - params.put("menuType", 1); - } - return menuService.listMenuAllJson(params); + return menuService.listMenuByRoleIdAndType(roleId, type); } @ApiOperation(value = "执行授权", notes = "执行授权接口") diff --git a/src/main/java/com/cm/serviceusercenter/controller/resources/system/menu/MenuResourceController.java b/src/main/java/com/cm/serviceusercenter/controller/resources/system/menu/MenuResourceController.java index 9ca4d5c..5ea6717 100644 --- a/src/main/java/com/cm/serviceusercenter/controller/resources/system/menu/MenuResourceController.java +++ b/src/main/java/com/cm/serviceusercenter/controller/resources/system/menu/MenuResourceController.java @@ -53,7 +53,7 @@ public class MenuResourceController extends BaseController { @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) @GetMapping("listmenubyclientid/{clientId}/{userId}") public Callable>> listMenuByClientId(@PathVariable("clientId") String clientId, @PathVariable("userId") String userId, HttpServletRequest httpServletRequest) throws Exception { - return () -> menuService.listMenuByClientIdAndUserId(clientId, userId); + return () -> new SuccessResultData<>(menuService.listMenuByClientIdAndUserId(clientId, userId)); } } diff --git a/src/main/java/com/cm/serviceusercenter/pojo/dtos/MenuDTO.java b/src/main/java/com/cm/serviceusercenter/pojo/dtos/MenuDTO.java index c47facc..f286cbb 100644 --- a/src/main/java/com/cm/serviceusercenter/pojo/dtos/MenuDTO.java +++ b/src/main/java/com/cm/serviceusercenter/pojo/dtos/MenuDTO.java @@ -77,7 +77,7 @@ public class MenuDTO implements Serializable { } public Boolean getParent() { - return isParent; + return isParent == null ? false : isParent; } public void setParent(Boolean parent) { @@ -173,7 +173,7 @@ public class MenuDTO implements Serializable { } public Boolean getRight() { - return isRight; + return isRight == null ? false : isRight; } public void setRight(Boolean right) { diff --git a/src/main/java/com/cm/serviceusercenter/service/system/menu/IMenuService.java b/src/main/java/com/cm/serviceusercenter/service/system/menu/IMenuService.java index ffc933a..4d3bbb4 100644 --- a/src/main/java/com/cm/serviceusercenter/service/system/menu/IMenuService.java +++ b/src/main/java/com/cm/serviceusercenter/service/system/menu/IMenuService.java @@ -121,7 +121,16 @@ public interface IMenuService { * @return * @throws SearchException */ - SuccessResultData> listMenuByClientIdAndUserId(String clientId, String userId) throws Exception; + List listMenuByClientIdAndUserId(String clientId, String userId) throws Exception; + + /** + * 通过角色和类型获取菜单列表 + * + * @param roleId + * @param type + * @return + */ + List listMenuByRoleIdAndType(String roleId, String type); /** * 通过用户获取菜单 @@ -131,4 +140,5 @@ public interface IMenuService { * @throws SearchException */ List listMenuIdByUser(Map params) throws SearchException; + } diff --git a/src/main/java/com/cm/serviceusercenter/service/system/menu/impl/MenuServiceImpl.java b/src/main/java/com/cm/serviceusercenter/service/system/menu/impl/MenuServiceImpl.java index 9c31663..48b49e7 100644 --- a/src/main/java/com/cm/serviceusercenter/service/system/menu/impl/MenuServiceImpl.java +++ b/src/main/java/com/cm/serviceusercenter/service/system/menu/impl/MenuServiceImpl.java @@ -1,7 +1,6 @@ package com.cm.serviceusercenter.service.system.menu.impl; import com.cm.common.component.SecurityComponent; -import com.cm.common.constants.ISystemConstant; import com.cm.common.exception.RemoveException; import com.cm.common.exception.SaveException; import com.cm.common.exception.SearchException; @@ -12,12 +11,12 @@ import com.cm.common.pojo.dtos.ZTreeDTO; import com.cm.common.result.SuccessResult; import com.cm.common.result.SuccessResultData; import com.cm.common.result.SuccessResultList; +import com.cm.common.utils.ArrayListUtil; import com.cm.common.utils.UUIDUtil; import com.cm.serviceusercenter.dao.system.menu.IMenuDao; import com.cm.serviceusercenter.dao.system.role.IRoleDao; import com.cm.serviceusercenter.pojo.dtos.MenuDTO; import com.cm.serviceusercenter.pojo.dtos.OauthClientDTO; -import com.cm.serviceusercenter.pojo.pos.UserPO; import com.cm.serviceusercenter.service.BaseService; import com.cm.serviceusercenter.service.system.menu.IMenuService; import com.cm.serviceusercenter.service.system.oauthclient.IOauthClientService; @@ -149,7 +148,7 @@ public class MenuServiceImpl extends BaseService implements IMenuService { } @Override - public SuccessResultData> listMenuByClientIdAndUserId(String clientId, String userId) throws Exception { + public List listMenuByClientIdAndUserId(String clientId, String userId) throws Exception { Map params = new HashMap<>(1); params.put("clientId", clientId); OauthClientDTO oauthClientDTO = oauthClientService.getOauthClient(params); @@ -158,24 +157,127 @@ public class MenuServiceImpl extends BaseService implements IMenuService { } String menuId = oauthClientDTO.getMenuId(); if (StringUtils.isBlank(menuId)) { - return new SuccessResultData<>(new ArrayList<>()); + return new ArrayList<>(); } - List userMenuIds; + List menuDTOs; // 如果是管理员获取管理员配置的菜单,管理员的ID是1 if (StringUtils.equalsIgnoreCase(userId, "1")) { - userMenuIds = listAdminMenuId(); + List userMenuIds = listAdminMenuId(); + menuDTOs = listMenuTree(userMenuIds, true); } else { params.put("userId", userId); params.put(IRoleService.ROLE_TYPE, IRoleService.ROLE_MENU); - userMenuIds = listMenuIdByUser(params); + List userMenuIds = listMenuIdByUser(params); + menuDTOs = listMenuTree(userMenuIds, false); } - // 获取全部菜单 - params.clear(); - params.put("menuParentId", menuId); + // 老方法 + // List menuDTOs = listMenusAll(params); + // 新方法 + // 返回客户端菜单 + List clientMenuDTOs = new ArrayList<>(); + for (MenuDTO menuDTO : menuDTOs) { + if (StringUtils.equals(menuId, menuDTO.getMenuId())) { + clientMenuDTOs.addAll(menuDTO.getSubMenus()); + } + } + return clientMenuDTOs; + } + + @Override + public List listMenuByRoleIdAndType(String roleId, String type) { + Map params = getHashMap(4); + // 全部菜单 params.put("menuStatus", 0); - params.put("menuIds", userMenuIds); - List menuDTOs = listMenusAll(params); - return new SuccessResultData<>(menuDTOs); + List menuDTOs = ArrayListUtil.deepClone(menuDao.listMenus(params), MenuDTO.class); + List resultMenuDTOs = new ArrayList<>(); + params.clear(); + params.put("roleId", roleId); + if (IRoleService.AUTH_TYPE_INSERT.equals(type)) { + params.put(IRoleService.ROLE_TYPE, IRoleService.ROLE_INSERT); + } else if (IRoleService.AUTH_TYPE_DELETE.equals(type)) { + params.put(IRoleService.ROLE_TYPE, IRoleService.ROLE_DELETE); + } else if (IRoleService.AUTH_TYPE_UPDATE.equals(type)) { + params.put(IRoleService.ROLE_TYPE, IRoleService.ROLE_UPDATE); + } else if (IRoleService.AUTH_TYPE_QUERY.equals(type)) { + params.put(IRoleService.ROLE_TYPE, IRoleService.ROLE_QUERY); + } else { + params.put(IRoleService.ROLE_TYPE, IRoleService.ROLE_MENU); + } + // 角色菜单 + List roleMenuBOs = roleDao.listRoleMenuInfo(params); + // 勾选权限 + for (MenuDTO menuDTO : menuDTOs) { + for (RoleMenuBO roleMenuBO : roleMenuBOs) { + if (StringUtils.equals(roleMenuBO.getMenuId(), menuDTO.getMenuId())) { + menuDTO.setRight(true); + break; + } + } + } + buildMenuTree(resultMenuDTOs, menuDTOs, "0"); + return resultMenuDTOs; + } + + /** + * 菜单树列表 + * + * @param userMenuIds + * @param isAdmin + * @return + */ + private List listMenuTree(List userMenuIds, boolean isAdmin) { + Map params = getHashMap(6); + params.put("menuStatus", 0); + List menuDTOs = ArrayListUtil.deepClone(menuDao.listMenus(params), MenuDTO.class); + List resultMenuDTOs = new ArrayList<>(); + if (isAdmin && userMenuIds.isEmpty()) { + buildMenuTree(resultMenuDTOs, menuDTOs, "0"); + return resultMenuDTOs; + } + // 得到用户菜单 + for (int i = 0; i < menuDTOs.size(); i++) { + MenuDTO menuDTO = menuDTOs.get(i); + boolean isUserMenu = false; + for (String menuId : userMenuIds) { + if (StringUtils.equals(menuDTO.getMenuId(), menuId)) { + isUserMenu = true; + break; + } + } + if (!isUserMenu) { + menuDTOs.remove(i); + i--; + } + } + // 构建树形结构 + buildMenuTree(resultMenuDTOs, menuDTOs, "0"); + return resultMenuDTOs; + } + + /** + * 构建菜单树 + * + * @param parentMenuDTOs + * @param menuDTOs + * @param menuParentId + */ + private void buildMenuTree(List parentMenuDTOs, List menuDTOs, String menuParentId) { + for (int i = 0; i < menuDTOs.size(); i++) { + MenuDTO menuDTO = menuDTOs.get(i); + if (StringUtils.equals(menuDTO.getMenuParentId(), menuParentId)) { + parentMenuDTOs.add(menuDTO); + menuDTOs.remove(i); + i--; + } + } + for (MenuDTO menuDTO : parentMenuDTOs) { + List subMenuDTOs = new ArrayList<>(); + menuDTO.setSubMenus(subMenuDTOs); + buildMenuTree(subMenuDTOs, menuDTOs, menuDTO.getMenuId()); + if (!subMenuDTOs.isEmpty()) { + menuDTO.setParent(true); + } + } } @Override diff --git a/src/main/resources/application-test.yml b/src/main/resources/application-test.yml index 0d53412..53d5b04 100644 --- a/src/main/resources/application-test.yml +++ b/src/main/resources/application-test.yml @@ -1,6 +1,6 @@ server: port: 7001 - url: http://192.168.0.103:7001/usercenter + url: http://127.0.0.1:7001/usercenter # ws: ws://192.168.0.103:7001/usercenter/ws title: 统一用户管理平台 login-page-name: 统一用户管理平台 @@ -28,14 +28,14 @@ spring: max-request-size: 1GB datasource: druid: - url: jdbc:mysql://49.233.36.36:6688/db_cloud?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&autoReconnect=true&failOverReadOnly=false&useSSL=false&serverTimezone=UTC -# url: jdbc:mysql://127.0.0.1:3306/db_cloud_v2_inspection?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&autoReconnect=true&failOverReadOnly=false&useSSL=false&serverTimezone=UTC +# url: jdbc:mysql://49.233.36.36:6688/db_cloud?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&autoReconnect=true&failOverReadOnly=false&useSSL=false&serverTimezone=UTC + url: jdbc:mysql://127.0.0.1:3306/db_cloud_v2_city?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&autoReconnect=true&failOverReadOnly=false&useSSL=false&serverTimezone=UTC db-type: mysql driver-class-name: com.mysql.cj.jdbc.Driver - username: wanggeng -# username: root - password: WenG>2132997 -# password: root +# username: wanggeng + username: root +# password: WenG>2132997 + password: root initial-size: 2 min-idle: 2 max-active: 10 @@ -99,7 +99,7 @@ swagger: swagger-base-package: com.cm file: - uploadPath: /Users/wanggeng/Desktop/UploadFiles/ + uploadPath: C:\Users\wenc0\Desktop\UploadFiles\ imageTypes: png,jpg,jpeg,gif,blob videoTypes: mp4,rmvb audioTypes: mp3,wmv diff --git a/src/main/resources/mybatis/mapper/system/menu/menu-mapper.xml b/src/main/resources/mybatis/mapper/system/menu/menu-mapper.xml index 5dfebd2..4696150 100644 --- a/src/main/resources/mybatis/mapper/system/menu/menu-mapper.xml +++ b/src/main/resources/mybatis/mapper/system/menu/menu-mapper.xml @@ -141,6 +141,10 @@ AND menu_parent_id = #{menuParentId} + + AND + menu_status = #{menuStatus} + ORDER BY menu_order asc