Initial commit
This commit is contained in:
parent
0af2f0ad97
commit
d6b795311f
@ -1,9 +1,9 @@
|
||||
package com.atguigu.auth.controller;
|
||||
|
||||
|
||||
import com.atguigu.auth.service.SysMenuService;
|
||||
import com.atguigu.common.result.Result;
|
||||
import com.atguigu.model.system.SysMenu;
|
||||
import com.atguigu.vo.system.AssginMenuVo;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -27,6 +27,32 @@ public class SysMenuController {
|
||||
@Autowired
|
||||
private SysMenuService sysMenuService;
|
||||
|
||||
/**
|
||||
* 根据角色获取菜单
|
||||
*
|
||||
* @param roleId
|
||||
* @return
|
||||
*/
|
||||
@ApiOperation(value = "根据角色获取菜单")
|
||||
@GetMapping("toAssign/{roleId}")
|
||||
public Result toAssign(@PathVariable Long roleId) {
|
||||
List<SysMenu> list = sysMenuService.findSysMenuByRoleId(roleId);
|
||||
return Result.ok(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 给角色分配权限
|
||||
*
|
||||
* @param assignMenuVo
|
||||
* @return
|
||||
*/
|
||||
@ApiOperation(value = "给角色分配权限")
|
||||
@PostMapping("/doAssign")
|
||||
public Result doAssign(@RequestBody AssginMenuVo assignMenuVo) {
|
||||
sysMenuService.doAssign(assignMenuVo);
|
||||
return Result.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 菜单列表
|
||||
*
|
||||
|
@ -1,9 +1,9 @@
|
||||
package com.atguigu.auth.service;
|
||||
|
||||
import com.atguigu.model.system.SysMenu;
|
||||
import com.atguigu.vo.system.AssginMenuVo;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@ -29,4 +29,19 @@ public interface SysMenuService extends IService<SysMenu> {
|
||||
* @param id
|
||||
*/
|
||||
void removeMenuById(Long id);
|
||||
|
||||
/**
|
||||
* 根据角色获取菜单
|
||||
*
|
||||
* @param roleId
|
||||
* @return
|
||||
*/
|
||||
List<SysMenu> findSysMenuByRoleId(Long roleId);
|
||||
|
||||
/**
|
||||
* 给角色分配权限
|
||||
*
|
||||
* @param assignMenuVo
|
||||
*/
|
||||
void doAssign(AssginMenuVo assignMenuVo);
|
||||
}
|
||||
|
@ -1,15 +1,20 @@
|
||||
package com.atguigu.auth.service.impl;
|
||||
|
||||
import com.atguigu.auth.mapper.SysMenuMapper;
|
||||
import com.atguigu.auth.mapper.SysRoleMenuMapper;
|
||||
import com.atguigu.auth.service.SysMenuService;
|
||||
import com.atguigu.auth.utils.MenuHelper;
|
||||
import com.atguigu.common.execption.GuiguException;
|
||||
import com.atguigu.model.system.SysMenu;
|
||||
import com.atguigu.model.system.SysRoleMenu;
|
||||
import com.atguigu.vo.system.AssginMenuVo;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@ -22,6 +27,9 @@ import java.util.List;
|
||||
@Service
|
||||
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {
|
||||
|
||||
|
||||
private SysRoleMenuMapper sysRoleMenuMapper;
|
||||
|
||||
/**
|
||||
* 菜单列表
|
||||
*
|
||||
@ -33,9 +41,8 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
List<SysMenu> sysMenuList = baseMapper.selectList(null);
|
||||
|
||||
// 2、构建树形结构
|
||||
List<SysMenu> list = MenuHelper.buildTree(sysMenuList);
|
||||
|
||||
return list;
|
||||
return MenuHelper.buildTree(sysMenuList);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -57,4 +64,51 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
baseMapper.deleteById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据角色获取菜单
|
||||
*
|
||||
* @param roleId
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<SysMenu> findSysMenuByRoleId(Long roleId) {
|
||||
//查询所有菜单
|
||||
List<SysMenu> allSysMenuList = this.list(new LambdaQueryWrapper<SysMenu>().eq(SysMenu::getStatus, 1));
|
||||
|
||||
//在角色菜单关系表中,根据角色id获取角色对应的所有菜单id
|
||||
List<SysRoleMenu> sysRoleMenuList = sysRoleMenuMapper.selectList(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, roleId));
|
||||
//根据获取菜单id,获取对应菜单对象
|
||||
List<Long> menuIdList = sysRoleMenuList.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
|
||||
//根据菜单id,和所有菜单集合中id比较,相同则封装
|
||||
allSysMenuList.forEach(permission -> {
|
||||
if (menuIdList.contains(permission.getId())) {
|
||||
permission.setSelect(true);
|
||||
} else {
|
||||
permission.setSelect(false);
|
||||
}
|
||||
});
|
||||
//返回规定格式的菜单列表
|
||||
return MenuHelper.buildTree(allSysMenuList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 给角色分配权限
|
||||
*
|
||||
* @param assignMenuVo
|
||||
*/
|
||||
@Override
|
||||
public void doAssign(AssginMenuVo assignMenuVo) {
|
||||
//根据角色id 删除菜单角色表 分配数据
|
||||
sysRoleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, assignMenuVo.getRoleId()));
|
||||
//从参数里面获取角色新分配菜单id列表,
|
||||
//进行遍历,把每个id 数据添加菜单角色表
|
||||
for (Long menuId : assignMenuVo.getMenuIdList()) {
|
||||
if (StringUtils.isEmpty(menuId)) continue;
|
||||
SysRoleMenu rolePermission = new SysRoleMenu();
|
||||
rolePermission.setRoleId(assignMenuVo.getRoleId());
|
||||
rolePermission.setMenuId(menuId);
|
||||
sysRoleMenuMapper.insert(rolePermission);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
||||
//查询的角色id
|
||||
List<SysUserRole> existUserRoleList = sysUserRoleMapper.selectList(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId, userId).select(SysUserRole::getRoleId));
|
||||
|
||||
List<Long> existRoleIdList = existUserRoleList.stream().map(c -> c.getRoleId()).collect(Collectors.toList());
|
||||
List<Long> existRoleIdList = existUserRoleList.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
|
||||
|
||||
//对角色进行分类
|
||||
List<SysRole> assginRoleList = new ArrayList<>();
|
||||
|
Loading…
Reference in New Issue
Block a user