进销存系统_菜单管理(14)
Posted 上善若水
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了进销存系统_菜单管理(14)相关的知识,希望对你有一定的参考价值。
一、项目结构
二、菜单管理
2.1、菜单主要列表展示
- 界面原型
- 菜单列表展示实现核心
对于菜单数据展示与前面用户、角色管理数据展示稍有不同,菜单表为父子关系关联表,所以在数据展示上使用树形表格展示结构更加清晰,合理,对于树形表格数据展示实现可参考官网tree-table介绍
2.2、添加 | 更新菜单记录
- 界面原型
1.菜单添加
2.菜单更新
- 菜单添加与更新流程
菜单项添加与更新流程与用户、角色记录添加|更新流程类似,参考用户|角色记录添加与更
新处理(同层级菜单名唯一、菜单权限码全局唯一)
2.3、删除菜单记录
- 界面原型
- 删除菜单记录流程
三、代码
Menu.java
package com.xbmu.admin.pojo;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.EqualsAndHashCode;
/**
* <p>
* 菜单表
* </p>
*
* @author bitaotao
* @since 2021-09-11
*/
@EqualsAndHashCode(callSuper = false)
@TableName("t_menu")
@ApiModel(value="Menu对象", description="菜单表")
public class Menu implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "主键")
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@ApiModelProperty(value = "菜单图标")
private String icon;
@ApiModelProperty(value = "菜单名称")
private String name;
@ApiModelProperty(value = "节点类型")
private Integer state;
@ApiModelProperty(value = "菜单url")
private String url;
@ApiModelProperty(value = "上级菜单id")
private Integer pId;
@ApiModelProperty(value = "权限码")
private String aclValue;
@ApiModelProperty(value = "菜单层级")
private Integer grade;
@ApiModelProperty(value = "是否删除")
private Integer isDel;
public static long getSerialVersionUID() {
return serialVersionUID;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getState() {
return state;
}
public void setState(Integer state) {
this.state = state;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Integer getpId() {
return pId;
}
public void setpId(Integer pId) {
this.pId = pId;
}
public String getAclValue() {
return aclValue;
}
public void setAclValue(String aclValue) {
this.aclValue = aclValue;
}
public Integer getGrade() {
return grade;
}
public void setGrade(Integer grade) {
this.grade = grade;
}
public Integer getIsDel() {
return isDel;
}
public void setIsDel(Integer isDel) {
this.isDel = isDel;
}
}
MenuController.java
package com.xbmu.admin.controller;
import com.xbmu.admin.dto.TreeDto;
import com.xbmu.admin.model.RespBean;
import com.xbmu.admin.pojo.Menu;
import com.xbmu.admin.service.IMenuService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
/**
* <p>
* 菜单表 前端控制器
* </p>
*
* @author bitaotao
* @since 2021-09-11
*/
@Controller
@RequestMapping("/menu")
public class MenuController {
@Resource
private IMenuService menuService;
/**
* 返回所有菜单数据
* @return
*/
@RequestMapping("queryAllMenus")
@ResponseBody
public List<TreeDto> queryAllMenus(Integer roleId){
return menuService.queryAllMenus(roleId);
}
/**
* 菜单主页
* @return
*/
@RequestMapping("index")
public String index(){
return "menu/menu";
}
/**
* 菜单列表查询接口
* @return
*/
@RequestMapping("list")
@ResponseBody
public Map<String,Object> menuList(){
return menuService.menuList();
}
/**
* 添加菜单页
* @param grade 菜单层级
* @param pId 父级菜单id
* @param model model对象
* @return
*/
@RequestMapping("addMenuPage")
public String addMenuPage(Integer grade, Integer pId, Model model){
model.addAttribute("grade",grade);
model.addAttribute("pId",pId);
return "menu/add";
}
/**
* 添加菜单
* @param menu
* @return
*/
@RequestMapping("save")
@ResponseBody
public RespBean saveMenu(Menu menu){
menuService.saveMenu(menu);
return RespBean.success("菜单记录添加成功!");
}
/**
* 更新菜单页
* @param id
* @param model
* @return
*/
@RequestMapping("updateMenuPage")
public String updateMenuPage(Integer id,Model model){
model.addAttribute("menu",menuService.getById(id));
return "menu/update";
}
/**
* 更新菜单
* @param menu
* @return
*/
@RequestMapping("update")
@ResponseBody
public RespBean updateMenu(Menu menu){
menuService.updateMenu(menu);
return RespBean.success("菜单记录更新成功");
}
/**
* 菜单删除
* @param id
* @return
*/
@RequestMapping("delete")
@ResponseBody
public RespBean deleteMenu(Integer id){
menuService.deleteMenuById(id);
return RespBean.success("菜单删除成功");
}
}
MenuServiceImpl.java
package com.xbmu.admin.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.xbmu.admin.dto.TreeDto;
import com.xbmu.admin.pojo.Menu;
import com.xbmu.admin.mapper.MenuMapper;
import com.xbmu.admin.pojo.RoleMenu;
import com.xbmu.admin.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbmu.admin.service.IRoleMenuService;
import com.xbmu.admin.util.AssertUtil;
import com.xbmu.admin.util.PageResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
/**
* <p>
* 菜单表 服务实现类
* </p>
*
* @author bitaotao
* @since 2021-09-11
*/
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {
@Resource
private IRoleMenuService roleMenuService;
@Override
public List<TreeDto> queryAllMenus(Integer roleId) {
List<TreeDto> treeDtos = this.baseMapper.queryAllMenus();
List<Integer> roleHasMenuIds = roleMenuService.queryRoleHasAllMenusByRoleId(roleId);
if(CollectionUtils.isNotEmpty(roleHasMenuIds)){
treeDtos.forEach(treeDto -> {
if(roleHasMenuIds.contains(treeDto.getId())){
treeDto.setChecked(true);
}
});
}
return treeDtos;
}
@Override
public Map<String, Object> menuList() {
List<Menu> menus = this.list(new QueryWrapper<Menu>().eq("is_del", 0));
return PageResultUtil.getResult((long) menus.size(),menus);
}
@Override
public Menu findMenuByNameAndGrade(String menuName,Integer grade) {
return this.getOne(new QueryWrapper<Menu>().eq("is_del",0).eq("name",menuName).eq("grade",grade));
}
@Override
public Menu findMenuByAclValue(String aclValue) {
return this.getOne(new QueryWrapper<Menu>().eq("is_del",0).eq("acl_value",aclValue));
}
@Override
public Menu findMenuById(Integer id) {
return this.getOne(new QueryWrapper<Menu>().eq("is_del",0).eq("id",id));
}
@Override
public Menu findMenuByGradeAndUrl(String url, Integer grade) {
return this.getOne(new QueryWrapper<Menu>().eq("is_del",0).eq("url",url).eq("grade",grade));
}
@Override
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public void saveMenu(Menu menu) {
/**
* 1.参数校验
* 菜单名 不能为空
* 菜单层级grade(0,1,2)只支持三级菜单
* 2.同一层级下
* 菜单名不可重复
* 3.权限码
* 非空、全局唯一
* 4.上级菜单
* 上级菜单 必须存在
* 5.url 判断
* 菜单属于二级菜单 url 不可重复的
* 6.设置参数
* 是否删除 isDel state
*/
AssertUtil.isTrue(StringUtils.isBlank(menu.getName()),"请输入菜单名!");
Integer grade = menu.getGrade();
AssertUtil.isTrue(null == grade || !(grade == 0 || grade == 1 || grade == 2),"菜单层级不合法!");
AssertUtil.isTrue(null != this.findMenuByNameAndGrade(menu.getName(),menu.getGrade()),"该层级下菜单已存在!");
AssertUtil.isTrue(null != this.findMenuByAclValue(menu.getAclValue()),"权限码已存在!");
AssertUtil.isTrue(null == menu.getpId() || null == this.findMenuById(menu.getpId()),"请指定上级菜单!");
if(grade == 1){
// 二级菜单
AssertUtil.isTrue(null != this.findMenuByGradeAndUrl(menu.getUrl(),menu.getGrade()),"该层级下url不可重复!");
}
menu.setIsDel(0);
menu.setState(0);
AssertUtil.isTrue(!(this.save(menu)),"菜单添加失败!");
}
@Override
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
pu以上是关于进销存系统_菜单管理(14)的主要内容,如果未能解决你的问题,请参考以下文章