三、菜单管理接口
3.1 创建SysMenu类及相关类
首先创建sys_menu表:
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=1 DEFAULT CHARSET=utf8mb4 COMMENT='菜单表';
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);
通过MyBatisPlus的代码生成器创建SysMenu相关类,前面已经使用多次,这里就不再说明。
创建好文件后,删除mapper目录下的xml文件和entity文件夹,在model模块中创建SysMenu实体类:
package pers.beiluo.yunshangoffice.model.system;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.List;
@ApiModel("菜单")
@Data
@TableName("sys_menu")
public class SysMenu extends BaseEntity {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "所属上级")
@TableField("parent_id")
private Long parentId;
@ApiModelProperty(value = "名称")
@TableField("name")
private String name;
@ApiModelProperty(value = "类型(1:菜单,2:按钮)")
@TableField("type")
private Integer type;
@ApiModelProperty(value = "路由地址")
@TableField("path")
private String path;
@ApiModelProperty(value = "组件路径")
@TableField("component")
private String component;
@ApiModelProperty(value = "权限标识")
@TableField("perms")
private String perms;
@ApiModelProperty(value = "图标")
@TableField("icon")
private String icon;
@ApiModelProperty(value = "排序")
@TableField("sort_value")
private Integer sortValue;
@ApiModelProperty(value = "状态(0:禁止,1:正常)")
@TableField("status")
private Integer status;
// 下级列表
@TableField(exist = false)
private List<SysMenu> children;
//是否选中
@TableField(exist = false)
private boolean isSelect;
}
3.2 SysMenuController
/**
* <p>
* 菜单表 前端控制器
* </p>
*
* @author beiluo
* @since 2024-03-04
*/
@Api(tags = "菜单管理")
@RestController
@RequestMapping("/admin/system/sysMenu")
public class SysMenuController {
@Autowired
private SysMenuService sysMenuService;
}
3.3 获取树形菜单
//SysMenuController
/**
* 这个方法以树形结构返回所有菜单,返回列表中的对象为根节点
* 通过递归的方法完成查询
* @return
*/
@Override
public List<SysMenu> getMenu() {
//首先查询所有目录
List<SysMenu> sysMenus = baseMapper.selectList(null);
//接着建立目录结构
List<SysMenu> sysMenus1 = MyUtils.buildMenu(sysMenus);
return sysMenus1;
}
//SysMenuServiceImpl
public List<SysMenu> getMenu() {
//首先查询所有目录
List<SysMenu> sysMenus = baseMapper.selectList(null);
//接着建立目录结构
List<SysMenu> sysMenus1 = MyUtils.buildMenu(sysMenus);
return sysMenus1;
}
//MyUtils.buildMenu
/**
* 构建树形菜单的工具方法,通过递归构建树形结构
* @param list 所有目录数据
* @return
*/
public static List<SysMenu> buildMenu(List<SysMenu> list){
List<SysMenu> sysMenus = new ArrayList<>();
for (SysMenu sysMenu : list) {
if(sysMenu.getParentId() == 0){
sysMenus.add(buildTree(sysMenu,list));
}
}
return sysMenus;
}
//buildTree
public static SysMenu buildTree(SysMenu sysMenu,List<SysMenu> list){
sysMenu.setChildren(new ArrayList<SysMenu>());
for (SysMenu menu : list) {
if(menu.getParentId()==sysMenu.getId()){
sysMenu.getChildren().add(buildTree(menu,list));
}
}
return sysMenu;
}
3.4 新增菜单
@ApiOperation("新增菜单")
@PostMapping("/save")
public Result save(@RequestBody SysMenu sysMenu){
sysMenuService.save(sysMenu);
return Result.ok();
}
3.5 通过id修改菜单
@ApiOperation("通过id修改菜单")
@PutMapping("/updateMenu")
public Result updateMenuById(@RequestBody SysMenu sysMenu){
sysMenuService.updateById(sysMenu);
return Result.ok();
}
3.6 删除菜单
@ApiOperation("删除菜单")
@DeleteMapping("/remove/{id}")
public Result removeMenu(@PathVariable Long id){
return Result.ok();
}
3.7 根据角色查询菜单
//SysMenuController
@ApiOperation("根据角色查询菜单")
@GetMapping("/getMenuByRole/{roleId}")
public Result getMenuByRole(@PathVariable("roleId") Long id){
List<SysMenu> menuByRole = sysMenuService.getMenuByRole(id);
return Result.ok(menuByRole);
}
//SysMenuServiceImpl
@Override
public List<SysMenu> getMenuByRole(Long id) {
//查询所有有效(状态为1)的菜单
LambdaQueryWrapper<SysMenu> sysMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
sysMenuLambdaQueryWrapper.eq(SysMenu::getStatus,1);
List<SysMenu> sysMenus = baseMapper.selectList(sysMenuLambdaQueryWrapper);
//查询角色菜单表
LambdaQueryWrapper<SysRoleMenu> sysRoleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
sysRoleMenuLambdaQueryWrapper.eq(SysRoleMenu::getRoleId, id);
List<SysRoleMenu> sysRoleMenus = sysRoleMenuMapper.selectList(sysRoleMenuLambdaQueryWrapper);
//取出当前角色拥有的菜单
List<Long> collect =
sysRoleMenus.stream().map(sysRoleMenu -> sysRoleMenu.getMenuId()).collect(Collectors.toList());
//遍历菜单列表,得到角色拥有的菜单
sysMenus.forEach(sysMenu -> {
if(collect.contains(sysMenu.getId())){
sysMenu.setSelect(true);
}else{
sysMenu.setSelect(false);
}
});
return MyUtils.buildMenu(sysMenus);
}
3.8 给角色添加菜单
//首先需要一个分配菜单的条件类
@Data
@ApiModel("角色分配条件类")
public class AssignRoleVo {
@ApiModelProperty("用户id")
private Long userId;
@ApiModelProperty("角色id列表")
private List<Long> roleIdList;
}
//SysMenuController
@ApiOperation("给角色添加菜单")
@PostMapping("/assignMenuForRole")
public Result assignMenuForRole(@RequestBody AssignMenuVo assignMenuVo){
sysMenuService.assignMenuForRole(assignMenuVo);
return Result.ok();
}
//SysMenuServiceImpl
@Override
public void assignMenuForRole(AssignMenuVo assignMenuVo) {
//首先删除角色菜单信息
LambdaQueryWrapper<SysRoleMenu> sysRoleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
sysRoleMenuLambdaQueryWrapper.eq(SysRoleMenu::getRoleId,assignMenuVo.getRoleId());
sysRoleMenuMapper.delete(sysRoleMenuLambdaQueryWrapper);
//向角色添加菜单
SysRoleMenu sysRoleMenu = new SysRoleMenu();
assignMenuVo.getMenuIdList().forEach(menuId -> {
sysRoleMenu.setRoleId(assignMenuVo.getRoleId());
sysRoleMenu.setMenuId(menuId);
sysRoleMenuMapper.insert(sysRoleMenu);
});
}