SiteManage.jsx 10.9 KB
Newer Older
Maofei94's avatar
Maofei94 committed
1
import React, { useState, useEffect } from 'react';
Maofei94's avatar
Maofei94 committed
2 3 4 5 6 7 8 9 10
import {
  Row,
  Col,
  Card,
  Input,
  Tooltip,
  Button,
  notification,
  Spin,
张烨's avatar
张烨 committed
11 12
  List,
  Space,
Maofei94's avatar
Maofei94 committed
13
} from 'antd';
Maofei94's avatar
Maofei94 committed
14
import { DoubleLeftOutlined, DoubleRightOutlined } from '@ant-design/icons';
15
import PageContainer from '@/components/BasePageContainer';
Maofei94's avatar
Maofei94 committed
16 17 18
import {
  getWebModuleTree,
  chooseUserToStation,
Maofei94's avatar
Maofei94 committed
19
  getUserByStation,
Maofei94's avatar
Maofei94 committed
20
} from '@/services/userCenter/siteManage/api';
Maofei94's avatar
Maofei94 committed
21 22 23 24
import ListCard, {
  checkChildrenByCondition,
  getId,
} from '@/components/CheckGroup';
25
import styles from '@/pages/userCenter/siteManage/SiteManage.less';
Maofei94's avatar
Maofei94 committed
26
import qs from 'qs';
Maofei94's avatar
Maofei94 committed
27
import { appConnector } from '@/containers/App/store';
张烨's avatar
张烨 committed
28
import classnames from 'classnames';
Maofei94's avatar
Maofei94 committed
29 30 31
import AddModal from './AddModal';
import DelModal from './DelModal';
import EditModal from './EditModal';
陈前坚's avatar
陈前坚 committed
32
import userStyles from '@/pages/userCenter/userManage/UserManage.less';
33

34
const { Search } = Input;
Maofei94's avatar
Maofei94 committed
35
const placeholder = '请输入人员姓名';
Maofei94's avatar
Maofei94 committed
36 37
const SiteManage = props => {
  const { userMode } = props;
Maofei94's avatar
Maofei94 committed
38
  const [treeData, setTreeData] = useState([]); // 树结构数据
张烨's avatar
张烨 committed
39
  const [currentStation, setCurrentStation] = useState({}); // 当前选中站点
Maofei94's avatar
Maofei94 committed
40
  const [searchWord, setSearchWord] = useState(''); // 关键字
Maofei94's avatar
Maofei94 committed
41 42 43 44
  const [modalVisible, setModalVisible] = useState(false); // 新增弹窗
  const [flag, setFlag] = useState(1);
  const [delVisible, setDelVisible] = useState(false); // 删除弹窗
  const [editVisible, setEditVisible] = useState(false); // 修改弹窗
Maofei94's avatar
Maofei94 committed
45 46
  const [subList, setSubList] = useState([]); // 选中的数组
  const [spinLoading, setSpinLoading] = useState(false);
Maofei94's avatar
Maofei94 committed
47
  const [btnLoading, setBtnLoading] = useState(false);
Maofei94's avatar
Maofei94 committed
48 49 50
  const [valueList, setValueList] = useState([]);
  const [dataList, setdataList] = useState([]);
  const [loading, setLoading] = useState(true);
Maofei94's avatar
Maofei94 committed
51
  const [mulu, setMulu] = useState(true);
张烨's avatar
张烨 committed
52

Maofei94's avatar
Maofei94 committed
53
  useEffect(() => {
Maofei94's avatar
Maofei94 committed
54
    setSpinLoading(true);
55
    getWebModuleTree({
Maofei94's avatar
Maofei94 committed
56
      userMode: userMode || 'super',
Maofei94's avatar
Maofei94 committed
57 58
      select: '',
      _version: 9999,
Maofei94's avatar
Maofei94 committed
59
      _dc: Date.now(),
Maofei94's avatar
Maofei94 committed
60
      node: -2,
61
    })
Maofei94's avatar
Maofei94 committed
62
      .then(res => {
Maofei94's avatar
Maofei94 committed
63
        setSpinLoading(false);
Maofei94's avatar
Maofei94 committed
64 65 66 67
        let arr = [];
        if (res) {
          arr.push(res.find(item => item.id === 'Web4StationRoot'));
        }
张烨's avatar
张烨 committed
68 69 70 71 72
        if (arr[0]) {
          const stations = arr[0].children;
          setTreeData(stations);
          if (!currentStation.stationID) {
            setCurrentStation(stations[0]);
张烨's avatar
张烨 committed
73 74 75 76 77
          } else {
            setCurrentStation(
              stations.find(s => s.stationID === currentStation.stationID) ||
                stations[0],
            );
张烨's avatar
张烨 committed
78 79
          }
        }
Maofei94's avatar
Maofei94 committed
80 81
      })
      .catch(err => {
Maofei94's avatar
Maofei94 committed
82
        setSpinLoading(false);
Maofei94's avatar
Maofei94 committed
83 84 85
        console.error(err);
      });
  }, [flag]);
Maofei94's avatar
Maofei94 committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
  useEffect(() => {
    if (!currentStation.stationID) return;
    setLoading(true);
    const defaultConfig = {};
    getUserByStation({
      stationID: currentStation.stationID,
      _version: 9999,
      _dc: new Date().getTime(),
    })
      .then(res => {
        const list = [];
        // eslint-disable-next-line no-unused-expressions
        res &&
          res.forEach(item => {
            list.push({ ...defaultConfig, ...item });
          });
        const finalList = buildMap(list);
        setdataList(finalList);
        setValueList(
          finalList
            .map(l =>
              checkChildrenByCondition(
                l,
                it => (it.isChecked ? [getId(it)] : []),
张烨's avatar
张烨 committed
110
                true,
Maofei94's avatar
Maofei94 committed
111 112 113 114 115 116 117 118 119 120 121 122
                'map',
              ).flat(Infinity),
            )
            .flat(Infinity)
            .filter(Boolean),
        );
        setLoading(false);
      })
      .catch(err => {
        setLoading(false);
      });
  }, [currentStation]);
张烨's avatar
张烨 committed
123

Maofei94's avatar
Maofei94 committed
124 125 126 127 128 129 130 131 132 133 134
  const buildMap = list => {
    const obj = {
      type: 'widgetGroup',
      searchWord,
      children: '',
      text: '',
      itemid: '',
    };
    let arr = list.map(item => {
      item.id = item.OUID * 10000;
      item.text = item.OUName;
Maofei94's avatar
Maofei94 committed
135
      item.isChecked = false;
Maofei94's avatar
Maofei94 committed
136 137 138 139 140 141 142 143 144 145 146
      item.type = 'widgetGroup';
      item.children = item.userList.map(u => ({
        ...u,
        text: u.userName,
        type: 'widgetChild',
      }));
      return item;
    });

    console.log(arr);
    return arr.filter(item => item.userList.length > 0);
Maofei94's avatar
Maofei94 committed
147
  };
Maofei94's avatar
Maofei94 committed
148 149

  // 新增站点
Maofei94's avatar
Maofei94 committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
  const handleAdd = e => {
    console.log(e);
    setModalVisible(true);
  };
  // 站点删除
  const handleDel = e => {
    setDelVisible(true);
  };
  // 编辑站点
  const handleEdit = e => {
    setEditVisible(true);
  };
  // 获取搜索框的值
  const handleSearch = value => {
    setSearchWord(value);
  };
166 167 168 169
  const handleChange = e => {
    const { value } = e.target;
    setSearchWord(value);
  };
Maofei94's avatar
Maofei94 committed
170 171 172 173 174 175 176 177 178 179 180 181
  const confirmModal = e => {
    setModalVisible(false);
    setFlag(flag + 1);
  };
  const delModal = () => {
    setDelVisible(false);
    setFlag(flag + 1);
  };
  const editModal = () => {
    setEditVisible(false);
    setFlag(flag + 1);
  };
Maofei94's avatar
Maofei94 committed
182 183
  const valueCallback = valueObj => {
    setSubList(valueObj);
Maofei94's avatar
Maofei94 committed
184
  };
Maofei94's avatar
Maofei94 committed
185
  const handleCommit = results => {
Maofei94's avatar
Maofei94 committed
186
    setBtnLoading(true);
Maofei94's avatar
Maofei94 committed
187 188
    chooseUserToStation(
      qs.stringify({
Maofei94's avatar
Maofei94 committed
189 190
        // userList: String(arr.flat()),
        userList: String(results.flat()),
张烨's avatar
张烨 committed
191
        stationID: currentStation.stationID,
Maofei94's avatar
Maofei94 committed
192 193 194 195 196 197 198 199
      }),
      {
        headers: {
          'content-type': 'application/x-www-form-urlencggoded;charset=UTF-8',
        },
      },
    )
      .then(res => {
Maofei94's avatar
Maofei94 committed
200
        setBtnLoading(false);
Maofei94's avatar
Maofei94 committed
201 202 203 204 205 206 207 208 209
        if (res.success) {
          notification.success({
            message: '提示',
            duration: 3,
            description: '设置成功',
          });
        } else {
          notification.error({
            message: '提示',
Maofei94's avatar
Maofei94 committed
210
            duration: 15,
Maofei94's avatar
Maofei94 committed
211 212 213 214 215
            description: res.message,
          });
        }
      })
      .catch(err => {
Maofei94's avatar
Maofei94 committed
216
        setBtnLoading(false);
Maofei94's avatar
Maofei94 committed
217 218
        console.log(err);
      });
Maofei94's avatar
Maofei94 committed
219
  };
Maofei94's avatar
Maofei94 committed
220 221 222
  const handleHide = () => {
    setMulu(!mulu);
  };
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
  const renderListItem = items =>
    items.map(t => (
      <List.Item
        onClick={() => {
          setCurrentStation(t);
        }}
        key={t.id}
        className={classnames({
          [styles.listItem]: true,
          [styles.selected]: currentStation.id === t.id,
        })}
      >
        {t.text}
      </List.Item>
    ));

239 240
  return (
    <PageContainer>
Maofei94's avatar
Maofei94 committed
241 242 243 244 245 246 247 248 249 250 251 252 253
      {/* <Row>
        <Col span={mulu ? 4 : 0}> */}
      <div className={styles.content}>
        <Card
          className={classnames({
            [styles.cardBox]: true,
            [styles.hideBox]: !mulu,
          })}
        >
          <Spin
            tip="loading...."
            spinning={spinLoading}
            style={{ marginTop: '20px' }}
Maofei94's avatar
Maofei94 committed
254
          >
Maofei94's avatar
Maofei94 committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
            <div className={userStyles.siteTitle}>
              <span>选择站点:</span>
            </div>
            <List>{renderListItem(treeData)}</List>
          </Spin>
          <AddModal
            visible={modalVisible}
            onCancel={() => setModalVisible(false)}
            confirmModal={confirmModal}
          />
          <DelModal
            visible={delVisible}
            stationId={currentStation.stationID}
            onCancel={() => setDelVisible(false)}
            confirmModal={delModal}
          />
          <EditModal
            visible={editVisible}
            stationObj={currentStation}
            onCancel={() => setEditVisible(false)}
            confirmModal={editModal}
          />
          <div>
            {mulu && (
              <Tooltip title="隐藏站点" className={styles.hide}>
                <DoubleLeftOutlined onClick={() => handleHide()} />
              </Tooltip>
            )}
            {!mulu && (
              <Tooltip title="显示站点" className={styles.hide}>
                <DoubleRightOutlined onClick={() => handleHide()} />
              </Tooltip>
            )}
          </div>
        </Card>
        {/* <div>
Maofei94's avatar
Maofei94 committed
291 292 293 294 295
            {mulu && (
              <Tooltip title="隐藏站点" className={styles.hide}>
                <DoubleLeftOutlined onClick={() => handleHide()} />
              </Tooltip>
            )}
陈前坚's avatar
陈前坚 committed
296
            {!mulu && (
Maofei94's avatar
Maofei94 committed
297 298 299
              <Tooltip title="显示站点" className={styles.hide}>
                <DoubleRightOutlined onClick={() => handleHide()} />
              </Tooltip>
陈前坚's avatar
陈前坚 committed
300 301
            )}
          </div> */}
Maofei94's avatar
Maofei94 committed
302
        {/* </Col> */}
陈前坚's avatar
陈前坚 committed
303 304
        {/* <Col span={mulu ? 0 : 1}>
          {mulu && (
Maofei94's avatar
Maofei94 committed
305 306 307
            <Tooltip title="隐藏站点" className={styles.hide}>
              <DoubleLeftOutlined onClick={() => handleHide()} />
            </Tooltip>
陈前坚's avatar
陈前坚 committed
308
          )}
Maofei94's avatar
Maofei94 committed
309 310 311 312 313
          {!mulu && (
            <Tooltip title="显示站点" className={styles.hide}>
              <DoubleRightOutlined onClick={() => handleHide()} />
            </Tooltip>
          )}
陈前坚's avatar
陈前坚 committed
314
        </Col> */}
Maofei94's avatar
Maofei94 committed
315 316 317 318 319 320 321 322 323 324 325 326 327
        {/* <Col span={mulu ? 20 : 23}> */}
        <div
          className={classnames({
            [styles.boxR]: true,
            [styles.boxH]: mulu,
          })}
        >
          <Card
            className={classnames({
              [styles.cardBoxTop]: true,
              [styles.boxH]: mulu,
            })}
          >
Maofei94's avatar
Maofei94 committed
328 329 330 331 332 333 334 335 336
            <Row align="middle">
              <Col span={1}>搜索</Col>
              <Col span={8}>
                <Search
                  allowClear
                  placeholder={placeholder}
                  onSearch={handleSearch}
                  onChange={handleChange}
                  enterButton
Maofei94's avatar
Maofei94 committed
337
                />
Maofei94's avatar
Maofei94 committed
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
              </Col>
              <Col span={3} />
              <Col span={8}>
                <Space size="large">
                  <Button
                    type="primary"
                    onClick={() => {
                      handleAdd();
                    }}
                  >
                    新增
                  </Button>
                  <Button
                    type="primary"
                    onClick={() => {
                      handleEdit();
                    }}
                    disabled={!currentStation.stationID || spinLoading}
                  >
                    编辑
                  </Button>
                  <Button
                    danger
                    type="primary"
                    onClick={() => {
                      handleDel(currentStation.stationID);
                    }}
                    disabled={!currentStation.stationID || spinLoading}
                  >
                    删除
                  </Button>
                </Space>
              </Col>
            </Row>
          </Card>
Maofei94's avatar
Maofei94 committed
373 374 375 376 377 378
          <Card
            className={classnames({
              [styles.boxH]: mulu,
              [styles.cardBoxR]: true,
            })}
          >
Maofei94's avatar
Maofei94 committed
379 380 381 382 383 384 385 386 387 388 389
            {currentStation.stationID && (
              <ListCard
                loading={loading}
                checkList={valueList}
                dataList={dataList}
                searchWord={searchWord}
                onCommit={handleCommit}
                btnLoading={btnLoading}
              />
            )}
          </Card>
Maofei94's avatar
Maofei94 committed
390 391 392 393
        </div>
      </div>
      {/* </Col>
      </Row> */}
394
    </PageContainer>
Maofei94's avatar
Maofei94 committed
395 396
  );
};
397

Maofei94's avatar
Maofei94 committed
398
export default appConnector(SiteManage);