UserModal.jsx 11.8 KB
Newer Older
1 2
import useModal from 'antd/lib/modal/useModal';
import React, { useEffect, useState } from 'react';
3
import SiteModal from '@/components/Modal/SiteModa';
4
import classnames from 'classnames';
5
import lodash, { clone } from 'lodash';
6
import { Card, Empty, Pagination, Checkbox, notification, Input, Row, Col } from 'antd';
7
import {
8 9 10 11 12 13 14 15 16
  getWebModuleTree,
  chooseUserToStation,
  getAllGroup,
  getUserByStation,
  getStationUserList,
  groupUserPagingList,
  addChildSiteNode,
  getSiteTree,
  getStationUsers,
邓超's avatar
邓超 committed
17
} from '@/services/siteManage/api';
18
import { UserOutlined } from '@ant-design/icons';
19
import qs from 'qs';
皮倩雯's avatar
皮倩雯 committed
20
import styles from './UserModal.less';
mayongxin's avatar
mayongxin committed
21 22
const { Search } = Input;
const placeholder = '请输入机构名称';
23
const UserModal = props => {
24 25 26 27 28 29 30 31 32 33
  const [dataList, setdataList] = useState([]);
  const [total, setTotal] = useState(0); // 分页总数
  const [page, setPage] = useState({ pageNum: 1, pageSize: 5 });
  const [selectList, setSelectList] = useState([]); // 选择列表数据
  const [updatePageUser, setUpdatePageUser] = useState(1); //
  const [updateCheck, setUpdateCheck] = useState(1);
  const [name, setName] = useState('');
  const { itemObj, confirmModal, visible } = props;
  const isAllChecked = index =>
    dataList[index].Users.filter(item => item.isChecked).length === dataList[index].Users.length;
34

35 36 37 38 39 40 41 42
  // 切换站点,点击分页按钮,提交
  useEffect(() => {
    if (!itemObj) return;
    getList();
  }, [updatePageUser, name]);
  useEffect(() => {
    if (!visible) {
      setName('');
43
    }
44 45 46
  }, [visible]);
  // 切换站点,提交时触发已勾选列表更新
  useEffect(() => {
皮倩雯's avatar
皮倩雯 committed
47
    // if (!itemObj) return;
48
    // getAllcheckList();
皮倩雯's avatar
皮倩雯 committed
49 50 51
    if (itemObj) {
      getAllCheckListNew();
    }
52
  }, [itemObj, updateCheck]);
53

54 55 56 57 58 59 60
  // 获取当前站点可编辑用户(已勾选和未勾选)分页展示
  const getList = () => {
    let params = {
      id: +itemObj.roleID || '',
      PageIndex: +page.pageNum,
      PageSize: +page.pageSize,
    };
皮倩雯's avatar
皮倩雯 committed
61
    console.log(123);
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    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;
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
        // handleShowModal('loading', false);
        setdataList(lodash.cloneDeep(list));
        setTotal(res.data.TotalCount);
      } else {
        // handleShowModal('loading', false);
        setTotal(0);
        setdataList(lodash.cloneDeep([]));
      }
    });
  };
  // 获取当前站点所有已经勾选的用户新接口
  const getAllCheckListNew = () => {
    getStationUsers({
      stationId: itemObj.roleID,
    }).then(res => {
      let list = [];
      if (res.data && res.data.length > 0) {
        console.log(res.data);
        res.data.map((item, index) => {
          list.push({
            GroupId: +item.OUID,
            GroupName: item.OUName,
            userName: item.userName,
            userID: item.userID,
          });
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
      }
      setSelectList(lodash.cloneDeep(list));
      console.log(lodash.cloneDeep(list));
      setUpdatePageUser(updatePageUser + 1);
    });
  };
  const onSubmit = () => {
    let result = [];
    let obj = {};
    selectList.forEach(item => {
      if (obj[item.GroupId]) {
        obj[item.GroupId].push(item.userID);
      } else {
        obj[item.GroupId] = [item.userID];
      }
    });
    // dataList.forEach(item => {
    //     if (obj[item.GroupId] && item.Users.length === obj[item.GroupId].length) {
    //         obj[item.GroupId].push(item.GroupId);
    //     }
    // });
    result = Object.values(obj);
    // 数据处理成后台需要的格式
    if (result.length === 0)
      return notification.warning({
        message: '提示',
        description: '请至少选择选择一个用户!',
      });
    chooseUserToStation(
      {
        userList: String(result.flat()),
        stationID: itemObj.roleID,
      },
      // {
      //     headers: {
      //         'content-type': 'application/x-www-form-urlencggoded;charset=UTF-8',
      //     },
      // },
    )
      .then(res => {
        if (res.code === 0) {
          setSelectList([]);
          setUpdateCheck(updateCheck + 1);
          confirmModal(itemObj.roleID);
          notification.success({
            message: '提示',
            duration: 3,
            description: '设置成功',
          });
159
        } else {
160 161 162 163 164
          notification.error({
            message: '提示',
            duration: 15,
            description: res.message,
          });
165
        }
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
      })
      .catch(err => {
        console.log(err);
      });
  };
  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: itemObj.roleID, groupId }).then(res => {
      if (res.code === 0 && res.data) {
        // handleShowModal('loading', false);
        dataList[index].children = res.data;
182
        setdataList(lodash.cloneDeep(dataList));
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
      }
    });
  };
  // 每组全选全不选
  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,
          });
202
        }
203 204 205 206 207
      }
      if (!e.target.checked) {
        selectList.splice(delIndex, 1);
      }
    });
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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
    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 handleChangePage = (pageNum, pageSize) => {
    setPage({ pageNum, pageSize });
    setUpdatePageUser(updatePageUser + 1);
  };
  // 删除已选列表
  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 handleSearch = value => {
    setPage({ pageNum: 1, pageSize: 5 });
    setName(value);
    // getList(value);
  };
  return (
    <SiteModal
      {...props}
      title="关联用户"
      bodyStyle={{ width: '100%', minHeight: '100px' }}
      style={{ top: 100 }}
      width="800px"
      destroyOnClose
      cancelText="取消"
      okText="确认"
      onOk={() => onSubmit()}
    >
      <div style={{ background: '#fff' }}>
        <Card
          className={classnames({
            [styles.cardBoxR]: true,
          })}
275
        >
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
          <Row align="middle">
            <Col span={8}>
              <Search
                allowClear
                placeholder={placeholder}
                onSearch={handleSearch}
                // onChange={handleChange}
                enterButton
              />
            </Col>
          </Row>
          {dataList.length > 0 ? (
            <>
              <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} />
          )}
          {dataList.map((item, index) => (
            <Panels2
              {...item}
              index={index}
              key={item.GroupId}
              handleChangeCollpase={handleChangeCollpase}
              handleChangeAll={handleChangeAll}
              handleChangeSignel={handleChangeSignel}
            />
          ))}
          <div style={{ textAlign: 'right' }}>
            <Pagination
              size="small"
              total={total}
              current={page.pageNum}
              defaultPageSize="5"
              onChange={handleChangePage}
              pageSizeOptions={['5']}
            />
          </div>
        </Card>
      </div>
    </SiteModal>
  );
};
331 332

const Panels2 = React.memo(props => {
333 334 335 336 337 338
  let { index, GroupId, GroupName, Users, isChecked, isShow, color } = props;
  return (
    <div className={styles.sitePanel} key={GroupId} id={`siteId${GroupId}`}>
      {/* onClick={() => props.handleChangeCollpase(GroupId, isShow)} */}
      <div className={styles.sitePanelHead}>
        {/* {isShow ? (
339 340 341 342
            <UpOutlined className={styles.siteIcon} />
          ) : (
            <DownOutlined className={styles.siteIcon} />
          )} */}
343 344 345
        {/* <UpOutlined className={styles.siteIcon} /> */}
        <UserOutlined className={styles.siteIcon} />
        <p style={{ color }}>{GroupName}</p>
346
        <Checkbox
347 348 349 350
          key="0"
          className={styles.siteListTitle}
          checked={isChecked}
          onClick={e => props.handleChangeAll(e, index)}
351
        >
352
          全选
353
        </Checkbox>
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
      </div>
      <div className={styles.sitePanelCon}>
        {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>
  );
382
});
383
export default UserModal;