• vue 权限分组


    在权限设计时,分为部门、岗位、人员三部分, 人员下面挂部门,部门下面挂岗位,岗位下面挂菜单,用户在进行授权时,勾选了操作权限,默认数据权限也进行勾选。

    权限组:查询、新增、修改、删除

    用户交互如下:

    1、选中新增、修改、删除中的任何一个复选框时,查询框也选中。

    2、选中查询框时,新增、修改、删除不变。

    3、取消选中查询框时,新增、修改、删除都取消选中。

    代码如下:

    1. <script>
    2. export default {
    3. data() {
    4. return {
    5. menuTree: [
    6. {
    7. id: '1717148200757',
    8. code: 'departmentManagement',
    9. title: '部门管理 ',
    10. name: '部门管理 ',
    11. parentIdId: null,
    12. children: [
    13. {
    14. id: '1717148287418',
    15. parentIdId: '1717148200757',
    16. code: 'department',
    17. title: '部门',
    18. name: '部门 ',
    19. children: [
    20. {
    21. id: 'department:query',
    22. code: 'department:query',
    23. name: '查询',
    24. title: '查询',
    25. parentIdId: '1717148287418',
    26. dataPermission: true
    27. },
    28. {
    29. id: 'department:add',
    30. code: 'department:add',
    31. name: '新增',
    32. title: '新增',
    33. parentIdId: '1717148287418',
    34. dataPermission: false
    35. },
    36. {
    37. id: 'department:update',
    38. code: 'department:update',
    39. name: '修改',
    40. title: '修改',
    41. parentIdId: '1717148287418',
    42. dataPermission: false
    43. },
    44. {
    45. id: 'department:delete',
    46. code: 'department:delete',
    47. name: '删除',
    48. title: '删除',
    49. parentIdId: '1717148287418',
    50. dataPermission: false
    51. }
    52. ]
    53. }
    54. ]
    55. },
    56. {
    57. id: '1717148223729',
    58. code: 'jobManagement',
    59. title: '岗位管理',
    60. name: '岗位管理',
    61. parentId: null,
    62. children: [
    63. {
    64. id: '1717148366037',
    65. parentId: '1717148223729',
    66. code: 'jobInside',
    67. title: '岗位',
    68. name: '岗位',
    69. children: [
    70. {
    71. id: 'permissionManagement:position:query',
    72. code: 'permissionManagement:position:query',
    73. name: '查询',
    74. title: '查询',
    75. parentIdId: '1717148366037',
    76. dataPermission: true
    77. },
    78. {
    79. id: 'permissionManagement:position:add',
    80. code: 'permissionManagement:position:add',
    81. name: '新增',
    82. title: '新增',
    83. parentIdId: '1717148366037',
    84. dataPermission: false
    85. },
    86. {
    87. id: 'permissionManagement:position:update',
    88. code: 'permissionManagement:position:update',
    89. name: '修改',
    90. title: '修改',
    91. parentIdId: '1717148366037',
    92. dataPermission: false
    93. },
    94. {
    95. id: 'permissionManagement:position:delete',
    96. code: 'permissionManagement:position:delete',
    97. name: '删除',
    98. title: '删除',
    99. parentIdId: '1717148366037',
    100. dataPermission: false
    101. }
    102. ]
    103. }
    104. ]
    105. },
    106. {
    107. id: '1717148248870',
    108. code: 'userManagement',
    109. title: '用户管理',
    110. name: '用户管理',
    111. parentId: null,
    112. children: [
    113. {
    114. id: '1717148408202',
    115. parentId: '1717148248870',
    116. code: 'userAdmin',
    117. title: '用户',
    118. name: '用户',
    119. children: [
    120. {
    121. id: 'userAdmin:query',
    122. code: 'userAdmin:query',
    123. name: '查询',
    124. title: '查询',
    125. parentIdId: '1717148408202',
    126. dataPermission: true
    127. },
    128. {
    129. id: 'userAdmin:add',
    130. code: 'userAdmin:add',
    131. name: '新增',
    132. title: '新增',
    133. parentIdId: '1717148408202',
    134. dataPermission: false
    135. },
    136. {
    137. id: 'userAdmin:update',
    138. code: 'userAdmin:update',
    139. name: '修改',
    140. title: '修改',
    141. parentIdId: '1717148408202',
    142. dataPermission: false
    143. },
    144. {
    145. id: 'userAdmin:delete',
    146. code: 'userAdmin:delete',
    147. name: '删除',
    148. title: '删除',
    149. parentIdId: '1717148408202',
    150. dataPermission: false
    151. }
    152. ]
    153. }
    154. ]
    155. }
    156. ]
    157. };
    158. },
    159. methods: {
    160. /**
    161. * 复选框选中事件
    162. * 实现的效果:
    163. * 1、查询节点选中时,其相邻兄弟节点不变
    164. * 2、查询节点取消选中时,其相邻兄弟节点取消选中
    165. * 3、其相邻兄弟节点任何一个选中时,查询节点必选中
    166. * 注:查询节点 dataPermission:true
    167. * **/
    168. handleCheck(data) {
    169. if (!data.children) {
    170. // 获取当前叶子节点的兄弟节点
    171. const sublingsNode = this.getSiblingsById(this.menuTree, data.id);
    172. // 获取当前叶子节点中为查询节点的项
    173. const dataPermissionItem = [data, ...sublingsNode].find(
    174. (item) => item.dataPermission
    175. );
    176. if (data.dataPermission) {
    177. // 操作的是查询节点时,将其兄弟节点设置为取消选中状态
    178. sublingsNode.forEach((item) => {
    179. this.$refs.treeRef.setChecked(item, false);
    180. });
    181. } else {
    182. // 操作的是兄弟节点时,将查询节点设置为选中状态
    183. this.$refs.treeRef.setChecked(dataPermissionItem, true);
    184. }
    185. }
    186. },
    187. /**
    188. * 通过当前节点的 id 查找兄弟节点
    189. * treeData:树形结构的数据源
    190. * nodeId:当前节点的 id
    191. * **/
    192. getSiblingsById(treeData, nodeId) {
    193. function findSiblings(nodes) {
    194. for (const node of nodes) {
    195. if (node.id === nodeId) {
    196. return nodes.filter((item) => item.id !== nodeId);
    197. }
    198. if (node.children) {
    199. const siblings = findSiblings(node.children, node.id);
    200. if (siblings) return siblings;
    201. }
    202. }
    203. return null;
    204. }
    205. return findSiblings(treeData, null);
    206. }
    207. }
    208. };
    209. script>

    总结:

    在写这部分逻辑时,难点在于取消数据权限时,怎么将操作权限也进行取消,这部分的交互相当于是查询是新增、修改、删除的取消全选操作。在进行这部分交互效果实现时,有考虑过使用 current-change 这个事件,因为这个事件可以获取到一个 Node 对象,在 Node 对象中可以获取到当前节点父节点,从而在父节点下找到当前节点及兄弟节点,从而避免了使用递归的方式查找兄弟节点,这个事件有个问题,无法触发 check 事件,导致在点击复选框选中时,事件未触发,所以退而求其次考虑使用 check 事件,配合递归方法实现最终的交互效果。

    补充:

    可以将递归函数去掉,使用 elementUI 中提供的 getNode 方法获取当前节点的兄弟节点,修改如下:

    1. <script>
    2. export default {
    3. data() {
    4. return {
    5. menuTree: [
    6. {
    7. id: '1717148200757',
    8. code: 'departmentManagement',
    9. title: '部门管理 ',
    10. name: '部门管理 ',
    11. parentIdId: null,
    12. children: [
    13. {
    14. id: '1717148287418',
    15. parentIdId: '1717148200757',
    16. code: 'department',
    17. title: '部门',
    18. name: '部门 ',
    19. children: [
    20. {
    21. id: 'department:query',
    22. code: 'department:query',
    23. name: '查询',
    24. title: '查询',
    25. parentIdId: '1717148287418',
    26. dataPermission: true
    27. },
    28. {
    29. id: 'department:add',
    30. code: 'department:add',
    31. name: '新增',
    32. title: '新增',
    33. parentIdId: '1717148287418',
    34. dataPermission: false
    35. },
    36. {
    37. id: 'department:update',
    38. code: 'department:update',
    39. name: '修改',
    40. title: '修改',
    41. parentIdId: '1717148287418',
    42. dataPermission: false
    43. },
    44. {
    45. id: 'department:delete',
    46. code: 'department:delete',
    47. name: '删除',
    48. title: '删除',
    49. parentIdId: '1717148287418',
    50. dataPermission: false
    51. }
    52. ]
    53. }
    54. ]
    55. },
    56. {
    57. id: '1717148223729',
    58. code: 'jobManagement',
    59. title: '岗位管理',
    60. name: '岗位管理',
    61. parentId: null,
    62. children: [
    63. {
    64. id: '1717148366037',
    65. parentId: '1717148223729',
    66. code: 'jobInside',
    67. title: '岗位',
    68. name: '岗位',
    69. children: [
    70. {
    71. id: 'permissionManagement:position:query',
    72. code: 'permissionManagement:position:query',
    73. name: '查询',
    74. title: '查询',
    75. parentIdId: '1717148366037',
    76. dataPermission: true
    77. },
    78. {
    79. id: 'permissionManagement:position:add',
    80. code: 'permissionManagement:position:add',
    81. name: '新增',
    82. title: '新增',
    83. parentIdId: '1717148366037',
    84. dataPermission: false
    85. },
    86. {
    87. id: 'permissionManagement:position:update',
    88. code: 'permissionManagement:position:update',
    89. name: '修改',
    90. title: '修改',
    91. parentIdId: '1717148366037',
    92. dataPermission: false
    93. },
    94. {
    95. id: 'permissionManagement:position:delete',
    96. code: 'permissionManagement:position:delete',
    97. name: '删除',
    98. title: '删除',
    99. parentIdId: '1717148366037',
    100. dataPermission: false
    101. }
    102. ]
    103. }
    104. ]
    105. },
    106. {
    107. id: '1717148248870',
    108. code: 'userManagement',
    109. title: '用户管理',
    110. name: '用户管理',
    111. parentId: null,
    112. children: [
    113. {
    114. id: '1717148408202',
    115. parentId: '1717148248870',
    116. code: 'userAdmin',
    117. title: '用户',
    118. name: '用户',
    119. children: [
    120. {
    121. id: 'userAdmin:query',
    122. code: 'userAdmin:query',
    123. name: '查询',
    124. title: '查询',
    125. parentIdId: '1717148408202',
    126. dataPermission: true
    127. },
    128. {
    129. id: 'userAdmin:add',
    130. code: 'userAdmin:add',
    131. name: '新增',
    132. title: '新增',
    133. parentIdId: '1717148408202',
    134. dataPermission: false
    135. },
    136. {
    137. id: 'userAdmin:update',
    138. code: 'userAdmin:update',
    139. name: '修改',
    140. title: '修改',
    141. parentIdId: '1717148408202',
    142. dataPermission: false
    143. },
    144. {
    145. id: 'userAdmin:delete',
    146. code: 'userAdmin:delete',
    147. name: '删除',
    148. title: '删除',
    149. parentIdId: '1717148408202',
    150. dataPermission: false
    151. }
    152. ]
    153. }
    154. ]
    155. }
    156. ]
    157. };
    158. },
    159. methods: {
    160. /**
    161. * 复选框选中事件
    162. * 实现的效果:
    163. * 1、查询节点选中时,其相邻兄弟节点不变
    164. * 2、查询节点取消选中时,其相邻兄弟节点取消选中
    165. * 3、其相邻兄弟节点任何一个选中时,查询节点必选中
    166. * 注:查询节点 dataPermission:true
    167. * **/
    168. handleCheck(data, checked, indeterminate) {
    169. if (!data.children) {
    170. // 只处理叶子节点
    171. const parentNode = this.$refs.treeRef.getNode(data).parent;
    172. if (parentNode) {
    173. // 获取当前叶子节点的兄弟节点
    174. // const sublingsNode = this.getSiblingsById(this.treeData, data.id);
    175. // 获取当前叶子节点和兄弟节点
    176. const siblings = parentNode.data.children;
    177. const dataPermissionItem = siblings.find(
    178. (item) => item.dataPermission
    179. );
    180. if (data.dataPermission) {
    181. // 如果是查询节点,则取消选中所有非查询兄弟节点
    182. siblings.forEach((sibling) => {
    183. if (!sibling.dataPermission) {
    184. this.$refs.treeRef.setChecked(sibling, false, false);
    185. }
    186. });
    187. } else if (checked) {
    188. // 如果是非查询节点被选中,则确保查询节点也被选中
    189. this.$refs.treeRef.setChecked(dataPermissionItem, true, false);
    190. }
    191. }
    192. }
    193. }
    194. // /**
    195. // * 通过当前节点的 id 查找兄弟节点
    196. // * treeData:树形结构的数据源
    197. // * nodeId:当前节点的 id
    198. // * **/
    199. // getSiblingsById(treeData, nodeId) {
    200. // function findSiblings(nodes) {
    201. // for (const node of nodes) {
    202. // if (node.id === nodeId) {
    203. // return nodes.filter((item) => item.id !== nodeId);
    204. // }
    205. // if (node.children) {
    206. // const siblings = findSiblings(node.children, node.id);
    207. // if (siblings) return siblings;
    208. // }
    209. // }
    210. // return null;
    211. // }
    212. // return findSiblings(treeData, null);
    213. // }
    214. }
    215. };
    216. script>

  • 相关阅读:
    源码编译postgresql
    Java GC
    elasticsearch 聚合DSL语法
    linux创建用户和组、授权、禁止root远程登录、限制SSH的IP登录
    【SpringMVC】JSON数据传输与异常处理的使用
    重塑感知,荣耀金洋!金洋奖两项用户体验奖项公布
    ios ipa包上传需要什么工具
    如何让Join跑的更快?(文末送书)
    嵌入式硬件设计实例:基于STM32的流水灯原理图和PCB设计
    Java并发 | 15.[基础] 线程安全分析
  • 原文地址:https://blog.csdn.net/qq_37309987/article/details/139373222