SelectUser.jsx 9.27 KB
Newer Older
1 2
import React, { useState, useEffect, useCallback, useRef } from 'react';
import { Modal, Input, Button, message, Spin, Pagination, Table, Tooltip, Space } from 'antd';
邓超's avatar
邓超 committed
3 4
import { GetGroupUserTree } from '@/services/messagemanage/messagemanage';
import { getStationUsers, chooseUserToStation } from '@/services/siteManage/api';
5
import { DeleteOutlined } from '@ant-design/icons';
6 7 8
import styles from './SelectUser.less';
import CardCheck from './CardCheck';

9 10
const SelectUser = props => {
  const { confirmModal, onCancel, visible, itemObj } = props;
11 12 13 14 15 16 17
  const [allList, setAllist] = useState([]); // 用于展示得数据
  const [checkList, setCheckList] = useState([]); // 选中得数据集合
  const [loading, setLoading] = useState(false);
  const [total, setTotal] = useState();
  const [currentPage, setCurrentPage] = useState(1);
  const [pageSize, setPageSize] = useState(10);
  const [searchName, setSearchName] = useState();
18 19
  const [deleKey, setDeleKey] = useState(); // 删除用户的key值
  const [delFlag, setDelFlag] = useState(0); // 删除标识每次删除后加一
皮倩雯's avatar
皮倩雯 committed
20

21 22 23 24 25 26 27 28 29 30 31 32
  useEffect(() => {
    if (visible) {
      setCurrentPage(1);
      getInitialData();
      // getData(searchName, 1, pageSize);
    } else {
      setCheckList([]);
      setAllist([]);
      setSearchName('');
    }
  }, [visible]);
  // 选中后得回调函数
33 34
  const checkCallBack = useCallback(newCheckList => {
    if (newCheckList) {
35 36 37
      setCheckList(newCheckList);
    }
  });
38

39 40 41 42 43 44 45 46
  // 监听分页
  const paginationChange = (page, pageSizes) => {
    setCurrentPage(page);
    setPageSize(pageSizes);
    getData(searchName, page, pageSizes);
  };
  // 获取初始数据
  const getInitialData = () => {
皮倩雯's avatar
皮倩雯 committed
47
    console.log(itemObj);
48 49 50 51 52 53
    let p1 = getStationUsers({ stationId: itemObj.roleID });
    let p2 = GetGroupUserTree({
      key: '',
      pageSize: 10,
      PageIndex: 1,
    });
54
    setLoading(true);
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    Promise.all([p1, p2]).then(res => {
      if (res[0].code === 0 && res[1].code === 0) {
        setTotal(res[1].data.count);
        let listCheck = res[0].data.map(item => ({
          label: item.userName,
          value: item.userID,
          groupName: item.OUName,
        }));

        setCheckList(listCheck);
        // 数据处理成checkbox组件需要得形式
        let list = res[1].data.data.map(item => {
          let indeterminate = false;
          let checkedList = [];
          let checkAll = false;
          let options = item.users.map(val => {
            listCheck.forEach(ele => {
              if (val.userId === ele.value) {
                checkedList.push(ele.value);
              }
            });
            return {
              label: val.userName,
              value: val.userId,
皮倩雯's avatar
皮倩雯 committed
79
              groupName: item.level,
80 81 82 83 84 85 86 87
            };
          });
          if (checkedList.length === options.length && checkedList.length > 0) {
            checkAll = true;
          }
          if (checkedList.length < options.length && checkedList.length > 0) {
            indeterminate = true;
          }
皮倩雯's avatar
皮倩雯 committed
88
          console.log(item.level);
89
          return {
皮倩雯's avatar
皮倩雯 committed
90
            groupName: item.level,
91 92 93 94 95 96 97
            groupId: item.groupId,
            indeterminate,
            checkAll,
            checkedList,
            plainOptions: options,
          };
        });
皮倩雯's avatar
皮倩雯 committed
98
        console.log(list);
99
        setAllist(list);
皮倩雯's avatar
皮倩雯 committed
100
        setLoading(false);
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 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
      } else if (res[0].code !== 0) {
        message.error(res[0].msg);
      } else {
        message.error(res[1].msg);
      }
    });
  };
  // 提交勾选人员
  const onFinish = () => {
    chooseUserToStation({
      userList: String(checkList.map(item => item.value)),
      stationID: itemObj.roleID,
    })
      .then(res => {
        if (res.code === 0) {
          confirmModal();
          message.success('关联成功');
        } else {
          message.error(res.msg);
        }
      })
      .catch(() => {
        message.error('网络异常,请稍后再试');
      });
  };
  // 搜索
  const onSearch = () => {
    setCurrentPage(1);
    getData(searchName, 1, pageSize);
  };
  // 重置
  const onReset = () => {
    setCurrentPage(1);
    getData('', 1, pageSize);
    setSearchName('');
  };
  // 搜索框监听
  const searchChange = e => {
    setSearchName(e.target.value);
  };
  // 获取数据
  const getData = (username, page, pageSizes) => {
    setLoading(true);
    GetGroupUserTree({
      key: username,
      pageSize: pageSizes,
      PageIndex: page,
    })
      .then(res => {
        setLoading(false);
        if (res.code === 0) {
          setTotal(res.data.count);
          // 数据处理成checkbox组件需要得形式
          let list = res.data.data.map(item => {
            let indeterminate = false;
            let checkedList = [];
            let checkAll = false;
            let options = item.users.map(val => {
              checkList.forEach(ele => {
                if (val.userId === ele.value) {
                  checkedList.push(ele.value);
                }
              });
              return {
                label: val.userName,
                value: val.userId,
皮倩雯's avatar
皮倩雯 committed
167
                groupName: item.level,
168 169 170 171 172 173 174 175
              };
            });
            if (checkedList.length === options.length && checkedList.length > 0) {
              checkAll = true;
            }
            if (checkedList.length < options.length && checkedList.length > 0) {
              indeterminate = true;
            }
皮倩雯's avatar
皮倩雯 committed
176
            console.log(item.level);
177
            return {
皮倩雯's avatar
皮倩雯 committed
178
              groupName: item.level,
179 180 181 182 183 184 185
              groupId: item.groupId,
              indeterminate,
              checkAll,
              checkedList,
              plainOptions: options,
            };
          });
皮倩雯's avatar
皮倩雯 committed
186
          console.log(list);
187 188 189 190 191 192 193 194 195 196
          setAllist(list);
        } else {
          message.error(res.msg);
        }
      })
      .catch(() => {
        setLoading(false);
        message.error('网络异常,请稍后再试');
      });
  };
197 198 199 200 201 202 203
  // 删除角色
  const deleteRol = key => {
    const dataSource = [...checkList];
    setCheckList(dataSource.filter(item => item.value !== key));
    setDeleKey(key);
    setDelFlag(delFlag + 1);
  };
204 205 206 207 208
  const columns = [
    {
      title: '已选用户',
      dataIndex: 'label',
      key: 'label',
209 210 211 212
      width: 220,
      ellipsis: {
        showTitle: true,
      },
213 214
      render: (text, record) => (
        <span>
皮倩雯's avatar
皮倩雯 committed
215 216
          <Tooltip placement="topLeft" title={`${record.label}(${record.level})`}>
            {record.label}({record.level})
217
          </Tooltip>
218 219 220
        </span>
      ),
    },
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
    {
      title: '操作',
      align: 'center',
      ellipsis: true,
      width: 80,
      render: record => (
        <>
          <Space>
            <Tooltip title="清除关联用户">
              <DeleteOutlined
                onClick={() => deleteRol(record.value)}
                style={{ fontSize: '16px', color: '#e86060' }}
              />
            </Tooltip>
          </Space>
        </>
      ),
    },
239
  ];
邓超's avatar
邓超 committed
240
  console.log(allList, 'allList');
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
  return (
    <>
      <Modal
        title="关联用户"
        visible={visible}
        onOk={onFinish}
        width="900px"
        onCancel={onCancel}
        maskClosable={false}
        destroyOnClose
        centered
      >
        <div className={styles.pushTestContent}>
          <div className={styles.leftContent}>
            {/* 头部搜索框 */}
            <div className={styles.searchHeader}>
              <Input.Search
                value={searchName}
                placeholder="请输入部门或用户"
                onChange={searchChange}
                onSearch={onSearch}
                enterButton
                style={{ width: '300px', marginRight: '15px' }}
              />
              <Button type="primary" htmlType="submit" onClick={onReset}>
                重置
              </Button>
            </div>
            {/* 复选框模块 */}
            <Spin spinning={loading}>
              <div className={styles.checkContainer}>
                {allList.map((item, index) => (
                  <div className={styles.checkBoxContent} key={item.groupId}>
                    <CardCheck
                      cardMsg={item}
                      cardIndex={index}
                      callback={(val, newCheckList) => checkCallBack(val, newCheckList)}
                      checkList={checkList}
279 280
                      deleKey={deleKey}
                      delFlag={delFlag}
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
                    />
                  </div>
                ))}
              </div>
            </Spin>
          </div>
          <div className={styles.tableRight}>
            <Table
              bordered
              style={{ width: '350px', overflowX: 'hidden' }}
              rowKey={record => record.value}
              columns={columns}
              dataSource={checkList}
              pagination={false}
              size="small"
              scroll={{ y: 530 }}
            />
          </div>
        </div>
300 301 302 303 304 305 306 307 308 309 310 311
        {/* 分页 */}
        <Pagination
          total={total}
          showTotal={(totals, range) => `第${range[0]}-${range[1]} 条/共 ${totals} 条`}
          defaultPageSize={pageSize}
          defaultCurrent={1}
          current={currentPage}
          onChange={paginationChange}
          style={{ marginBottom: '10px' }}
          size="small"
          showQuickJumper
        />
312 313 314 315 316
      </Modal>
    </>
  );
};

317
export default SelectUser;