RelateRoleModal.jsx 7.28 KB
Newer Older
1
import React, { useState, useCallback, useEffect } from 'react';
2
import { Modal, Spin, Tabs, notification, message, Checkbox, Divider } from 'antd';
3
import { SetUserRelationList, setUserRelation, setUserRelations } from '@/services/userManage/api';
4
import ListCardItem from './components/listCardItem';
5
const CheckboxGroup = Checkbox.Group;
6 7 8 9 10 11 12 13 14 15 16 17 18

const RelateRoleModal = props => {
  const {
    currentUser,
    currentSelectOrg,
    userIDs,
    visible,
    rolelist,
    stationlist,
    multiRelateRoles,
    onCancel,
    onSelect,
    loading,
19 20
    multiRoleList,
    multistationList,
21
    mult,
22 23 24 25
  } = props;
  const { TabPane } = Tabs;
  const [roleValueList, setRoleValueList] = useState({}); // 勾选的角色列表
  const [stationValueList, setStationValueList] = useState({}); // 勾选的站点列表
26
  const authority = localStorage.getItem('panda-oms-authority');
27
  const [activeKey, setActiveKey] = useState('1');
28 29

  const getRoleValueCallback = useCallback((value, index) => {
30
    console.log(value);
31
    roleValueList[index] = value;
32
    console.log(roleValueList);
33
    setRoleValueList({ ...roleValueList });
34
    console.log({ ...roleValueList });
35 36 37
  }, []);

  const getStationValueCallback = useCallback((value, index) => {
38
    console.log(value);
39
    stationValueList[index] = value;
40
    console.log(stationValueList);
41 42 43
    setStationValueList({ ...stationValueList });
  }, []);

44
  useEffect(() => {
45 46 47
    if (!visible) {
      setActiveKey('1');
    }
48
    console.log(currentUser);
49 50 51
    console.log(multiRoleList);
    console.log(multistationList);
  }, [visible]);
52

53 54
  // 提交-关联角色
  const submitRole = () => {
55
    console.log(1212121212);
56
    SetUserRelationList(
57
      currentUser.userId,
58 59 60 61 62 63 64 65
      Object.keys(roleValueList)
        .map(k => roleValueList[k])
        .flat(),
      Object.keys(stationValueList)
        .map(k => stationValueList[k])
        .flat(),
    )
      .then(res => {
66
        if (res.code == 0) {
67 68
          onCancel();
          // 跳转到新组织机构下的用户表
69 70 71
          if (currentSelectOrg !== '-1') {
            onSelect([currentSelectOrg]);
          }
72 73 74 75 76 77 78
          notification.success({
            message: '提交成功',
            duration: 2,
          });
        } else {
          notification.error({
            message: '提交失败',
79
            description: res.msg,
80 81 82 83 84 85 86 87 88
          });
        }
      })
      .catch(err => {
        message.error(err);
      });
  };
  // 提交-批量关联角色
  const submitRoles = () => {
89
    console.log(64634676437);
90 91 92 93 94 95 96 97 98 99 100 101 102 103
    setUserRelations(
      userIDs,
      Object.keys(roleValueList)
        .map(k => roleValueList[k])
        .flat()
        .toString(),
      Object.keys(stationValueList)
        .map(k => stationValueList[k])
        .flat()
        .toString(),
    )
      .then(res => {
        if (res.code === 0) {
          onCancel();
104 105
          // 跳转到组织重新请求该机构下用户数据,查找用户时currentSelectOrg为'-1',不需要重新请求
          if (currentSelectOrg !== '-1') {
106
            onSelect([currentSelectOrg]);
107
          }
108 109 110 111 112
          notification.success({
            message: '提交成功',
            duration: 2,
          });
        } else {
113 114 115 116 117
          console.log(1);
          notification.error({
            message: '提交失败',
            description: res.msg,
          });
118 119 120 121 122 123
        }
      })
      .catch(err => {
        message.error(err);
      });
  };
124
  const onChangeList = () => {};
125 126 127 128 129 130
  const title = (
    <span>
      <span style={{ marginRight: '20px' }}>批量关联角色</span>
      <span style={{ color: 'red' }}>提示:批量关联角色会覆盖原有角色</span>
    </span>
  );
131 132
  const title1 = (
    <span>
133
      <span>
134
        关联权限
135 136 137
        <span style={{ fontWeight: 'bold', color: 'rgb(24, 144, 255)' }}>
{currentUser.userName}
        </span>
138
      </span>
139
    </span>
140
  );
141 142 143 144
  const onChange = e => {
    console.log(e);
    setActiveKey(e);
  };
145
  if (mult == 'Yes') {
146 147
    return (
      <Modal
148
        title={title}
149
        visible={visible}
150
        onOk={submitRoles}
151 152
        maskClosable={false}
        destroyOnClose
153 154 155 156 157 158
        onCancel={onCancel}
        okText="确认"
        cancelText="取消"
        width="500px"
      >
        <Spin spinning={loading} tip="loading">
159
          <Tabs activeKey={activeKey} onChange={onChange} style={{ marginTop: '-16px' }}>
160
            <TabPane tab="角色" key="1">
161
              <div style={{ height: '500px', overflowY: 'scroll' }}>
162 163 164 165 166 167 168 169 170 171 172 173 174 175
                {visible &&
                  rolelist.map((role, index) => (
                    <ListCardItem
                      style={{ display: 'none' }}
                      itemid={index}
                      key={`item${index}key`}
                      userList={role.roleList}
                      multiRoleList={multiRoleList}
                      mult={mult}
                      OUName={role.visibleTitle}
                      getValueCallback={getRoleValueCallback}
                    />
                  ))}
              </div>
176 177
            </TabPane>
            <TabPane tab="站点" key="2">
178
              <div style={{ height: '500px', overflowY: 'scroll' }}>
179 180 181 182 183 184 185 186 187 188 189 190 191
                {visible &&
                  stationlist.map((station, index) => (
                    <ListCardItem
                      itemid={index}
                      key={`item${index}key`}
                      userList={station.stationList}
                      multistationList={multistationList}
                      mult={mult}
                      OUName={station.visibleTitle}
                      getValueCallback={getStationValueCallback}
                    />
                  ))}
              </div>
192 193 194 195 196
            </TabPane>
          </Tabs>
        </Spin>
      </Modal>
    );
197 198 199 200 201 202 203
  }
  return (
    <Modal
      title={multiRelateRoles ? title : title1}
      visible={visible}
      onOk={multiRelateRoles ? submitRoles : submitRole}
      onCancel={onCancel}
204
      maskClosable={false}
205 206 207 208 209
      okText="确认"
      cancelText="取消"
      width="500px"
    >
      <Spin spinning={loading} tip="loading">
210
        <Tabs activeKey={activeKey} onChange={onChange} style={{ marginTop: '-16px' }}>
211
          <TabPane tab="角色" key="1">
212 213 214 215 216 217 218 219 220 221 222 223 224
            <div style={{ height: '500px', overflowY: 'scroll' }}>
              {visible &&
                rolelist.map((role, index) => (
                  <ListCardItem
                    itemid={index}
                    key={`item${index}key`}
                    userList={role.roleList}
                    mult={mult}
                    OUName={role.visibleTitle}
                    getValueCallback={getRoleValueCallback}
                  />
                ))}
            </div>
225 226
          </TabPane>
          <TabPane tab="站点" key="2">
227 228 229 230 231 232 233 234 235 236 237 238 239
            <div style={{ height: '500px', overflowY: 'scroll' }}>
              {visible &&
                stationlist.map((station, index) => (
                  <ListCardItem
                    itemid={index}
                    key={`item${index}key`}
                    userList={station.stationList}
                    mult={mult}
                    OUName={station.visibleTitle}
                    getValueCallback={getStationValueCallback}
                  />
                ))}
            </div>
240 241 242 243 244
          </TabPane>
        </Tabs>
      </Spin>
    </Modal>
  );
245 246 247
};

export default RelateRoleModal;