一、菜单管理
数据库表:
CREATE TABLE `sys_menu` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '编号',
`parent_id` BIGINT(20) NOT NULL DEFAULT '0' COMMENT '所属上级',
`name` VARCHAR(20) NOT NULL DEFAULT '' COMMENT '名称',
`type` TINYINT(3) NOT NULL DEFAULT '0' COMMENT '类型(0:目录,1:菜单,2:按钮)',
`path` VARCHAR(100) DEFAULT NULL COMMENT '路由地址',
`component` VARCHAR(100) DEFAULT NULL COMMENT '组件路径',
`perms` VARCHAR(100) DEFAULT NULL COMMENT '权限标识',
`icon` VARCHAR(100) DEFAULT NULL COMMENT '图标',
`sort_value` INT(11) DEFAULT NULL COMMENT '排序',
`status` TINYINT(4) DEFAULT NULL COMMENT '状态(0:禁止,1:正常)',
`create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`is_deleted` TINYINT(3) NOT NULL DEFAULT '0' COMMENT '删除标记(0:不可用 1:可用)',
PRIMARY KEY (`id`),
KEY `idx_parent_id` (`parent_id`)
) ENGINE=INNODB AUTO_INCREMENT=58 DEFAULT CHARSET=utf8mb4 COMMENT='菜单表';
#
# Data for table "sys_menu"
#
INSERT INTO `sys_menu` VALUES (2,0,'系统管理',0,'system','Layout',NULL,'el-icon-s-tools',1,1,'2021-05-31 18:05:37','2022-06-09 09:23:24',0),
(3,2,'用户管理',1,'sysUser','system/sysUser/list','','el-icon-s-custom',1,1,'2021-05-31 18:05:37','2022-06-09 09:22:47',0),
(4,2,'角色管理',1,'sysRole','system/sysRole/list','','el-icon-user-solid',2,1,'2021-05-31 18:05:37','2022-06-09 09:37:18',0),
(5,2,'菜单管理',1,'sysMenu','system/sysMenu/list','','el-icon-s-unfold',3,1,'2021-05-31 18:05:37','2022-06-09 09:37:21',0),
(6,3,'查看',2,NULL,NULL,'bnt.sysUser.list',NULL,1,1,'2021-05-31 18:05:37','2022-06-09 09:22:38',0),
(7,3,'添加',2,NULL,NULL,'bnt.sysUser.add',NULL,1,1,'2021-05-31 18:05:37','2022-06-09 09:22:38',0),
(8,3,'修改',2,NULL,NULL,'bnt.sysUser.update',NULL,1,1,'2021-05-31 18:05:37','2022-06-09 09:22:38',0),
(9,3,'删除',2,NULL,NULL,'bnt.sysUser.remove',NULL,1,1,'2021-05-31 18:05:37','2022-06-09 09:22:38',0),
(10,4,'查看',2,NULL,NULL,'bnt.sysRole.list',NULL,1,1,'2021-05-31 18:05:37','2022-06-09 09:22:38',0),
(11,4,'添加',2,NULL,NULL,'bnt.sysRole.add',NULL,1,1,'2021-05-31 18:05:37','2022-06-09 09:22:38',0),
(12,4,'修改',2,NULL,NULL,'bnt.sysRole.update',NULL,1,1,'2021-05-31 18:05:37','2022-06-09 09:22:38',0),
(13,4,'删除',2,NULL,NULL,'bnt.sysRole.remove',NULL,1,1,'2021-05-31 18:05:37','2022-06-09 09:22:38',0),
(14,5,'查看',2,NULL,NULL,'bnt.sysMenu.list',NULL,1,1,'2021-05-31 18:05:37','2022-06-09 09:22:38',0),
(15,5,'添加',2,NULL,NULL,'bnt.sysMenu.add',NULL,1,1,'2021-05-31 18:05:37','2022-06-09 09:22:38',0),
(16,5,'修改',2,NULL,NULL,'bnt.sysMenu.update',NULL,1,1,'2021-05-31 18:05:37','2022-06-09 09:22:38',0),
(17,5,'删除',2,NULL,NULL,'bnt.sysMenu.remove',NULL,1,1,'2021-05-31 18:05:37','2022-06-09 09:22:38',0),
(18,3,'分配角色',2,NULL,NULL,'bnt.sysUser.assignRole',NULL,1,1,'2022-05-23 17:14:32','2022-06-09 09:22:38',0),
(19,4,'分配权限',2,'assignAuth','system/sysRole/assignAuth','bnt.sysRole.assignAuth',NULL,1,1,'2022-05-23 17:18:14','2022-06-09 09:22:38',0),
(20,2,'部门管理',1,'sysDept','system/sysDept/list','','el-icon-s-operation',4,1,'2022-05-24 10:07:05','2022-06-09 09:38:12',0),
(21,20,'查看',2,NULL,NULL,'bnt.sysDept.list',NULL,1,1,'2022-05-24 10:07:44','2022-06-09 09:22:38',0),
(22,2,'岗位管理',1,'sysPost','system/sysPost/list','','el-icon-more-outline',5,1,'2022-05-24 10:25:30','2022-06-09 09:38:13',0),
(23,22,'查看',2,NULL,NULL,'bnt.sysPost.list',NULL,1,1,'2022-05-24 10:25:45','2022-06-09 09:22:38',0),
(24,20,'添加',2,NULL,NULL,'bnt.sysDept.add',NULL,1,1,'2022-05-25 15:31:27','2022-06-09 09:22:38',0),
(25,20,'修改',2,NULL,NULL,'bnt.sysDept.update',NULL,1,1,'2022-05-25 15:31:41','2022-06-09 09:22:38',0),
(26,20,'删除',2,NULL,NULL,'bnt.sysDept.remove',NULL,1,1,'2022-05-25 15:31:59','2022-06-09 09:22:38',0),
(27,22,'添加',2,NULL,NULL,'bnt.sysPost.add',NULL,1,1,'2022-05-25 15:32:44','2022-06-09 09:22:38',0),
(28,22,'修改',2,NULL,NULL,'bnt.sysPost.update',NULL,1,1,'2022-05-25 15:32:58','2022-06-09 09:22:38',0),
(29,22,'删除',2,NULL,NULL,'bnt.sysPost.remove',NULL,1,1,'2022-05-25 15:33:11','2022-06-09 09:22:38',0),
(30,34,'操作日志',1,'sysOperLog','system/sysOperLog/list','','el-icon-document-remove',7,1,'2022-05-26 16:09:59','2022-06-09 09:39:23',0),
(31,30,'查看',2,NULL,NULL,'bnt.sysOperLog.list',NULL,1,1,'2022-05-26 16:10:17','2022-06-09 09:22:38',0),
(32,34,'登录日志',1,'sysLoginLog','system/sysLoginLog/list','','el-icon-s-goods',8,1,'2022-05-26 16:36:13','2022-06-09 09:39:24',0),
(33,32,'查看',2,NULL,NULL,'bnt.sysLoginLog.list',NULL,1,1,'2022-05-26 16:36:31','2022-06-09 09:36:36',0),
(34,2,'日志管理',0,'log','ParentView','','el-icon-tickets',6,1,'2022-05-31 13:23:07','2022-06-09 09:39:22',0),
(35,0,'审批设置',0,'processSet','Layout','','el-icon-setting',1,1,'2022-12-01 09:32:46','2022-12-01 09:32:46',0),
(36,35,'审批模板',1,'processTemplate','processSet/processTemplate/list','','el-icon-s-help',2,1,'2022-12-01 09:37:08','2022-12-19 14:10:48',0),
(37,36,'查看',2,'','','bnt.processTemplate.list','',1,1,'2022-12-01 09:37:49','2022-12-01 09:37:49',0),
(38,36,'审批模板设置',2,'templateSet','processSet/processTemplate/templateSet','bnt.processTemplate.templateSet','',1,1,'2022-12-01 14:52:08','2022-12-13 18:11:56',0),
(39,35,'审批类型',1,'processType','processSet/processType/list','','el-icon-s-unfold',1,1,'2022-12-02 14:46:18','2022-12-13 18:12:24',0),
(40,39,'查看',2,'','','bnt.processType.list','',1,1,'2022-12-02 14:46:41','2022-12-02 14:46:41',0),
(41,0,'审批管理',0,'processMgr','Layout','','el-icon-more-outline',1,1,'2022-12-02 14:48:11','2022-12-20 09:29:30',0),
(42,41,'审批列表',1,'process','processMgr/process/list','','el-icon-document-remove',1,1,'2022-12-02 14:49:06','2022-12-02 14:59:17',0),
(43,42,'查看',2,'','','bnt.process.list','',1,1,'2022-12-02 14:49:24','2022-12-02 14:49:24',0),
(44,36,'在线流程设置',2,'onlineProcessSet','processSet/processTemplate/onlineProcessSet','bnt.processTemplate.onlineProcessSet','',1,1,'2022-12-08 10:13:15','2022-12-19 18:57:35',0),
(45,39,'添加',2,'','','bnt.processType.add','',1,1,'2022-12-09 09:14:53','2022-12-09 09:14:53',0),
(46,39,'修改',2,'','','bnt.processType.update','',1,1,'2022-12-09 09:15:10','2022-12-09 09:15:10',0),
(47,39,'删除',2,'','','bnt.processType.remove','',1,1,'2022-12-09 09:15:25','2022-12-09 09:15:25',0),
(48,36,'删除',2,'','','bnt.processTemplate.remove','',1,1,'2022-12-09 09:22:29','2022-12-09 09:22:29',0),
(49,36,'发布',2,'','','bnt.processTemplate.publish','',1,1,'2022-12-09 09:24:47','2022-12-09 09:24:47',0),
(50,0,'公众号菜单',0,'wechat','Layout','','el-icon-s-operation',1,1,'2022-12-13 09:06:58','2022-12-21 11:20:55',0),
(51,50,'菜单列表',1,'menu','wechat/menu/list','','el-icon-s-help',1,1,'2022-12-13 09:07:52','2022-12-13 09:09:49',0),
(52,51,'查看',2,'','','bnt.menu.list','',1,1,'2022-12-13 09:08:48','2022-12-13 17:58:23',0),
(53,51,'添加',2,'','','bnt.menu.add','',1,1,'2022-12-13 16:29:25','2022-12-13 17:58:34',0),
(54,51,'修改',2,'','','bnt.menu.update','',1,1,'2022-12-13 16:29:41','2022-12-13 17:58:42',0),
(55,51,'删除',2,'','','bnt.menu.remove','',1,1,'2022-12-13 16:29:59','2022-12-13 17:58:47',0),
(56,51,'删除微信菜单',2,'','','bnt.menu.removeMenu','',1,1,'2022-12-13 16:30:36','2022-12-13 17:58:54',0),
(57,51,'同步微信菜单',2,'','','bnt.menu.syncMenu','',1,1,'2022-12-13 16:31:00','2022-12-13 17:59:01',0);
CREATE TABLE `sys_role_menu` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`role_id` BIGINT(20) NOT NULL DEFAULT '0',
`menu_id` BIGINT(11) NOT NULL DEFAULT '0',
`create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`is_deleted` TINYINT(3) NOT NULL DEFAULT '0' COMMENT '删除标记(0:不可用 1:可用)',
PRIMARY KEY (`id`),
KEY `idx_role_id` (`role_id`),
KEY `idx_menu_id` (`menu_id`)
) ENGINE=INNODB AUTO_INCREMENT=33 DEFAULT CHARSET=utf8 COMMENT='角色菜单';
后端代码
utils类
public class MenuHelper {
//使用递归方式构建菜单
public static List<SysMenu> buildTree(List<SysMenu> sysMenuList) {
//创建list集合,用于最终数据
List<SysMenu> trees =new ArrayList<>();
//把所有菜单数据进行遍历
for (SysMenu sysMenu : sysMenuList) {
//递归入口进入
if (sysMenu.getParentId().longValue() == 0) {
trees.add(getChildren(sysMenu, sysMenuList));
}
}
return trees;
}
public static SysMenu getChildren(SysMenu sysMenu, List<SysMenu> sysMenuList) {
sysMenu.setChildren(new ArrayList<>());
//遍历所有菜单数据
for (SysMenu sysMenu1 : sysMenuList) {
if (sysMenu.getId().longValue() == sysMenu1.getParentId().longValue()) {
if (sysMenu.getChildren() == null){
sysMenu.setChildren(new ArrayList<>());
}
sysMenu.getChildren().add(getChildren(sysMenu1, sysMenuList));
}
}
return sysMenu;
}
}
service接口
public interface SysMenuService extends IService<SysMenu> {
List<SysMenu> findNodes();
void removeMenuById(Long id);
List<SysMenu> findMenuByRoleId(Long roleId);
void doAssign(AssginMenuVo assginMenuVo);
}
service实现类
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {
@Autowired
private SysRoleMenuService sysRoleMenuService;
//菜单列表
@Override
public List<SysMenu> findNodes() {
//查询所有菜单数据
List<SysMenu> menuList = baseMapper.selectList(null);
//构建树形结构
List<SysMenu> buildTree = MenuHelper.buildTree(menuList);
return buildTree;
}
//删除菜单
@Override
public void removeMenuById(Long id) {
//判断当前菜单是否有下一层菜单
LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(SysMenu::getParentId,id);
Integer count = baseMapper.selectCount(wrapper);
//如果有就不能删除
if (count != 0){
throw new CJCException(201,"菜单不能删除");
}
//没有子菜单就进行删除
baseMapper.deleteById(id);
}
//查询所有菜单和角色分配的菜单
@Override
public List<SysMenu> findMenuByRoleId(Long roleId) {
//查询所有菜单根据status状态查询
LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(SysMenu::getStatus,1);
List<SysMenu> list = baseMapper.selectList(wrapper);
//根据角色id查询菜单id
LambdaQueryWrapper<SysRoleMenu> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SysRoleMenu::getRoleId,roleId);
List<SysRoleMenu> sysRoleMenuList = sysRoleMenuService.list(queryWrapper);
List<Long> menuByIdList = sysRoleMenuList.stream().map(c -> c.getMenuId()).collect(Collectors.toList());
//根据菜单id和菜单里面的所有id进行比较
list.stream().forEach(c ->{
if (menuByIdList.contains(c.getId())){
c.setSelect(true);
}else {
c.setSelect(false);
}
});
//封装为树形结构
List<SysMenu> treeList = MenuHelper.buildTree(list);
return treeList;
}
//角色分配菜单
@Override
public void doAssign(AssginMenuVo assginMenuVo) {
//根据角色id删除菜单角色表分配数据
LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(SysRoleMenu::getRoleId,assginMenuVo.getRoleId());
sysRoleMenuService.remove(wrapper);
//获取角色新分配菜单id列表,进行遍历,把每个id数据添加菜单列表
List<Long> menuIdList = assginMenuVo.getMenuIdList();
for (Long menuId:menuIdList) {
if (menuId == null){
continue;
}
SysRoleMenu sysRoleMenu = new SysRoleMenu();
sysRoleMenu.setRoleId(assginMenuVo.getRoleId());
sysRoleMenu.setMenuId(menuId);
sysRoleMenuService.save(sysRoleMenu);
}
}
}
controller层
@Api(tags = "菜单管理接口")
@RestController
@RequestMapping("/admin/system/sysMenu")
public class SysMenuController {
@Autowired
private SysMenuService sysMenuService;
@ApiOperation("菜单列表")
@GetMapping("/findNodes")
public Result findNodes(){
List<SysMenu> list = sysMenuService.findNodes();
return Result.ok(list);
}
@ApiOperation("新增菜单")
@PostMapping("/addMenu")
public Result addMenu(@RequestBody SysMenu sysMenu){
sysMenuService.save(sysMenu);
return Result.ok();
}
@ApiOperation("修改菜单")
@PutMapping("/updateMenu")
public Result updateMenu(@RequestBody SysMenu sysMenu){
sysMenuService.updateById(sysMenu);
return Result.ok();
}
@ApiOperation("删除菜单")
@DeleteMapping("/deleteMenu/{id}")
public Result deleteMenu(@PathVariable Long id){
sysMenuService.removeMenuById(id);
return Result.ok();
}
@ApiOperation("查询所有菜单和角色分配的菜单")
@GetMapping("/toAssign/{roleId}")
public Result toAssign(@PathVariable("roleId") Long roleId){
List<SysMenu> list = sysMenuService.findMenuByRoleId(roleId);
return Result.ok(list);
}
@ApiOperation("角色分配菜单")
@PostMapping("/doAssign")
public Result doAssign(@RequestBody AssginMenuVo assginMenuVo){
sysMenuService.doAssign(assginMenuVo);
return Result.ok();
}
}
前端代码省略。。。。