Permissiontree.jsx 5.64 KB
Newer Older
TZW's avatar
TZW committed
1 2 3 4 5 6 7
import React, { useState, useEffect, useReducer } from 'react';
import { Button, Tree, message } from 'antd';
import { roleTree, adminDataqueryAll } from '@/services/system';
import { doFetch } from '@/utils/doFetch';
import { useRequest } from 'ahooks';
const TreeNode = Tree.TreeNode;
const initState = {
TZW's avatar
TZW committed
8 9 10
  pcList: [],
  havepremIdList: [],
  haveParentIdList: [],
TZW's avatar
TZW committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24
  appTree: [],
  appCheckedKeys: [],
  appHalfCheckedKeys: [],
  daauthTree: [],
  daauthCheckkedKeys: [],
  daauthHalfCheckedKeys: [],
};
function reducer(state, action) {
  let { type } = action,
    newState = {};
  switch (type) {
    case 'changePc':
      newState = {
        ...state,
TZW's avatar
TZW committed
25 26
        havepremIdList: [...action.checkedKeys],
        haveParentIdList: [...action.halfCheckedKeys],
TZW's avatar
TZW committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
      };
      break;
    case 'changeApp':
      newState = {
        ...state,
        appCheckedKeys: [...action.checkedKeys],
        appHalfCheckedKeys: [...action.halfCheckedKeys],
      };
      break;
    case 'changeDaauth':
      newState = {
        ...state,
        daauthCheckkedKeys: [...action.checkedKeys],
        daauthHalfCheckedKeys: [...action.halfCheckedKeys],
      };
      break;
    case 'reset':
      newState = {
        ...state,
TZW's avatar
TZW committed
46 47 48
        pcList: [...action.pcList],
        havepremIdList: [...action.havepremIdList],
        haveParentIdList: [...action.haveParentIdList],
TZW's avatar
TZW committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
        // appTree: [...action.appTree],
        // appCheckedKeys: [...action.appCheckedKeys],
        // appHalfCheckedKeys: [...action.appHalfCheckedKeys]
      };
      break;
    case 'daauth':
      newState = {
        ...state,
        daauthTree: action.nodeList ? [...action.nodeList] : [],
        daauthCheckkedKeys: action.havepremIdList ? [...action.havepremIdList] : [],
        daauthHalfCheckedKeys: action.haveParentIdList ? [...action.haveParentIdList] : [],
      };
      break;
  }

  return newState;
}

const Permissiontree = (props) => {
  const { id, close, treeType } = props,
    [state, dispatch] = useReducer(reducer, initState),
    {
TZW's avatar
TZW committed
71 72 73
      pcList,
      havepremIdList,
      haveParentIdList,
TZW's avatar
TZW committed
74 75 76 77 78 79 80 81
      appTree,
      appCheckedKeys,
      appHalfCheckedKeys,
      daauthCheckkedKeys,
      daauthHalfCheckedKeys,
      daauthTree,
    } = state;

TZW's avatar
TZW committed
82 83 84
  useEffect(() => {
    resetData();
  }, []);
TZW's avatar
TZW committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
  const loop = (data) => {
      return data.map((item) => {
        if (item.children) {
          return (
            <TreeNode key={item.key} title={item.title}>
              {loop(item.children)}
            </TreeNode>
          );
        }
        return <TreeNode key={item.key} title={item.title} />;
      });
    },
    pcCheck = (checkedKeys, info) => {
      dispatch({ type: 'changePc', checkedKeys, halfCheckedKeys: info.halfCheckedKeys });
    },
    appCheck = (checkedKeys, info) => {
      dispatch({ type: 'changeApp', checkedKeys, halfCheckedKeys: info.halfCheckedKeys });
    },
    daauthCheck = (checkedKeys, info) => {
      dispatch({ type: 'changeDaauth', checkedKeys, halfCheckedKeys: info.halfCheckedKeys });
    },
    { run, loading } = useRequest(doFetch, {
      manual: true,
      onSuccess: (result, params) => {
        if (result.code == '0000') {
TZW's avatar
TZW committed
110
          message.success('权限配置成功!');
TZW's avatar
TZW committed
111 112 113 114 115 116 117 118 119 120 121 122 123
          close();
        }
      },
    });

  function resetData() {
    if (!id) {
      return;
    }
    if (treeType == 'auth') {
      roleTree({ roleId: id }).then((res) => {
        if (res.code == '0000') {
          let data = res?.data || {};
TZW's avatar
TZW committed
124 125
          const { pcList, havepremIdList, haveParentIdList } = data;
          dispatch({ type: 'reset', pcList, havepremIdList, haveParentIdList });
TZW's avatar
TZW committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139
        }
      });
    } else {
      // adminDataqueryAll({ roleId: id }).then(res => {
      //     if (res.code == "0000") {
      //         let data = res?.data || {};
      //         const { nodeList, havepremIdList, haveParentIdList } = data;
      //         dispatch({ type: "daauth", nodeList, havepremIdList, haveParentIdList })
      //     }
      // })
    }
  }
  const saveData = () => {
    run({
TZW's avatar
TZW committed
140
      url: '/auth/sysRolePermission/save',
TZW's avatar
TZW committed
141 142
      params: {
        roleId: id,
TZW's avatar
TZW committed
143
        permissionIds: [...havepremIdList, ...haveParentIdList],
TZW's avatar
TZW committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157
      },
    });
  };

  return (
    <div>
      <div style={{ width: '100%', display: 'flex', overflow: 'hidden', minHeight: 100 }}>
        {treeType == 'auth' ? (
          <>
            <div style={{ flex: 1, minHeight: 100 }}>
              <h3 style={{ marginBottom: 16 }}>请配置角色权限:</h3>
              <Tree
                checkable
                defaultExpandAll={false}
TZW's avatar
TZW committed
158
                checkedKeys={havepremIdList}
TZW's avatar
TZW committed
159 160
                onCheck={pcCheck}
              >
TZW's avatar
TZW committed
161
                {loop(pcList || [])}
TZW's avatar
TZW committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
              </Tree>
            </div>
            {/* <div style={{ flex: 1, minHeight: 100 }}>
                        <div style={{ marginBottom: 10 }}>app权限:</div>
                        <Tree
                            checkable
                            defaultExpandAll={false}
                            checkedKeys={appCheckedKeys}
                            onCheck={appCheck}
                        >
                            {loop(appTree || [])}
                        </Tree>
                    </div> */}
          </>
        ) : (
          <Tree
            checkable
            defaultExpandAll={false}
            checkedKeys={daauthCheckkedKeys}
            onCheck={daauthCheck}
          >
            {loop(daauthTree || [])}
          </Tree>
        )}
      </div>
      <Button
        loading={loading}
        type="primary"
        style={{ width: '100%', marginTop: 15 }}
        onClick={saveData}
      >
        提交
      </Button>
    </div>
  );
};

export default Permissiontree;