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

邓超's avatar
邓超 committed
20
  const [checkList, setCheckList] = useState(null);
邓超's avatar
邓超 committed
21 22 23 24 25
  useEffect(() => {
    if (visible) {
      form.setFieldsValue(currentPageConfig);
      getCheckList();
    } else {
邓超's avatar
邓超 committed
26
      setCheckList(null);
邓超's avatar
邓超 committed
27 28 29 30 31 32
      form.resetFields();
    }
  }, [visible]);

  // 保存配置
  const onSubmit = () => {
邓超's avatar
邓超 committed
33 34 35 36 37
    console.log(form.getFieldsValue().homePage);
    if (!form.getFieldsValue().homePage) {
      message.error('请填写主页url');
      return;
    }
38 39 40 41
    if (!checkList || checkList.length === 0) {
      message.error('当前主页没有挂接角色');
      return;
    }
邓超's avatar
邓超 committed
42 43 44
    let obj = { homePage: form.getFieldsValue().homePage, productType: '' };

    let arrUrl = obj.homePage.split('/'); // 用const声明常量
45 46 47
    let allProList = JSON.parse(JSON.stringify(allProductList));
    allProList.push({ PackageName: 'civ_base' });
    const product = allProList.find(item => item.PackageName.includes(arrUrl[0]));
邓超's avatar
邓超 committed
48 49 50 51 52
    if (!product) {
      // arrUrl.shift();
      obj.homePage = `civweb4/${obj.homePage}`;
    }
    obj.productType = product?.PackageName || 'civweb4';
53 54 55
    let proList = JSON.parse(JSON.stringify(productList));
    proList.push({ PackageName: 'civ_base' });
    if (!proList.some(item => item.PackageName === obj.productType)) {
邓超's avatar
邓超 committed
56 57 58
      message.error(`${obj.productType}未授权,不能使用该功能当主页`);
      return;
    }
邓超's avatar
邓超 committed
59 60 61
    let roleName = [];
    let roleId = [];
    const getArr = arr => {
62
      arr.roleGroups.forEach(item => {
邓超's avatar
邓超 committed
63
        if (item.roleGroups) {
64
          getArr(item);
邓超's avatar
邓超 committed
65 66 67 68 69 70 71 72
        } else if (item.isCheck) {
          roleName.push(item.roleName);
          roleId.push(item.roleId);
        }
      });
    };

    getArr(checkList);
邓超's avatar
邓超 committed
73
    console.log(checkList, { ...obj, roleName, roleId }, 'aaaaaa');
邓超's avatar
邓超 committed
74
    onFinish({
邓超's avatar
邓超 committed
75
      ...obj,
邓超's avatar
邓超 committed
76 77 78 79 80 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
      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
110
            childList.push(item);
邓超's avatar
邓超 committed
111 112 113 114 115 116 117
          } else {
            defaultList = item.roleGroups;
          }
        });

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

邓超's avatar
邓超 committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
        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
134

邓超's avatar
邓超 committed
135 136 137
            return item.roleGroups ? obj : ojbChild;
          }),
        };
邓超's avatar
邓超 committed
138 139 140 141 142 143 144
        console.log(list, 'list');
        setCheckList(list);
      }
    });
  };
  // 多选
  const changeAll = (e, item) => {
邓超's avatar
邓超 committed
145
    console.log(item, 'itemasjdf');
邓超's avatar
邓超 committed
146
    let list = lodash.cloneDeep(checkList);
邓超's avatar
邓超 committed
147 148 149
    let current = item.index
      ? item.index.reduce((acc, cur) => (acc.roleGroups ? acc.roleGroups[cur] : acc), list)
      : list;
邓超's avatar
邓超 committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    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
166
    console.log(item.index, list, 'asfd');
邓超's avatar
邓超 committed
167
    let current = item.index.reduce(
邓超's avatar
邓超 committed
168
      (acc, cur) => (acc.roleGroups ? acc.roleGroups[cur] : acc),
邓超's avatar
邓超 committed
169 170
      list,
    );
邓超's avatar
邓超 committed
171
    console.log(current, 'current');
邓超's avatar
邓超 committed
172 173 174 175 176
    current.isCheck = e.target.checked;
    let parentIndex = [...item.index];
    parentIndex.pop();

    let allCurrent = parentIndex.reduce(
邓超's avatar
邓超 committed
177
      (acc, cur) => (acc.roleGroups ? acc.roleGroups[cur] : acc),
邓超's avatar
邓超 committed
178 179
      list,
    );
邓超's avatar
邓超 committed
180
    console.log(allCurrent, 'allCurrent');
邓超's avatar
邓超 committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194
    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
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
  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
218
                  <Checkbox
邓超's avatar
邓超 committed
219 220 221
                    onChange={e => onChange(e, item)}
                    style={{ width: '150px', marginLeft: '0px', marginBottom: '10px' }}
                    checked={item.isCheck}
邓超's avatar
邓超 committed
222
                  >
邓超's avatar
邓超 committed
223
                    {item.roleName}
邓超's avatar
邓超 committed
224
                  </Checkbox>
邓超's avatar
邓超 committed
225 226 227 228 229 230 231 232
                )}
              </>
            );
          })}
        </div>
      </>
    );
  };
邓超's avatar
邓超 committed
233 234 235 236 237 238 239 240 241 242
  return (
    <Modal
      width={800}
      title="主页配置"
      visible={visible}
      onCancel={handleCancel}
      maskClosable={false}
      onOk={onSubmit}
      destroyOnClose
    >
邓超's avatar
邓超 committed
243
      <div className={styles.content}>
邓超's avatar
邓超 committed
244 245 246 247 248 249
        <Form
          form={form}
          labelCol={{ span: 3 }}
          wrapperCol={{ span: 21 }}
          initialValues={{ remember: true }}
        >
邓超's avatar
邓超 committed
250
          {/* <Form.Item label="产品类型" name="productType">
邓超's avatar
邓超 committed
251 252 253 254 255 256 257
            <Select placeholder="请选择主页产品类型">
              {productList.map(item => (
                <Option value={item.PackageName} key={item.PackageName}>
                  {`${item.ProductName}(${item.PackageName})`}
                </Option>
              ))}
            </Select>
邓超's avatar
邓超 committed
258
          </Form.Item> */}
邓超's avatar
邓超 committed
259
          <Form.Item label="主页Url" name="homePage">
邓超's avatar
邓超 committed
260 261
            {/* <Input placeholder="请输入主页路径" autoComplete="off" /> */}
            <TreeSelect menuChange={val => {}} />
邓超's avatar
邓超 committed
262 263 264 265 266 267 268 269 270
          </Form.Item>
        </Form>
        <div className={styles.roleCheck}>{checkRender(checkList)}</div>
      </div>
    </Modal>
  );
};

export default HomeConfigModal;