HomeConfigModal.jsx 6.92 KB
Newer Older
邓超's avatar
邓超 committed
1 2 3 4 5 6 7 8 9 10
import React, { useState, useEffect } from 'react';
import { Modal, Form, Select, Input, Checkbox } from 'antd';
import lodash from 'lodash';
import styles from './HomeConfigModal.less';
import { GetRoleGroups } from '@/services/webConfig/api';
const { Option } = Select;
const HomeConfigModal = props => {
  const { handleCancel, productList, visible, onFinish, currentPageConfig, client } = props;
  const [form] = Form.useForm();

邓超's avatar
邓超 committed
11
  const [checkList, setCheckList] = useState(null);
邓超's avatar
邓超 committed
12 13 14 15 16
  useEffect(() => {
    if (visible) {
      form.setFieldsValue(currentPageConfig);
      getCheckList();
    } else {
邓超's avatar
邓超 committed
17
      setCheckList(null);
邓超's avatar
邓超 committed
18 19 20 21 22 23 24 25 26
      form.resetFields();
    }
  }, [visible]);

  // 保存配置
  const onSubmit = () => {
    let roleName = [];
    let roleId = [];
    const getArr = arr => {
27
      arr.roleGroups.forEach(item => {
邓超's avatar
邓超 committed
28
        if (item.roleGroups) {
29
          getArr(item);
邓超's avatar
邓超 committed
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
        } else if (item.isCheck) {
          roleName.push(item.roleName);
          roleId.push(item.roleId);
        }
      });
    };

    getArr(checkList);
    console.log(checkList, { ...form.getFieldsValue(), roleName, roleId });
    onFinish({
      ...form.getFieldsValue(),
      roleName: roleName.join(','),
      roleId,
      keyIndex: currentPageConfig.keyIndex,
    });
    handleCancel();
  };
  const getCheckList = () => {
    console.log(client, 'client');
    if (!client) {
      return;
    }
    GetRoleGroups({ client }).then(res => {
      if (res.code === 0) {
        let childList = [];
        let defaultList = [];
        const chekMap = new Set(currentPageConfig.roleId);
        res.data.forEach(item => {
          if (item.groupName !== '默认') {
            let checks = [];
            item.roleGroups.forEach(ele => {
              if (chekMap.has(ele.roleId)) {
                checks.push(ele);
              }
            });
            if (checks.length === item.roleGroups.length) {
              item.indeterminate = false;
              item.isCheck = true;
            } else if (checks.length === 0) {
              item.indeterminate = false;
              item.isCheck = false;
            } else {
              item.indeterminate = true;
              item.isCheck = false;
            }
邓超's avatar
邓超 committed
75
            childList.push(item);
邓超's avatar
邓超 committed
76 77 78 79 80 81 82
          } else {
            defaultList = item.roleGroups;
          }
        });

        console.log(chekMap, currentPageConfig, 'chekMap');

邓超's avatar
邓超 committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
        let list = {
          groupName: '关联角色',
          isCheck: false,
          roleGroups: [...defaultList, ...childList].map((item, index) => {
            let obj = {
              groupName: item.groupName,
              isCheck: item.isCheck,
              indeterminate: item.indeterminate,
              index: [index],
              roleGroups: item.roleGroups?.map((ele, i) => ({
                ...ele,
                isCheck: chekMap.has(ele.roleId),
                index: [index, i],
              })),
            };
            let ojbChild = { ...item, isCheck: chekMap.has(item.roleId), index: [index] };
邓超's avatar
邓超 committed
99

邓超's avatar
邓超 committed
100 101 102
            return item.roleGroups ? obj : ojbChild;
          }),
        };
邓超's avatar
邓超 committed
103 104 105 106 107 108 109
        console.log(list, 'list');
        setCheckList(list);
      }
    });
  };
  // 多选
  const changeAll = (e, item) => {
邓超's avatar
邓超 committed
110
    console.log(item, 'itemasjdf');
邓超's avatar
邓超 committed
111
    let list = lodash.cloneDeep(checkList);
邓超's avatar
邓超 committed
112 113 114
    let current = item.index
      ? item.index.reduce((acc, cur) => (acc.roleGroups ? acc.roleGroups[cur] : acc), list)
      : list;
邓超's avatar
邓超 committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    console.log(current);
    current.isCheck = e.target.checked;
    checkAll(e.target.checked, current.roleGroups);
    current.indeterminate = false;
    setCheckList(list);
  };
  const checkAll = (checked, list) => {
    list.forEach(item => {
      if (item.roleGroups) {
        checkAll(checked, item.roleGroups);
      }
      item.isCheck = checked;
    });
  };
  const onChange = (e, item) => {
    let list = lodash.cloneDeep(checkList);
邓超's avatar
邓超 committed
131
    console.log(item.index, list, 'asfd');
邓超's avatar
邓超 committed
132
    let current = item.index.reduce(
邓超's avatar
邓超 committed
133
      (acc, cur) => (acc.roleGroups ? acc.roleGroups[cur] : acc),
邓超's avatar
邓超 committed
134 135
      list,
    );
邓超's avatar
邓超 committed
136
    console.log(current, 'current');
邓超's avatar
邓超 committed
137 138 139 140 141
    current.isCheck = e.target.checked;
    let parentIndex = [...item.index];
    parentIndex.pop();

    let allCurrent = parentIndex.reduce(
邓超's avatar
邓超 committed
142
      (acc, cur) => (acc.roleGroups ? acc.roleGroups[cur] : acc),
邓超's avatar
邓超 committed
143 144
      list,
    );
邓超's avatar
邓超 committed
145
    console.log(allCurrent, 'allCurrent');
邓超's avatar
邓超 committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159
    let num = allCurrent?.roleGroups?.filter(ele => ele.isCheck).length;

    if (num === allCurrent.roleGroups.length) {
      allCurrent.indeterminate = false;
      allCurrent.isCheck = true;
    } else if (num === 0) {
      allCurrent.indeterminate = false;
      allCurrent.isCheck = false;
    } else {
      allCurrent.indeterminate = true;
    }

    setCheckList(list);
  };
邓超's avatar
邓超 committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
  const checkRender = list => {
    console.log(list, 'fasldfjaslkdjf');
    let num = list?.roleGroups?.filter(ele => ele.isCheck || ele.indeterminate).length;
    return (
      <>
        <div className={styles.checkList}>
          {list?.groupName && (
            <div className={styles.title}>
              <Checkbox
                onChange={e => changeAll(e, list)}
                checked={num === list?.roleGroups.length}
                indeterminate={num > 0 && num < list?.roleGroups.length}
              >
                {list?.groupName}
              </Checkbox>
            </div>
          )}
          {list?.roleGroups.map(item => {
            return (
              <>
                {item.roleGroups ? (
                  checkRender(item)
                ) : (
邓超's avatar
邓超 committed
183
                  <Checkbox
邓超's avatar
邓超 committed
184 185 186
                    onChange={e => onChange(e, item)}
                    style={{ width: '150px', marginLeft: '0px', marginBottom: '10px' }}
                    checked={item.isCheck}
邓超's avatar
邓超 committed
187
                  >
邓超's avatar
邓超 committed
188
                    {item.roleName}
邓超's avatar
邓超 committed
189
                  </Checkbox>
邓超's avatar
邓超 committed
190 191 192 193 194 195 196 197
                )}
              </>
            );
          })}
        </div>
      </>
    );
  };
邓超's avatar
邓超 committed
198 199 200 201 202 203 204 205 206 207
  return (
    <Modal
      width={800}
      title="主页配置"
      visible={visible}
      onCancel={handleCancel}
      maskClosable={false}
      onOk={onSubmit}
      destroyOnClose
    >
邓超's avatar
邓超 committed
208
      <div className={styles.content}>
邓超's avatar
邓超 committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
        <Form
          form={form}
          labelCol={{ span: 3 }}
          wrapperCol={{ span: 21 }}
          initialValues={{ remember: true }}
        >
          <Form.Item label="产品类型" name="productType">
            <Select placeholder="请选择主页产品类型">
              {productList.map(item => (
                <Option value={item.PackageName} key={item.PackageName}>
                  {`${item.ProductName}(${item.PackageName})`}
                </Option>
              ))}
            </Select>
          </Form.Item>
          <Form.Item label="主页Url" name="homePage">
            <Input placeholder="请输入主页路径" autoComplete="off" />
          </Form.Item>
        </Form>
        <div className={styles.roleCheck}>{checkRender(checkList)}</div>
      </div>
    </Modal>
  );
};

export default HomeConfigModal;