RoleModal.jsx 5.56 KB
Newer Older
1
import React, { useEffect, useState } from 'react';
2
import { Modal, Checkbox, Spin } from 'antd';
3
import { RoleGroupList } from '@/services/messagemanage/messagemanage';
4
import { GetUserRelationListNew } from '@/services/userManage/api';
5 6 7 8 9 10
import styles from './RoleModal.less';
const CheckboxGroup = Checkbox.Group;

const RoleModal = porps => {
  const { visible, rolCallBack, onCancel, selectValue } = porps;
  const [roleList, setRoleList] = useState([]); // 角色列表
11
  const [isLoading, setIsLoading] = useState(false);
12 13 14

  useEffect(() => {
    if (visible) {
15
      setIsLoading(true);
16
      let hasCheckList = selectValue ? selectValue.split(',') : [];
17
      GetUserRelationListNew({ userID: 9999999 }).then(res => {
18
        setIsLoading(false);
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
        if (res.code === 0) {
          let roleListData = res.data.roleList;
          // 给子角色分类放到外面来
          roleListData.forEach((item, index) => {
            if (item.child.length > 0) {
              item.child.forEach((val, i) => {
                val.visibleTitle = `${item.visibleTitle}(${val.visibleTitle})`;
                roleListData.splice(index + 1 + i, 0, val);
              });
            }
          });
          console.log(roleListData, 'roleListData');
          let list = roleListData.map(item => {
            let checkedList = []; // 已选
            let options = []; // 数据源
            let indeterminate = false; // 有选中但没全选样式
            let checkAll = false; // 全选样式
            let arr = {};
            let name = {};
            let last = {};
            options = item.roleList.map(val => {
              hasCheckList.forEach(ele => {
                if (val.roleID === ele) {
                  checkedList.push(val.roleName);
                }
              });
              return val.roleName;
            });

            name = item.roleList.map(val => {
              hasCheckList.forEach(ele => {
                if (val.roleID === ele) {
                  last[val.roleID] = val.roleName;
                }
              });
              arr[val.roleID] = val.roleName;
              return arr;
            });
            if (checkedList.length === options.length && checkedList.length > 0) {
              checkAll = true;
            }
            if (checkedList.length < options.length && checkedList.length > 0) {
              indeterminate = true;
            }
            return {
              options,
              checkedList,
              checkAll,
              name,
              last,
              arr,
              indeterminate,
              visibleTitle: item.visibleTitle,
            };
          });
          console.log(list, 'list');
          setRoleList(list);
        }
      });
      console.log(selectValue, 'selectValue');
79 80
    } else {
      setRoleList([]);
81 82 83 84 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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    }
  }, [visible]);
  // 多选change事件
  const onCheckAllChange = (e, index) => {
    let newCheckList = JSON.parse(JSON.stringify(roleList));
    let aa = {};
    if (e.target.checked) {
      newCheckList[index].options.map(j => {
        Object.keys(newCheckList[index].arr).forEach(i => {
          if (newCheckList[index].arr[i] == j) {
            aa[i] = j;
          }
        });
      });
      newCheckList[index].last = aa;
    } else {
      newCheckList[index].last = {};
    }
    newCheckList[index].checkedList = e.target.checked ? newCheckList[index].options : [];
    newCheckList[index].indeterminate = false;
    newCheckList[index].checkAll = e.target.checked;
    setRoleList(newCheckList);
  };
  // 单选change事件
  const onChange = (list, index) => {
    let newCheckList = JSON.parse(JSON.stringify(roleList));
    let aa = {};
    list.map(j => {
      Object.keys(newCheckList[index].arr).forEach(i => {
        if (newCheckList[index].arr[i] == j) {
          aa[i] = j;
        }
      });
    });
    newCheckList[index].last = aa;
    newCheckList[index].checkedList = list;
    newCheckList[index].indeterminate =
      !!list.length && list.length < newCheckList[index].options.length;
    newCheckList[index].checkAll = list.length === newCheckList[index].options.length;
    setRoleList(newCheckList);
  };
  return (
    <div>
      <Modal
        title="关联角色"
        visible={visible}
        onOk={() => rolCallBack(roleList)}
        width="900px"
        onCancel={onCancel}
        maskClosable={false}
        destroyOnClose
        centered
      >
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
        <Spin spinning={isLoading}>
          <div className={styles.checkContainer}>
            {roleList.map((item, index) => (
              <div className={styles.checkContent} key={item.visibleTitle}>
                <div className={styles.topCheckbox}>
                  <Checkbox
                    indeterminate={item.indeterminate}
                    onChange={e => onCheckAllChange(e, index)}
                    checked={item.checkAll}
                  >
                    {item.visibleTitle}
                  </Checkbox>
                </div>
                <div className={styles.bottomCheckbox}>
                  <CheckboxGroup
                    value={item.checkedList}
                    onChange={list => onChange(list, index)}
                    style={{ display: 'flex', flexWrap: 'wrap' }}
                  >
                    {item.options.map(val => (
                      <Checkbox key={val} value={val}>
                        {val}
                      </Checkbox>
                    ))}
                  </CheckboxGroup>
                </div>
160
              </div>
161 162 163
            ))}
          </div>
        </Spin>
164 165 166 167 168 169
      </Modal>
    </div>
  );
};

export default RoleModal;