西安做网站建设的公司,公司网站 建设,wordpress 演示导入,永康建设局网站电话目录 前言#xff1a; 
二. element ui 2.1官网提供的核心代码 
三.表结构 
编辑  
四.后端 
4.1功能分析 
4.2实体类 
4.3 查询全部权限显示的结果 
4.2修改角色权限的后台方法  五.vue 
5.0代码总览 
5.1树形图 5.2所需要的绑定数据 
5.3所需方法 前言#xff1a; 
先上图…目录 前言 
二. element ui 2.1官网提供的核心代码 
三.表结构 
编辑  
四.后端 
4.1功能分析 
4.2实体类 
4.3 查询全部权限显示的结果 
4.2修改角色权限的后台方法  五.vue 
5.0代码总览 
5.1树形图 5.2所需要的绑定数据 
5.3所需方法 前言 
先上图看效果页面不是很美观 二. element ui 2.1官网提供的核心代码 
el-tree:datadatashow-checkboxdefault-expand-allnode-keyidreftreehighlight-current:propsdefaultProps
/el-treediv classbuttonsel-button clickgetCheckedNodes通过 node 获取/el-buttonel-button clickgetCheckedKeys通过 key 获取/el-buttonel-button clicksetCheckedNodes通过 node 设置/el-buttonel-button clicksetCheckedKeys通过 key 设置/el-buttonel-button clickresetChecked清空/el-button
/divscriptexport default {methods: {getCheckedNodes() {console.log(this.$refs.tree.getCheckedNodes());},getCheckedKeys() {console.log(this.$refs.tree.getCheckedKeys());},setCheckedNodes() {this.$refs.tree.setCheckedNodes([{id: 5,label: 二级 2-1}, {id: 9,label: 三级 1-1-1}]);},setCheckedKeys() {this.$refs.tree.setCheckedKeys([3]);},resetChecked() {this.$refs.tree.setCheckedKeys([]);}},data() {return {data: [{id: 1,label: 一级 1,children: [{id: 4,label: 二级 1-1,children: [{id: 9,label: 三级 1-1-1}, {id: 10,label: 三级 1-1-2}]}]}, {id: 2,label: 一级 2,children: [{id: 5,label: 二级 2-1}, {id: 6,label: 二级 2-2}]}, {id: 3,label: 一级 3,children: [{id: 7,label: 二级 3-1}, {id: 8,label: 二级 3-2}]}],defaultProps: {children: children,label: label}};}};
/script打开官网找到Tree树形控件第一次应该很难看懂我来详细跟大家讲解一下 1. el-tree   :datadata   show-checkbox   default-expand-all   node-keyid   reftree   highlight-current   :propsdefaultProps /el-tree   :datadata 绑定的数据叫datadata可以替换成自己写的集合  node-key里的id对应的是数据库中表的id     reftree 表示这个树形图。 :propsdefaultProps  用来设置树形图的属性 说白了 就是设置节点叫啥子节点是什么集合。估计还是很懵到时候直接带大家看项目  2.    getCheckedNodes() {         console.log(this.$refs.tree.getCheckedNodes());       }, 将勾选的节点以json集合的形式打印到控制台       getCheckedKeys() {         console.log(this.$refs.tree.getCheckedKeys());       }, 将勾选中的节点以字符串数组的形式打印到控制台    3.  setCheckedKeys() {         this.$refs.tree.setCheckedKeys([3]);       },      设置选中的节点的key值将其样式改为勾选。 里面填的是3就是把id为3的节点设置为选中状态  setCheckedNodes() {         this.$refs.tree.setCheckedNodes([{           id: 5,           label: 二级 2-1         }, {           id: 9,           label: 三级 1-1-1         }]);       }, 通过json的集合类型来设置选中的节点 4. data: [{           id: 1,           label: 一级 1,           children: [{             id: 4,             label: 二级 1-1,             children: [{               id: 9,               label: 三级 1-1-1             }, {               id: 10,               label: 三级 1-1-2             }]           }]         }, {           id: 2,           label: 一级 2,           children: [{             id: 5,             label: 二级 2-1           }, {             id: 6,             label: 二级 2-2           }]         }, {           id: 3,           label: 一级 3,           children: [{             id: 7,             label: 二级 3-1           }, {             id: 8,             label: 二级 3-2           }]         }], 该示例 树形图绑定的数据类型为datadata数据内部细节为父子孙结构按照该结构显示到前端页面上。树形图的显示是由绑定的数据结构来决定的。 三.表结构 四.后端 
4.1功能分析 1.查询所有的权限将含有所有权限的集合跟树形图绑定 2.查询不同的角色所拥有的不同权限用集合存储并调用setCheckedNodes用来设置选中的节点  3.修改节点功能 删除该角色所拥有的权限 添加该角色的权限 前端勾选节点将已勾选的节点的id打包成string数组转换成字符串传给服务器。服务器通过对象的属性保存分割后的字符串id。 4.2实体类 
package com.dmdd.javasecuritypractice.entity;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;import java.io.Serializable;
import java.util.List;/*** p* * /p** author xray* since 2023-01-31*/
Data
public class Permission implements Serializable {private static final long serialVersionUID  1L;TableId(value  id, type  IdType.AUTO)private Integer id;private String name;private String description;private String url;private Integer pid;private String icon;private Integer sort;//子权限集合TableField(exist  false)private ListPermission subPermissions;Overridepublic String toString() {return Permission{ id  id , name  name , description  description , url  url , pid  pid , icon  icon , sort  sort };}
}通过一级权限查询二级权限实体类里面定义一个集合用来存储二级权限  4.3 查询全部权限显示的结果 包含所有权限的集合的数据形式 将该集合传给前端树形控件绑定该集合就会以该方式显示到页面上。 4.2修改角色权限的后台方法  //1,2,3,4,5 中间表的 permission_id permission的 idTransactionalOverridepublic void setRolePermissions(Long roleId, String permissions) {ArrayListRolePermission rolePermissions  new ArrayList();//清空该角色权限
//        boolean b  this.removeById(roleId);boolean b  this.remove(new QueryWrapperRolePermission().lambda().eq(RolePermission::getRoleId, roleId));if (b) {//将权限依次赋值给rolePermissionString[] strings  permissions.split(,);for (String permissionId : strings) {RolePermission rolePermission  new RolePermission();//设置当前角色id 权限id id会自增rolePermission.setRoleId(roleId);rolePermission.setPermissionId(Long.valueOf(permissionId));rolePermissions.add(rolePermission);}//mybats-plus批量添加方法boolean b1  this.saveBatch(rolePermissions);}} 对中间表进行操作实现角色权限的修改  五.vue 
5.0代码总览 
templatediv
!--    添加角色的树形结构--!--权限树对话框--el-dialog title权限信息 :visible.syncdialogTreeVisibleel-tree:dataallPermissionsshow-checkboxdefault-expand-allnode-keyidreftreehighlight-current:propsdefaultProps/el-treediv slotfooter classdialog-footerel-button clickdialogTreeVisible  false取 消/el-buttonel-button typeprimary clicksetRolePermissions()确 定/el-button/div/el-dialogel-button typetext clickdialogFormVisible true新增模块/el-buttonel-dialog title权限操作 :visible.syncdialogFormVisibleel-form :modelroleel-form-item label编号 :label-widthformLabelWidthel-input v-modelrole.id autocompleteoff/el-input/el-form-itemel-form-item label用户名 :label-widthformLabelWidthel-input v-modelrole.name autocompleteoff/el-input/el-form-itemel-form-item label描述 :label-widthformLabelWidthel-input v-modelrole.description autocompleteoff/el-input/el-form-itemel-form-item label地区id :label-widthformLabelWidthel-input v-modelrole.siteId autocompleteoff/el-input/el-form-item/el-formdiv slotfooter classdialog-footerel-button clickdialogFormVisible  false取 消/el-buttonel-button typeprimary clickupdate()确 定/el-button/div/el-dialogel-table:datarolesstylewidth: 100%el-table-columnlabelidwidth180template slot-scopescopei classel-icon-time/ispan stylemargin-left: 10px{{ scope.row.id }}/span/template/el-table-columnel-table-columnlabel用户名width180template slot-scopescopeel-popover triggerhover placementtopp姓名: {{ scope.row.name }}/ppid: {{ scope.row.id }}/pdiv slotreference classname-wrapperel-tag sizemedium{{ scope.row.name }}/el-tag/div/el-popover/template/el-table-columnel-table-columnlabel描述width180template slot-scopescopei classel-icon-time/ispan stylemargin-left: 10px{{ scope.row.description }}/span/template/el-table-columnel-table-columnlabelsiteIdwidth180template slot-scopescopei classel-icon-time/ispan stylemargin-left: 10px{{ scope.row.siteId }}/span/template/el-table-columnel-table-column label操作template slot-scopescopeel-buttonsizeminiclickhandleEdit(scope.$index, scope.row)编辑/el-buttonel-buttonsizeminiclickopenTree(scope.row.id)修改权限/el-buttonel-buttonsizeminitypedangerclickhandleDelete(scope.$index, scope.row)删除/el-button/template/el-table-column/el-table//分页功能el-paginationbackgroundlayoutprev, pager, next:totaltotal:page-sizepageSizecurrent-changeloadRole/el-pagination/div
/templatescript
export default {name: RoleView,data() {return {roles: [],role: {},current: 1,total: 0,pageSize: 10,dialogFormVisible: false,formLabelWidth: 120px,dialogTreeVisible: false,allPermissions: [],rolePermissions:[],defaultProps: {children: subPermissions,label: name},RoleId:-1}},methods:{//点击修改角色权限 弹出树形图openTree(roleId){this.RoleIdroleId;this.dialogTreeVisibletrue//查询所有权限通过角色this.axios.get(http://localhost:8080/permissionsByRole).then(result{console.log(所有权限:result)if (result.data.statusOK){this.allPermissionsresult.data.data;//查询该角色拥有的权限this.axios.get(http://localhost:8080/permissionsByRoleId?RoleIdroleId).then(result{console.log(当前角色的权限result)if (result.data.statusOK){this.rolePermissionsresult.data.data;//设置勾选效果this.$refs.tree.setCheckedNodes(this.rolePermissions);}})}})},//设置角色权限setRolePermissions(){console.log(this.$refs.tree.getCheckedKeys());//将数组转换成字符串let keys  this.$refs.tree.getCheckedKeys();let keyStrfor (let i0;ikeys.length;i){keyStrkeys[i],;}keyStr.substr(0,keyStr.length-1);//调用后台接口this.axios.post(http://localhost:8080/permission/set-role,roleIdthis.RoleIdpermissionskeyStr).then(result{if (result.data.statusOK){this.$message(result.data.data);}})this.dialogTreeVisiblefalse;},//查询角色表loadRole(current,pageSize){this.currentcurrent;this.axios.get(http://localhost:8080/role/page?currentthis.currentpageSizethis.pageSize).then(result{console.log(result)this.rolesresult.data.data.recordsthis.totalresult.data.data.total})},update() {if (this.role.id) {//执行修改方法this.axios.put(http://localhost:8080/role, this.role).then(result  {if (result.data.status  OK) {console.log(result.data)this.role{}this.dialogFormVisiblefalsethis.loadRole(this.current)}})}else{this.axios.post(http://localhost:8080/role,this.role).then(result{if (result.data.statusOK){console.log(result)this.role{}this.dialogFormVisiblefalsethis.loadRole(1)}})}},//回显handleEdit(index,row){this.roleJSON.parse(JSON.stringify(row));this.dialogFormVisibletrue},},mounted() {this.loadRole(1)}
}
/scriptstyle scoped/style 
5.1树形图 !--    添加角色的树形结构--!--权限树对话框--el-dialog title权限信息 :visible.syncdialogTreeVisibleel-tree:dataallPermissionsshow-checkboxdefault-expand-allnode-keyidreftreehighlight-current:propsdefaultProps/el-treediv slotfooter classdialog-footerel-button clickdialogTreeVisible  false取 消/el-buttonel-button typeprimary clicksetRolePermissions()确 定/el-button/div/el-dialog 树形图控件  5.2所需要的绑定数据 allPermissions: [],rolePermissions:[],defaultProps: {children: subPermissions,label: name},RoleId:-1 defaultProps: {         children: subPermissions,         label: name       }, 设置树形图父节点下的集合名字为subPermissions.  该集合名allPermissions是集合里的对象的属性 label 表示每个节点的名字 我设置的name表示的是 allPermissions里的对象的name属性值 5.3所需方法 //点击修改角色权限 弹出树形图openTree(userId) {this.userId  userId;this.dialogTreeVisible  true//查询所有权限通过角色this.axios.get(http://localhost:8080/selectAllRole).then(result  {console.log(所有权限:  result)if (result.data.status  OK) {this.allRoles  result.data.data;//查询该角色拥有的权限this.axios.get(http://localhost:8080/select-role?userId  userId).then(result  {console.log(当前角色的权限  result)if (result.data.status  OK) {this.userRoles  result.data.data;//设置勾选效果this.$refs.tree.setCheckedNodes(this.userRoles);}})}})},//设置用户的角色setUserRoles() {console.log(this.$refs.tree.getCheckedKeys());//将数组转换成字符串let keys  this.$refs.tree.getCheckedKeys();let keyStr  for (let i  0; i  keys.length; i) {keyStr  keys[i]  ,;}keyStr.substr(0, keyStr.length - 1);//调用后台接口this.axios.post(http://localhost:8080/setRole, userId  this.userId  roles  keyStr).then(result  {// if (result.request.status  200) {this.$message(result.data);// }})this.dialogTreeVisible  false;}, 点击修改权限的按钮设置树形图为可见显示所有节点数据设置勾选节点是哪些。 1.将后端的通过角色查询到的权限的集合传入前台通过 this.$refs.tree.setCheckedNodes(this.rolePermissions);
设置勾选节点其他的大家可以自己去体会如果完全了解的话不管是怎么样的数据结构的树形图都会写 最后附上我的另一个通过用户查询角色权限的实现图。  文章转载自: http://www.morning.lbgsh.cn.gov.cn.lbgsh.cn http://www.morning.qwpyf.cn.gov.cn.qwpyf.cn http://www.morning.xtrnx.cn.gov.cn.xtrnx.cn http://www.morning.hytqt.cn.gov.cn.hytqt.cn http://www.morning.qbwtb.cn.gov.cn.qbwtb.cn http://www.morning.wwgpy.cn.gov.cn.wwgpy.cn http://www.morning.ztdlp.cn.gov.cn.ztdlp.cn http://www.morning.myhpj.cn.gov.cn.myhpj.cn http://www.morning.flmxl.cn.gov.cn.flmxl.cn http://www.morning.rlnm.cn.gov.cn.rlnm.cn http://www.morning.ptxwg.cn.gov.cn.ptxwg.cn http://www.morning.drqrl.cn.gov.cn.drqrl.cn http://www.morning.fnmgr.cn.gov.cn.fnmgr.cn http://www.morning.fosfox.com.gov.cn.fosfox.com http://www.morning.ckbmz.cn.gov.cn.ckbmz.cn http://www.morning.kyfrl.cn.gov.cn.kyfrl.cn http://www.morning.rgxcd.cn.gov.cn.rgxcd.cn http://www.morning.xqqcq.cn.gov.cn.xqqcq.cn http://www.morning.fncgw.cn.gov.cn.fncgw.cn http://www.morning.mwnch.cn.gov.cn.mwnch.cn http://www.morning.srbsr.cn.gov.cn.srbsr.cn http://www.morning.lwnwl.cn.gov.cn.lwnwl.cn http://www.morning.rljr.cn.gov.cn.rljr.cn http://www.morning.jqpq.cn.gov.cn.jqpq.cn http://www.morning.zztmk.cn.gov.cn.zztmk.cn http://www.morning.xsrnr.cn.gov.cn.xsrnr.cn http://www.morning.bdwqy.cn.gov.cn.bdwqy.cn http://www.morning.rjkfj.cn.gov.cn.rjkfj.cn http://www.morning.qqtzn.cn.gov.cn.qqtzn.cn http://www.morning.kpgbz.cn.gov.cn.kpgbz.cn http://www.morning.ppllj.cn.gov.cn.ppllj.cn http://www.morning.rxcqt.cn.gov.cn.rxcqt.cn http://www.morning.mstbbs.com.gov.cn.mstbbs.com http://www.morning.tbjtm.cn.gov.cn.tbjtm.cn http://www.morning.qjlkp.cn.gov.cn.qjlkp.cn http://www.morning.jbztm.cn.gov.cn.jbztm.cn http://www.morning.mytmx.cn.gov.cn.mytmx.cn http://www.morning.dtnzk.cn.gov.cn.dtnzk.cn http://www.morning.rysmn.cn.gov.cn.rysmn.cn http://www.morning.bwhcl.cn.gov.cn.bwhcl.cn http://www.morning.mwjwy.cn.gov.cn.mwjwy.cn http://www.morning.wcjgg.cn.gov.cn.wcjgg.cn http://www.morning.grpbt.cn.gov.cn.grpbt.cn http://www.morning.yjxfj.cn.gov.cn.yjxfj.cn http://www.morning.sfgtp.cn.gov.cn.sfgtp.cn http://www.morning.dtlnz.cn.gov.cn.dtlnz.cn http://www.morning.kybpj.cn.gov.cn.kybpj.cn http://www.morning.rgwrl.cn.gov.cn.rgwrl.cn http://www.morning.psxwc.cn.gov.cn.psxwc.cn http://www.morning.qhmql.cn.gov.cn.qhmql.cn http://www.morning.ygrdb.cn.gov.cn.ygrdb.cn http://www.morning.gyzfp.cn.gov.cn.gyzfp.cn http://www.morning.gczqt.cn.gov.cn.gczqt.cn http://www.morning.tbzcl.cn.gov.cn.tbzcl.cn http://www.morning.msbmp.cn.gov.cn.msbmp.cn http://www.morning.mcgsq.cn.gov.cn.mcgsq.cn http://www.morning.kkgbs.cn.gov.cn.kkgbs.cn http://www.morning.hmktd.cn.gov.cn.hmktd.cn http://www.morning.mcwrg.cn.gov.cn.mcwrg.cn http://www.morning.qsyyp.cn.gov.cn.qsyyp.cn http://www.morning.yknsr.cn.gov.cn.yknsr.cn http://www.morning.rtsdz.cn.gov.cn.rtsdz.cn http://www.morning.nqmwk.cn.gov.cn.nqmwk.cn http://www.morning.rbffj.cn.gov.cn.rbffj.cn http://www.morning.fpxms.cn.gov.cn.fpxms.cn http://www.morning.xnhnl.cn.gov.cn.xnhnl.cn http://www.morning.wgzgr.cn.gov.cn.wgzgr.cn http://www.morning.tnwgc.cn.gov.cn.tnwgc.cn http://www.morning.mrcpy.cn.gov.cn.mrcpy.cn http://www.morning.xiaobaixinyong.cn.gov.cn.xiaobaixinyong.cn http://www.morning.rykx.cn.gov.cn.rykx.cn http://www.morning.ktblf.cn.gov.cn.ktblf.cn http://www.morning.gnghp.cn.gov.cn.gnghp.cn http://www.morning.yrkdq.cn.gov.cn.yrkdq.cn http://www.morning.51meihou.cn.gov.cn.51meihou.cn http://www.morning.qhrlb.cn.gov.cn.qhrlb.cn http://www.morning.ruifund.com.gov.cn.ruifund.com http://www.morning.rkrl.cn.gov.cn.rkrl.cn http://www.morning.mhnxs.cn.gov.cn.mhnxs.cn http://www.morning.ygkk.cn.gov.cn.ygkk.cn