1、完成图标功能
要实现图标功能,首先是后端建表
- 建dict表
DROP TABLE IF EXISTS `dict`;
CREATE TABLE `dict` (
`name` varchar(255) DEFAULT NULL COMMENT '名称',
`value` varchar(255) DEFAULT NULL COMMENT '内容',
`type` varchar(255) DEFAULT NULL COMMENT '类型'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into `dict`(`name`,`value`,`type`) values
('user','el-icon-user','icon'),
('house','el-icon-house','icon'),
('menu','el-icon-menu','icon'),
('s-custom','el-icon-s-custom','icon'),
('s-menu','el-icon-s-grid','icon'),
('file','el-icon-document','icon');
- Dict实体类
Dict.java
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("dict")
public class Dict {
private String name;
private String value;
private String type;
}
- DictMapper.java
@Repository
public interface DictMapper extends BaseMapper<Dict> {
}
- DictService.java
&#64;Service
public class DictService extends ServiceImpl<DictMapper, Dict> {
}
- 图标功能是在菜单管理里面显示&#xff0c;所以实现功能写在MenuController中
MenuController.java
&#64;GetMapping("/icons")
public Result getIcons(){
QueryWrapper<Dict> queryWrapper &#61; new QueryWrapper<>();
queryWrapper.eq("type", Constants.DICT_TYPE_ICON);
return Result.success(dictService.list(queryWrapper));
}
- 前端实现菜单管理里编辑&#xff0c;可以下拉选择图标&#xff0c;如
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GJscqE2B-1673355144545)(D:\桌面\Java学习\项目\管理系统–前后端分离\项目截图\46.jpg)]
- 前端实现角色管理页面&#xff0c;分配菜单的时候&#xff0c;同样显示图标出来
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XbCYwnVm-1673355144546)(D:\桌面\Java学习\项目\管理系统–前后端分离\项目截图\47.jpg)]
{{ data.name }}
- 完整代码
Menu.vue
搜索
重置
新增
class&#61;"ml-5"
confirm-button-text&#61;&#39;确定&#39;
cancel-button-text&#61;&#39;我再想想&#39;
icon&#61;"el-icon-info"
icon-color&#61;"red"
title&#61;"您确定要删除这些内容吗&#xff1f;"
&#64;confirm&#61;"delBatch"
>
批量删除
row-key&#61;"id" default-expand-all &#64;selection-change&#61;"handleSelectionChange">
新增子菜单
编辑
class&#61;"ml-5"
confirm-button-text&#61;&#39;确定&#39;
cancel-button-text&#61;&#39;我再想想&#39;
icon&#61;"el-icon-info"
icon-color&#61;"red"
title&#61;"您确定要删除吗&#xff1f;"
&#64;confirm&#61;"handleDelete(scope.row.id)"
>
删除
{{ item.name }}
取 消
确 定
Role.vue
搜索
重置
新增
class&#61;"ml-5"
confirm-button-text&#61;&#39;确定&#39;
cancel-button-text&#61;&#39;我再想想&#39;
icon&#61;"el-icon-info"
icon-color&#61;"red"
title&#61;"您确定要删除这些内容吗&#xff1f;"
&#64;confirm&#61;"delBatch"
>
批量删除
分配菜单
编辑
class&#61;"ml-5"
confirm-button-text&#61;&#39;确定&#39;
cancel-button-text&#61;&#39;我再想想&#39;
icon&#61;"el-icon-info"
icon-color&#61;"red"
title&#61;"您确定要删除吗&#xff1f;"
&#64;confirm&#61;"handleDelete(scope.row.id)"
>
删除
&#64;size-change&#61;"handleSizeChange"
&#64;current-change&#61;"handleCurrentChange"
:current-page&#61;"pageNum"
:page-sizes&#61;"[2, 4, 6, 10]"
:page-size&#61;"pageSize"
layout&#61;"total, sizes, prev, pager, next, jumper"
:total&#61;"total">
取 消
确 定
:props&#61;"props"
:data&#61;"menuData"
show-checkbox
node-key&#61;"id"
ref&#61;"tree"
:default-expanded-keys&#61;"expands"
:default-checked-keys&#61;"checks">
{{ data.name }}
取 消
确 定
2、实现菜单管理和角色管理关系
实现&#xff1a;角色管理中&#xff0c;分配菜单后&#xff0c;会出现一个role_id对应一个或者多个menu_id &#xff0c;两者id之间的关系可以储存到数据库中&#xff0c;然后下次点开该角色的分配菜单时默认回显出来
- 建表Role_Menu表
USE &#96;management&#96;;
DROP TABLE IF EXISTS &#96;role_menu&#96;;
CREATE TABLE &#96;role_menu&#96; (
&#96;role_id&#96; int(11) NOT NULL COMMENT &#39;角色id&#39;,
&#96;menu_id&#96; int(11) NOT NULL COMMENT &#39;菜单id&#39;,
PRIMARY KEY (&#96;role_id&#96;,&#96;menu_id&#96;)
) ENGINE&#61;InnoDB DEFAULT CHARSET&#61;utf8 COMMENT&#61;&#39;角色菜单关系表&#39;;
- 注意&#xff0c;roleId和menuId都应该设置为主键
- 实体类RoleMenu.java
&#64;Data
&#64;TableName("role_menu")
public class RoleMenu {
private Integer roleId;
private Integer menuId;
}
和之前一样&#xff0c;把RoleMenuMapper和RoleMenuService建好&#xff0c;这里就省略
- 实现功能&#xff1a;不需要新建一个controller&#xff0c;因为这也算是role中的功能。
RoleController.java
&#64;PostMapping("/roleMenu/{roleId}")
public Result roleMenu(&#64;PathVariable Integer roleId, &#64;RequestBody List<Integer> menuIds){
roleService.setRoleMenu(roleId,menuIds);
return Result.success();
}
&#64;GetMapping("/roleMenu/{roleId}")
public Result getRoleMenu(&#64;PathVariable Integer roleId){
return Result.success(roleService.getRoleMenu(roleId));
}
- 在service层中实现业务
RoleService.java
&#64;Service
public class RoleService extends ServiceImpl<RoleMapper, Role> {
&#64;Autowired
private RoleMenuService roleMenuService;
&#64;Autowired
private RoleMenuMapper roleMenuMapper;
&#64;Transactional
public void setRoleMenu(Integer roleId, List<Integer> menuIds) {
QueryWrapper<RoleMenu> queryWrapper &#61; new QueryWrapper<>();
queryWrapper.eq("role_id",roleId);
roleMenuService.remove(queryWrapper);
for (Integer menuId:menuIds){
RoleMenu roleMenu &#61; new RoleMenu();
roleMenu.setRoleId(roleId);
roleMenu.setMenuId(menuId);
roleMenuService.save(roleMenu);
}
}
public List<Integer> getRoleMenu(Integer roleId) {
return roleMenuMapper.selectByRoleId(roleId);
}
}
- RoleMenuMapper.java
&#64;Repository
public interface RoleMenuMapper extends BaseMapper<RoleMenu> {
&#64;Select("select menu_id from role_menu where role_id &#61; #{roleId}")
List<Integer> selectByRoleId(&#64;Param("roleId") Integer roleId);
}
- 在前端中实现
saveRoleMenu(){
this.request.post("/role/roleMenu/" &#43; this.roleId,this.$refs.tree.getCheckedKeys()).then(res &#61;> {
if(res.code &#61;&#61;&#61; &#39;200&#39;) {
this.$message.success("绑定成功")
this.menuDialogVisable &#61; false
}else{
this.$message.error(res.msg)
}
})
},
- 请求后端查询出存储的关系然后回显到前端&#xff08;就是在再一次打开该角色的分配菜单时&#xff0c;要默认显示出之前已经勾选的&#xff09;
this.request.get("/role/roleMenu/" &#43; roleId).then(res &#61;> {
this.checks &#61; res.data
})
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YXBNwzKL-1673355144547)(D:\桌面\Java学习\项目\管理系统–前后端分离\项目截图\48.jpg)]
这次改动的前端代码就是Role.vue和Menu.vue两个页面&#xff0c;完整代码在前面已经给出了&#xff01;
通过实现&#xff0c;完成这两个功能&#xff0c;就成功了&#xff01;