修改 获取菜单根据角色权限获取

This commit is contained in:
萌狼蓝天 2024-11-01 11:19:01 +08:00
parent b87db3cef4
commit adc270d0ef
2 changed files with 13 additions and 45 deletions

View File

@ -10,6 +10,6 @@ import java.util.List;
@Mapper @Mapper
public interface SystemMapper { public interface SystemMapper {
List<SystemMenu> getAllMenus(); List<SystemMenu> getAllMenus();
@Select("SELECT * from menu_data_item") @Select("SELECT * from menu_data_item where id in (select system_role_menu.menu_id from system_role_menu where role_id=#{roleId})")
List<MenuDataItem> getMenus(); List<MenuDataItem> getMenus(String roleId);
} }

View File

@ -1,10 +1,13 @@
package com.guaiguailang.harmony.service.impl; package com.guaiguailang.harmony.service.impl;
import cn.dev33.satoken.stp.StpUtil;
import com.guaiguailang.harmony.domain.entity.MenuDataItem; import com.guaiguailang.harmony.domain.entity.MenuDataItem;
import com.guaiguailang.harmony.domain.entity.SystemMenu; import com.guaiguailang.harmony.domain.entity.SystemMenu;
import com.guaiguailang.harmony.domain.entity.UserInfo;
import com.guaiguailang.harmony.domain.vo.MenuVO; import com.guaiguailang.harmony.domain.vo.MenuVO;
import com.guaiguailang.harmony.domain.vo.ResponseResult; import com.guaiguailang.harmony.domain.vo.ResponseResult;
import com.guaiguailang.harmony.mapper.SystemMapper; import com.guaiguailang.harmony.mapper.SystemMapper;
import com.guaiguailang.harmony.mapper.UserMapper;
import com.guaiguailang.harmony.service.SystemService; import com.guaiguailang.harmony.service.SystemService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
@ -19,54 +22,19 @@ import java.util.stream.Collectors;
public class SystemServiceImpl implements SystemService { public class SystemServiceImpl implements SystemService {
private final SystemMapper systemMapper; private final SystemMapper systemMapper;
private final UserMapper userMapper;
public SystemServiceImpl(SystemMapper systemMapper) { public SystemServiceImpl(SystemMapper systemMapper, UserMapper userMapper) {
this.systemMapper = systemMapper; this.systemMapper = systemMapper;
this.userMapper = userMapper;
} }
// // 备份旧的方法
// public ResponseResult getMenu() {
// // 获取所有激活的状态菜单
// List<SystemMenu> menus = systemMapper.getAllMenus();
// Map<String, MenuVO> menuMap = new HashMap<>();
// List<MenuVO> rootMenus = new ArrayList<>();
// // 第一步将SystemMenu转换为MenuVO并存储在map中
// for (SystemMenu menu : menus) {
// MenuVO menuVO = new MenuVO();
// menuVO.setId(menu.getId());
// menuVO.setTitle(menu.getTitle());
// menuVO.setKey(menu.getKey());
// menuVO.setName(menu.getName());
// menuVO.setComponent(menu.getComponent());
// menuVO.setRedirect(menu.getRedirect());
// menuVO.setIcon(menu.getIcon());
// menuVO.setStatus(menu.getStatus());
// menuVO.setOrder(menu.getOrder());
// menuVO.setParent(menu.getParent());
// menuMap.put(menu.getName(), menuVO);
// }
// // 第二步构建树形结构
// for (MenuVO menuVO : menuMap.values()) {
// String parentId = menuVO.getParent();
// if (parentId == null) {
// // 没有父菜单即为根菜单
// rootMenus.add(menuVO);
// } else {
// // 查找父菜单并添加到其子菜单列表中
// MenuVO parentMenuVO = menuMap.get(parentId);
// if (parentMenuVO != null) {
// if (parentMenuVO.getChildren() == null) {
// parentMenuVO.setChildren(new ArrayList<>());
// }
// parentMenuVO.getChildren().add(menuVO);
// }
// }
// }
// return ResponseResult.success(rootMenus);
// }
public ResponseResult getMenu() { public ResponseResult getMenu() {
// 获取用户角色
Long uid = Long.parseLong(StpUtil.getLoginId().toString());
UserInfo userInfo = userMapper.getUserById(uid);
// 获取所有激活的状态菜单 // 获取所有激活的状态菜单
List<MenuDataItem> menus = systemMapper.getMenus(); List<MenuDataItem> menus = systemMapper.getMenus(userInfo.getRoleId());
return ResponseResult.success(menus); return ResponseResult.success(menus);
} }