siteManage.jsx 26.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
import React, { useEffect, useState } from 'react'
import {
    // Tree,
    Table,
    Space,
    message,
    Modal,
    Input,
    notification,
    Tooltip,
    Card,
    Button,
    Spin,
    Dropdown,
    Menu,
    Row,
    Col,
    Empty,
    Pagination,
    Checkbox
} from 'antd';

import classnames from 'classnames'
import {
    UserOutlined,
26
    PlusSquareOutlined,
27 28
    UsergroupAddOutlined,
    EditOutlined,
29
    FormOutlined,
30 31 32 33
    DeleteOutlined,
    DoubleLeftOutlined,
    DoubleRightOutlined,
    DownOutlined,
34
    PlusOutlined
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 79 80 81 82 83 84 85
} from '@ant-design/icons';

import PageContainer from '@/components/BasePageContainer';
import Tree from '@/components/ExpendableTree';
import voca from 'voca';
import zhCN from 'antd/es/locale/zh_CN';
import qs from 'qs';
import styles from './siteManage.less';

import {
    getWebModuleTree,
    chooseUserToStation,
    getAllGroup,
    getUserByStation,
    getStationUserList,
    groupUserPagingList,
    addChildSiteNode,
    getSiteTree,
    getStationUsers
} from '@/services/userCenter/siteManage/api';
import lodash, { clone } from 'lodash';
import AddModal from '../siteManage/AddModal';
import DelModal from '../siteManage/DelModal';
import EditModal from '../siteManage/EditModal';
import AddChildModal from '../siteManage/AddChildModal';
const { Search } = Input;
const placeholder = '请输入机构名称';
const SiteManageV2 = () => {




    const [treeVisible, setTreeVisible] = useState(true); // 树是否可见
    const [treeData, setTreeData] = useState([]); // 用户站点树
    const [treeDataCopy, setTreeDataCopy] = useState([]); // 机构树数据备份,用于更改机构
    const [treeState, setTreeState] = useState(true); // 树第一次加载
    const [treeLoading, setTreeLoading] = useState(false);
    const [currentStation, setCurrentStation] = useState(''); // 当前选中站点
    const [currentStationOperate, setCurrentStationOperate] = useState(false)
    const [flag, setFlag] = useState(1);//操作标致触发界面刷新
    const [dataList, setdataList] = useState([]);//当前站点对应的分页用户列表
    const [visibleParams, setvisibleParams] = useState({
        modalVisible: false, // 新增弹窗
        delVisible: false, // 删除弹窗
        editVisible: false, // 修改弹窗
        spinLoading: false, // 加载弹窗
        btnLoading: false,
        loading: false,
        checkBoxLoading: false,
    });
    const [total, setTotal] = useState(0); // 分页总数
86
    const [page, setPage] = useState({ pageNum: 1, pageSize: 10 });
87 88 89 90 91 92 93 94 95
    const [selectList, setSelectList] = useState([]); // 选择列表数据
    const [updatePageUser, setUpdatePageUser] = useState(1);//
    const [updateCheck, setUpdateCheck] = useState(1);
    const [name, setName] = useState('');

    // 渲染机构树
    const mapTree = org => {
        const haveChildren = Array.isArray(org.children) && org.children.length > 0;
        return {
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
            title: (
                <div className={styles.title}>
                    <div>{org.text}</div>
                    <div className={styles.tip}>
                        <Tooltip title="添加下级站点" className={styles.fs}>
                            <PlusSquareOutlined style={{ fontSize: '16px', color: '#1890FF' }} onClick={e => addSite(e, org)} />
                        </Tooltip>
                        <Tooltip title="编辑当前站点" className={styles.fs}>
                            <FormOutlined style={{ fontSize: '16px', color: '#1890FF' }} onClick={e => editorSite(e, org)} />
                        </Tooltip>
                        <Tooltip title="删除当前站点" className={styles.fs}>
                            <DeleteOutlined style={{ fontSize: '16px', color: '#1890FF' }} onClick={e => delSite(e, org)} />
                        </Tooltip>
                    </div>
                </div>
            ),
112 113 114 115 116 117
            key: org.id,
            // icon: <UserOutlined style={{ display: 'inline' }} />,
            // 判断它是否存在子集,若果存在就进行再次进行遍历操作,知道不存在子集便对其他的元素进行操作
            children: haveChildren ? org.children.map(i => mapTree(i)) : [],
        };
    };
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    //添加下级站点
    const addSite = (e, recode) => {
        e.stopPropagation();
        setCurrentStation(recode.id);
        handleShowModal('addChildVisible', true);
    }
    //删除当前站点
    const delSite = (e, recode) => {
        e.stopPropagation();
        setCurrentStation(recode.id);
        handleShowModal('delVisible', true);

    }
    //编辑当前站点
    const editorSite = (e, recode) => {
        e.stopPropagation();
        setCurrentStation(recode.id);
        handleShowModal('editVisible', true);
    }
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    // 重新渲染树
    const updateTrees = () => {
        setTreeLoading(true);
        getSiteTree({ selectNode: -1 }).then(
            res => {
                if (res.data.length > 0) {
                    setTreeLoading(false);
                    setTreeData(res.data);
                    setTreeDataCopy(res.data);
                    // 第一次加载,默认选择第一个组织
                    if (treeState) {
                        onSelect([res.data[0].id], false);//待会儿要改
                        setTreeState(false);
                    }
                } else {
                    setTreeLoading(false);
                    notification.error({
                        message: '获取失败',
                        description: res.message,
                    });
                }
            }
        )

    };

    // 获取用户机构树
    useEffect(() => {
        updateTrees();
    }, [flag]);

    //切换站点,点击分页按钮,提交 
    useEffect(() => {
        if (!currentStation) return;
        getList();
    }, [updatePageUser, name]);
    //切换站点,提交时触发已勾选列表更新
    useEffect(() => {
        if (!currentStation) return;
176 177
        //getAllcheckList();
        getAllCheckListNew()
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
    }, [currentStation, updateCheck]);

    //获取当前站点可编辑用户(已勾选和未勾选)分页展示
    const getList = () => {
        let params = {
            id: +currentStation || '',
            PageIndex: +page.pageNum,
            PageSize: +page.pageSize,
        };
        if (name) params = { ...params, name };
        groupUserPagingList(params).then(res => {
            if (res.code === 0 && res.data) {
                let { list } = res.data;
                // 还原选择的数据
                if (selectList.length > 0) {
                    selectList.forEach(item => {
                        list.forEach((value, index) => {
                            if (item.GroupId === value.GroupId) {
                                list[index].Users.forEach((user, userIndex) => {
                                    if (user.userID === item.userID) {
                                        list[index].Users[userIndex].isChecked = true;
                                    }
                                });
                                let checkedLen = list[index].Users.filter(v => v.isChecked)
                                    .length;
                                if (checkedLen === list[index].Users.length) {
                                    list[index].isChecked = true;
                                }
                            }
                        });
                    });
                }
                handleShowModal('loading', false);
                setdataList(lodash.cloneDeep(list));
                setTotal(res.data.TotalCount);
            } else {
                handleShowModal('loading', false);
                setdataList(lodash.cloneDeep([]));
            }
        });
    }
    //获取当前站点所有已经勾选的用户
    const getAllcheckList = async () => {
        let res = await getUserByStation({
            stationID: currentStation,
            _version: 9999,
            _dc: new Date().getTime(),
        });
        if (res.length > 0) {
            // 还原从后台返回的数据 选中的列表
            let list = [];
            // console.log(res);
            res.forEach(item => {
                if (item.userList.length > 0) {
                    item.userList.forEach(value => {
                        // console.log(value);
                        if (
                            value.isChecked &&
                            list.findIndex(
                                v => +v.GroupId === +item.OUID && +v.userID === +value.userID,
                            ) === -1
                        ) {
                            list.push({
                                GroupId: +item.OUID,
                                GroupName: item.OUName,
                                userName: value.userName,
                                userID: value.userID,
                            });
                        }
                    });
                }
            });
            setSelectList(lodash.cloneDeep(list));
            setUpdatePageUser(updatePageUser + 1)
        }
    }
254 255 256 257

    //获取当前站点所有已经勾选的用户新接口
    const getAllCheckListNew = () => {
        getStationUsers({
258
            stationId: currentStation
259 260 261 262
        }).then(
            res => {
                let list = []
                if (res.data.length > 0) {
263

264 265 266 267 268 269 270 271
                    res.data.map((item, index) => {
                        list.push({
                            GroupId: +item.OUID,
                            GroupName: item.OUName,
                            userName: item.userName,
                            userID: item.userID,
                        });
                    })
272

273 274 275 276 277 278
                }
                setSelectList(lodash.cloneDeep(list));
                setUpdatePageUser(updatePageUser + 1)
            }
        )
    }
279 280
    //选中某个站点
    const onSelect = (props, e) => {
281
        console.log('props[0]', props[0]);
282 283 284 285 286 287
        if (!props[0]) {

            setCurrentStation(currentStation)
        } else {
            setCurrentStation(props[0]);
        }
288
        setPage({ pageNum: 1, pageSize: 10 });
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311

    }
    // 弹出模态框
    const handleShowModal = (key, value) => {
        setvisibleParams({ ...visibleParams, [key]: value });
    };
    // 获取搜索框的值
    const handleSearch = value => {
        setName(value);
        getList(value);
    };
    const confirmModal = e => {
        handleShowModal('modalVisible', false);
        setFlag(flag + 1);
    };
    const delModal = () => {
        handleShowModal('delVisible', false);
        setFlag(flag + 1);
    };
    const editModal = () => {
        handleShowModal('editVisible', false);
        setFlag(flag + 1);
    };
312
    const addChildModal = () => {
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 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 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
        handleShowModal('addChildVisible', false);
        setFlag(flag + 1);
    }

    const handleChangeCollpase = (groupId, isShow) => {
        let index = dataList.findIndex(item => item.GroupId === groupId);
        if (dataList[index].children && dataList[index].children.length > 0) {
            setdataList(lodash.cloneDeep(dataList));
            return;
        }
        handleShowModal('loading', true);
        getStationUserList({ stationID: currentStation, groupId }).then(
            res => {
                if (res.code === 0 && res.data) {
                    handleShowModal('loading', false);
                    dataList[index].children = res.data;
                    setdataList(lodash.cloneDeep(dataList));
                }
            },
        );
    };
    // 每组全选全不选
    const handleChangeAll = (e, index) => {
        dataList[index].isChecked = e.target.checked;
        dataList[index].Users.forEach(item => {
            item.isChecked = e.target.checked;
            let delIndex = selectList.findIndex(
                v => v.GroupId === dataList[index].GroupId && v.userID === item.userID,
            );
            if (e.target.checked) {
                if (delIndex === -1) {
                    selectList.push({
                        GroupId: dataList[index].GroupId,
                        GroupName: dataList[index].GroupName,
                        userName: item.userName,
                        userID: item.userID,
                    });
                }
            }
            if (!e.target.checked) {
                selectList.splice(delIndex, 1);
            }
        });

        setSelectList(lodash.cloneDeep(selectList));
        setdataList(lodash.cloneDeep(dataList));
    };
    // 单个选择checkbox
    const handleChangeSignel = (e, index, vIndex) => {
        dataList[index].Users[vIndex].isChecked = e.target.checked;
        let checked = isAllChecked(index);
        let hasIndex = selectList.findIndex(
            item =>
                item.userID === dataList[index].Users[vIndex].userID &&
                item.GroupId === dataList[index].GroupId,
        );
        dataList[index].isChecked = checked;
        if (e.target.checked && hasIndex === -1) {
            selectList.push({
                GroupId: dataList[index].GroupId,
                GroupName: dataList[index].GroupName,
                userName: dataList[index].Users[vIndex].userName,
                userID: dataList[index].Users[vIndex].userID,
            });
        } else {
            selectList.splice(hasIndex, 1);
        }
        setdataList(lodash.cloneDeep(dataList));
    };
    const isAllChecked = index =>
        dataList[index].Users.filter(item => item.isChecked).length ===
        dataList[index].Users.length;
    // 删除已选列表
    const handleDel = index => {
        let { GroupId, userID } = selectList[index];
        let outerIndex = dataList.findIndex(item => item.GroupId === GroupId);
        if (outerIndex > -1) {
            let innerIndex = dataList[outerIndex].Users.findIndex(
                item => item.userID === userID,
            );
            dataList[outerIndex].Users[innerIndex].isChecked = false;
            dataList[outerIndex].isChecked = isAllChecked(outerIndex);
        }
        selectList.splice(index, 1);
        setSelectList(lodash.cloneDeep(selectList));
        setdataList(lodash.cloneDeep(dataList));
    };
    // 提交
    const handleCommitBtn = () => {
        handleShowModal('btnLoading', true);
        let result = [];
        let obj = {};
        selectList.forEach(item => {
            if (obj[item.GroupId]) {
                obj[item.GroupId].push(item.userID);
            } else {
                obj[item.GroupId] = [item.userID];
            }
        });
412 413 414 415 416
        // dataList.forEach(item => {
        //     if (obj[item.GroupId] && item.Users.length === obj[item.GroupId].length) {
        //         obj[item.GroupId].push(item.GroupId);
        //     }
        // });
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
        result = Object.values(obj);
        // 数据处理成后台需要的格式
        if (result.length === 0)
            return notification.warning({
                message: '提示',
                description: '请至少选择选择一个用户!',
            });
        chooseUserToStation(
            qs.stringify({
                userList: String(result.flat()),
                stationID: currentStation,
            }),
            {
                headers: {
                    'content-type': 'application/x-www-form-urlencggoded;charset=UTF-8',
                },
            },
        )
            .then(res => {
                handleShowModal('btnLoading', false);

皮倩雯's avatar
皮倩雯 committed
438
                if (res.code===0) {
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
                    setSelectList([]);
                    setUpdateCheck(updateCheck + 1);
                    notification.success({
                        message: '提示',
                        duration: 3,
                        description: '设置成功',
                    });
                } else {
                    notification.error({
                        message: '提示',
                        duration: 15,
                        description: res.message,
                    });
                }
            })
            .catch(err => {
                handleShowModal('btnLoading', false);
                console.log(err);
            });
    };
    // 分页
    const handleChangePage = (pageNum, pageSize) => {
        setPage({ pageNum, pageSize });
        setUpdatePageUser(updatePageUser + 1);
    };
    /** ***操作按钮**** */
    // 机构操作
    const addTopStation = () => {
        handleShowModal('modalVisible', true);
    }
    return (
        <PageContainer className={styles.siteManageContainer}>
            <div className={styles.contentContainer}>
                <Card
                    className={classnames({
                        [styles.orgContainer]: true,
                        [styles.orgContainerHide]: !treeVisible,
                    })}
                >
478
                    <span style={{ margin: '0 180px  0 10px' }}>站点列表</span>
479
                    <Tooltip title="添加顶级站点">
480
                        <PlusOutlined
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
                            onClick={() => addTopStation()}
                            style={{
                                color: '#1890FF',
                                fontSize: '18px',
                                verticalAlign: '0.04em',
                            }}
                        />
                    </Tooltip>
                    {treeData.length > 0 && (
                        <Tree
                            showIcon="true"
                            showLine={{ showLeafIcon: false }}
                            blockNode
                            autoExpandParent
                            selectedKeys={[currentStation]}
                            onSelect={onSelect}
497
                            height={treeData.length && treeData.length > 30 ? treeData.length * 14 : 780}
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
                            treeData={treeData.map(t => mapTree(t))}
                        />
                    )}
                    <div className={styles.switcher}>
                        {treeVisible && (
                            <Tooltip title="隐藏机构列表">
                                <DoubleLeftOutlined onClick={() => setTreeVisible(false)} />
                            </Tooltip>
                        )}
                        {!treeVisible && (
                            <Tooltip title="显示机构列表">
                                <DoubleRightOutlined onClick={() => setTreeVisible(true)} />
                            </Tooltip>
                        )}
                    </div>
                </Card>
                {/* 右侧用户表 */}
                <div
                    className={classnames({
                        [styles.userContainer]: true,
                        [styles.userContainerHide]: !treeVisible,
                    })}
                >
                    <AddModal
                        visible={visibleParams.modalVisible}
                        onCancel={() => handleShowModal('modalVisible', false)}
                        confirmModal={confirmModal}
                    />
                    <AddChildModal
                        visible={visibleParams.addChildVisible}
                        pid={currentStation}
                        onCancel={() => handleShowModal('addChildVisible', false)}
                        confirmModal={addChildModal}
                    />
                    <DelModal
                        visible={visibleParams.delVisible}
                        stationId={currentStation}
                        onCancel={() => handleShowModal('delVisible', false)}
                        confirmModal={delModal}
                    />
                    <EditModal
                        visible={visibleParams.editVisible}
                        stationObj={currentStation}
                        onCancel={() => handleShowModal('editVisible', false)}
                        confirmModal={editModal}
                    />
                    <div
                        className={classnames({
                            [styles.boxR]: true,
                            [styles.boxH]: treeVisible,
                        })}
                    >
                        <Card
                            className={classnames({
                                [styles.cardBoxTop]: true,
                                [styles.boxH]: treeVisible,
                            })}
                        >
                            <Row align="middle">
                                <Col span={1}>搜索</Col>
                                <Col span={8}>
                                    <Search
                                        allowClear
                                        placeholder={placeholder}
                                        onSearch={handleSearch}
                                        // onChange={handleChange}
                                        enterButton
                                    />
                                </Col>
                                <Col span={3} />
568
                                
569 570 571 572 573 574 575 576 577 578
                            </Row>
                        </Card>
                        <div style={{ background: '#fff' }}>
                            <Card
                                className={classnames({
                                    [styles.boxH]: treeVisible,
                                    [styles.cardBoxR]: true,
                                })}
                            >
                                {/* <Checkbox className={styles.siteAll}>全选/反选</Checkbox> */}
579

580 581
                                {dataList.length > 0 && !visibleParams.loading ? (
                                    <>
582

583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
                                        <p className={styles.siteline}>已选择列表:</p>
                                        <div className={styles.siteSelectList}>
                                            <ul className={styles.siteSelectUl}>
                                                {selectList.map((item, index) => (
                                                    <li
                                                        key={`${item.userName}${item.GroupId}${index}`}
                                                        onClick={() => handleDel(index)}
                                                    >
                                                        {`${item.userName}(${item.GroupName})`}
                                                    </li>
                                                ))}
                                            </ul>
                                        </div>
                                    </>
                                ) : (
                                    <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} />
                                )}
600
                                <Spin spinning={visibleParams.loading} >
601 602 603 604 605 606 607 608 609 610 611
                                    {dataList.map((item, index) => (
                                        <Panels
                                            {...item}
                                            index={index}
                                            key={item.GroupId}
                                            handleChangeCollpase={handleChangeCollpase}
                                            handleChangeAll={handleChangeAll}
                                            handleChangeSignel={handleChangeSignel}
                                        />
                                    ))}
                                </Spin>
612
                            </Card>
613 614 615 616 617 618
                            {dataList.length > 0 && !visibleParams.loading ? (
                                <div style={{ textAlign: 'right' }}>
                                    <Pagination
                                        size="small"
                                        total={total}
                                        current={page.pageNum}
619
                                        defaultPageSize="10"
620
                                        onChange={handleChangePage}
621
                                        pageSizeOptions={['10']}
622 623 624
                                    />
                                </div>) : ''
                            }
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
                            <div className={styles.siteBtn}>
                                <Button
                                    type="primary"
                                    className={styles.siteCommit}
                                    onClick={handleCommitBtn}
                                >
                                    提交
                                </Button>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </PageContainer>
    )
}


const Panels = React.memo(props => {
    let { index, GroupId, GroupName, Users, isChecked, isShow, color } = props;
    return (
        <div className={styles.sitePanel} key={GroupId} id={`siteId${GroupId}`}>
648

649 650 651 652 653 654 655 656 657 658 659 660
            {/* onClick={() => props.handleChangeCollpase(GroupId, isShow)} */}
            <div className={styles.sitePanelHead}>
                {/* {isShow ? (
            <UpOutlined className={styles.siteIcon} />
          ) : (
            <DownOutlined className={styles.siteIcon} />
          )} */}
                {/* <UpOutlined className={styles.siteIcon} /> */}
                <UserOutlined className={styles.siteIcon} />
                <p style={{ color }}>{GroupName}</p>
                <Checkbox
                    key="0"
661
                    className={styles.siteListTitle}
662 663 664 665
                    checked={isChecked}
                    onClick={e => props.handleChangeAll(e, index)}
                >
                    全选
666
                </Checkbox>
667 668 669
            </div>
            <div className={styles.sitePanelCon}>

670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
                {Users.length > 0 &&
                    Users.map((v, vIndex) => (
                        <CheckBoxRow
                            {...v}
                            index={index}
                            vIndex={vIndex}
                            key={v.userID}
                            handleChangeSignel={props.handleChangeSignel}
                        />
                    ))}
            </div>
        </div>
    );
});

const CheckBoxRow = React.memo(props => {
    let { vIndex, index, isChecked, userName } = props;
    return (
        <Checkbox
            className={styles.siteList}
            checked={isChecked}
            onClick={e => props.handleChangeSignel(e, index, vIndex)}
        >
            {userName}
        </Checkbox>
    );
});
export default SiteManageV2;