Yansen 2 лет назад
Родитель
Сommit
824748cfc9
3 измененных файлов: 95 добавлений и 42 удалений
  1. 34
    8
      src/pages/roles/menus.jsx
  2. 0
    34
      src/utils/arr2Tree.js
  3. 61
    0
      src/utils/array.js

+ 34
- 8
src/pages/roles/menus.jsx Просмотреть файл

@@ -1,7 +1,7 @@
1 1
 import React from 'react';
2
-import { Button, Card, Tree, Modal } from 'antd';
2
+import { Button, Card, Tree, message } from 'antd';
3 3
 import useBool from '@/utils/hooks/useBool';
4
-import arr2Tree from '@/utils/arr2Tree';
4
+import { arr2Tree, uniq } from '@/utils/array';
5 5
 import { getResourceList } from '@/services/role';
6 6
 
7 7
 export default (props) => {
@@ -11,16 +11,33 @@ export default (props) => {
11 11
   const [list, setList] = React.useState([]);
12 12
   const [treeData, setTreeData] = React.useState([]);
13 13
   const treeDictRef = React.useRef();
14
+  const [checkedKeys, setCheckedKeys] = React.useState([]);
15
+  const checkedNodesRef = React.useState([]);
14 16
 
15 17
   const title = role ? `${role.name} - 授权菜单` : '授权菜单';
16 18
 
17
-  const onSelect = (selectedKeys, info) => {
18
-    console.log('selected', selectedKeys, info);
19
-  };
20
-  const onCheck = (checkedKeys, info) => {
21
-    console.log('onCheck', checkedKeys, info);
19
+  const onCheck = (keys, info) => {
20
+    const { checkedNodes } = info;
21
+    checkedNodesRef.current = checkedNodes;
22
+    // const keys = checkedNodes.map(node => getKeysWithParent(node, treeDictRef.current));
23
+    // setCheckedKeys(uniq(keys));
24
+    setCheckedKeys(keys);
22 25
   };
23 26
 
27
+  const onSubmit = () => {
28
+    if (!checkedNodesRef.current) {
29
+      return;
30
+    }
31
+
32
+    if (checkedNodesRef.current.length !== checkedKeys.length) {
33
+      message.warn('未知错误, 请刷新重试');
34
+      return;
35
+    }
36
+
37
+    const keys = checkedNodesRef.current.map(node => getKeysWithParent(node, treeDictRef.current));
38
+
39
+  }
40
+
24 41
   React.useEffect(() => {
25 42
     startLoading();
26 43
     getResourceList({ pageSize: 500, type: 'menu' }).then((res) => {
@@ -50,14 +67,23 @@ export default (props) => {
50 67
     <Card
51 68
       loading={loading}
52 69
       title={title}
53
-      extra={<Button type='primary' ghost>保存</Button>}
70
+      extra={<Button type='primary' ghost onClick={onSubmit}>保存</Button>}
54 71
     >
55 72
       <Tree
56 73
         checkable
74
+        autoExpandParent
75
+        checkStrictly={false}
57 76
         selectable={false}
77
+        checkedKeys={checkedKeys}
58 78
         onCheck={onCheck}
59 79
         treeData={treeData}
60 80
       />
61 81
     </Card>
62 82
   )
63 83
 }
84
+
85
+function getKeysWithParent(node, dict) {
86
+  if (node.parentId === -1) return [node.key];
87
+  const parentKeys = getKeysWithParent(dict[node.parentId], dict);
88
+  return parentKeys ? [node.key].concat(parentKeys) : [node.key];
89
+}

+ 0
- 34
src/utils/arr2Tree.js Просмотреть файл

@@ -1,34 +0,0 @@
1
-
2
-export default function arr2Tree(arr = [], parent = 'parentId', key = 'key') {
3
-  // 转换为字典
4
-  const dict = arr.reduce((acc, item) => {
5
-    return {
6
-      ...acc,
7
-      [item[key]]: {
8
-        ...item,
9
-        children: []
10
-      }
11
-    }
12
-  }, {});
13
-
14
-  // 挂载父子节点
15
-  const rootId = -1;
16
-  const tree = [];
17
-  for (let item of arr) {
18
-    const parentNodeId = item[parent];
19
-
20
-    if (parentNodeId === rootId) {
21
-      tree.push(dict[item[key]]);
22
-    } else {
23
-      const parentNode = dict[parentNodeId];
24
-      if (parentNode) {
25
-        parentNode.children.push(item);
26
-      }
27
-    }
28
-  }
29
-
30
-  console.log(tree, dict)
31
-
32
-  return [tree, dict];
33
-}
34
-

+ 61
- 0
src/utils/array.js Просмотреть файл

@@ -0,0 +1,61 @@
1
+
2
+/**
3
+ * 数组 转 Tree
4
+ * @param {*} arr 
5
+ * @param {*} parent 
6
+ * @param {*} key 
7
+ * @returns 
8
+ */
9
+export function arr2Tree(arr = [], parent = 'parentId', key = 'key') {
10
+  // 转换为字典
11
+  const dict = arr.reduce((acc, item) => {
12
+    return {
13
+      ...acc,
14
+      [item[key]]: {
15
+        ...item,
16
+        children: []
17
+      }
18
+    }
19
+  }, {});
20
+
21
+  // 挂载父子节点
22
+  const rootId = -1;
23
+  const tree = [];
24
+  for (let item of arr) {
25
+    const parentNodeId = item[parent];
26
+
27
+    if (parentNodeId === rootId) {
28
+      tree.push(dict[item[key]]);
29
+    } else {
30
+      const parentNode = dict[parentNodeId];
31
+      if (parentNode) {
32
+        parentNode.children.push(item);
33
+      }
34
+    }
35
+  }
36
+
37
+  return [tree, dict];
38
+}
39
+
40
+/**
41
+ * 深度展平数组
42
+ * @param {*} arr 
43
+ * @returns 
44
+ */
45
+export function flatten(arr = []) {
46
+  return arr.reduce((acc, it) => {
47
+    const list = Array.isArray(it) ? flatten(it) : it
48
+    return acc.concat(list);
49
+  }, []);
50
+}
51
+
52
+/**
53
+ * 深度数组去重
54
+ * @param {*} arr 
55
+ */
56
+export function uniq(arr = []) {
57
+  const list = flatten(arr);
58
+  return list.reduce((acc, it) => {
59
+    return acc.indexOf(it) > -1 ? acc : acc.concat(it)
60
+  }, []);
61
+}