HomeConfigModal.jsx 8.11 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';
6
import TreeSelect from '../menuconfig/NewTreeSelect';
邓超's avatar
邓超 committed
7 8
const { Option } = Select;
const HomeConfigModal = props => {
邓超's avatar
邓超 committed
9 10 11 12 13 14 15
  const {
    handleCancel,
    productList,
    allProductList,
    visible,
    onFinish,
    currentPageConfig,
16
    userMode,
邓超's avatar
邓超 committed
17
    client,
18
    curWeb,
邓超's avatar
邓超 committed
19
  } = props;
邓超's avatar
邓超 committed
20 21
  const [form] = Form.useForm();

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

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

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

    getArr(checkList);
邓超's avatar
邓超 committed
78
    console.log(checkList, { ...obj, roleName, roleId }, 'aaaaaa');
邓超's avatar
邓超 committed
79
    onFinish({
邓超's avatar
邓超 committed
80
      ...obj,
邓超's avatar
邓超 committed
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
      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
115
            childList.push(item);
邓超's avatar
邓超 committed
116 117 118 119 120 121 122
          } else {
            defaultList = item.roleGroups;
          }
        });

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

邓超's avatar
邓超 committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
        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
139

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

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

export default HomeConfigModal;