OpePermissions.jsx 11.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
import React, { useState, useEffect, useCallback, useRef, useMemo } from 'react';
import {
  Modal,
  Input,
  Button,
  message,
  Spin,
  Pagination,
  Table,
  Tooltip,
  Space,
  Checkbox,
  notification,
14 15
  Radio,
  Popconfirm,
16
} from 'antd';
17 18 19 20 21 22
import {
  getRolePermission,
  updateRolePermission,
  getRolePermissionTable,
  updateRolePermissionTable,
} from '@/services/RoleManage/api';
23 24 25 26 27 28
import styles from './SelectUser.less';

const CheckboxGroup = Checkbox.Group;

const OpePermissions = props => {
  const { confirmModal, onCancel, visible, itemObj, roleID } = props;
29
  const [checkAll, setCheckAll] = useState({});
30 31 32 33
  const [loading, setLoading] = useState(false);
  const [option, setOption] = useState([]);
  const [groupList, setGroupList] = useState([]);
  const [keepGroupList, setKeepGroupList] = useState([]);
34
  const [value4, setValue4] = useState('BI');
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

  useEffect(() => {
    getInitialData();
  }, [itemObj]);

  // 获取初始数据
  const getInitialData = () => {
    setLoading(true);
    getRolePermission({ roleId: roleID })
      .then(res => {
        setLoading(false);
        if (res.code === 0) {
          setOption(res.data.dicPermissions);
          setGroupList(res.data.projectPermission);
          setKeepGroupList(res.data.projectPermission);
50
          getAllData(res.data.projectPermission);
51 52 53
        } else {
          setOption([]);
          setGroupList([]);
54
          getAllData({});
55 56 57 58 59 60 61
        }
      })
      .catch(err => {
        setLoading(false);
      });
  };

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  const getTableData = () => {
    setLoading(true);
    getRolePermissionTable({ roleId: roleID })
      .then(res => {
        if (res.code === 0) {
          setLoading(false);
          setOption(res.data.dicPermissions);
          setGroupList(res.data.projectPermission);
          setKeepGroupList(res.data.projectPermission);
          getAllData(res.data.projectPermission);
        } else {
          setOption([]);
          setGroupList([]);
          getAllData({});
        }
      })
      .catch(err => {
        setLoading(false);
      });
  };

83 84
  const getAllData = val => {
    let obj = { 预览: [], 编辑: [], 发布: [], 删除: [], 导出: [] };
85
    val?.map(i => {
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
      if (i.permission.indexOf('预览') !== -1) {
        obj.预览 = [...obj.预览, i.projectName];
      }
      if (i.permission.indexOf('编辑') !== -1) {
        obj.编辑 = [...obj.编辑, i.projectName];
      }
      if (i.permission.indexOf('发布') !== -1) {
        obj.发布 = [...obj.发布, i.projectName];
      }
      if (i.permission.indexOf('删除') !== -1) {
        obj.删除 = [...obj.删除, i.projectName];
      }
      if (i.permission.indexOf('导出') !== -1) {
        obj.导出 = [...obj.导出, i.projectName];
      }
    });
    if (
      obj.预览.length === val.length &&
      obj.编辑.length === val.length &&
      obj.发布.length === val.length &&
      obj.删除.length === val.length &&
      obj.导出.length === val.length
    ) {
      obj.全选 = true;
    }
    setCheckAll(obj);
  };

114 115 116 117 118 119 120
  const onChange = (list, id) => {
    let newData = JSON.parse(JSON.stringify(groupList));
    let item = newData.find(i => i.projeectId === id);
    let index = newData.findIndex(i => i.projeectId === id);
    item.permission = list;
    newData[index] = item;
    setGroupList(newData);
121
    getAllData(newData);
122
  };
123

124 125 126 127 128 129 130
  const onCheckAllChange = (e, id) => {
    let newData = JSON.parse(JSON.stringify(groupList));
    let item = newData.find(i => i.projeectId === id);
    let index = newData.findIndex(i => i.projeectId === id);
    item.permission = e.target.checked ? option : [];
    newData[index] = item;
    setGroupList(newData);
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 167 168 169 170 171 172 173 174
    getAllData(newData);
  };

  const onCheckAll = (e, val) => {
    let newData = JSON.parse(JSON.stringify(groupList));
    newData.map(i => {
      if (e.target.checked) {
        if (val === '预览') {
          if (i.permission.indexOf('预览') === -1) {
            i.permission.push('预览');
          }
        } else if (val === '编辑') {
          if (i.permission.indexOf('编辑') === -1) {
            i.permission.push('编辑');
          }
        } else if (val === '发布') {
          if (i.permission.indexOf('发布') === -1) {
            i.permission.push('发布');
          }
        } else if (val === '删除') {
          if (i.permission.indexOf('删除') === -1) {
            i.permission.push('删除');
          }
        } else if (val === '导出') {
          if (i.permission.indexOf('导出') === -1) {
            i.permission.push('导出');
          }
        }
      } else {
        if (val === '预览') {
          i.permission = i.permission.filter(item => item !== '预览');
        } else if (val === '编辑') {
          i.permission = i.permission.filter(item => item !== '编辑');
        } else if (val === '发布') {
          i.permission = i.permission.filter(item => item !== '发布');
        } else if (val === '删除') {
          i.permission = i.permission.filter(item => item !== '删除');
        } else if (val === '导出') {
          i.permission = i.permission.filter(item => item !== '导出');
        }
      }
    });
    setGroupList(newData);
    getAllData(newData);
175
  };
176 177 178 179 180 181 182 183 184 185

  const onAll = e => {
    let newData = JSON.parse(JSON.stringify(groupList));
    newData.map(i => {
      i.permission = e.target.checked ? option : [];
    });
    setGroupList(newData);
    getAllData(newData);
  };

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
  // 提交勾选人员
  const onFinish = () => {
    let arrList = [];
    groupList.forEach(item => {
      let data = keepGroupList.find(i => i.projeectId === item.projeectId);
      if (item.permission.length !== data.permission.length) {
        arrList.push(item);
      }
    });
    console.log(arrList);
    let arr = [];
    if (arrList.length > 0) {
      arrList.forEach(item => {
        arr.push({
          projectId: item.projeectId,
          permission: item.permission.toString(),
        });
      });
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
      if (value4 === 'BI') {
        updateRolePermission({ roleId: roleID, permissons: arr }).then(res => {
          if (res.code === 0) {
            notification.success({
              message: '提示',
              duration: 3,
              description: '设置成功',
            });
            getInitialData();
          } else {
            notification.error({
              message: '提示',
              duration: 3,
              description: res.msg,
            });
          }
        });
      } else {
        updateRolePermissionTable({ roleId: roleID, permissons: arr }).then(res => {
          if (res.code === 0) {
            notification.success({
              message: '提示',
              duration: 3,
              description: '设置成功',
            });
            getTableData();
          } else {
            notification.error({
              message: '提示',
              duration: 3,
              description: res.msg,
            });
          }
        });
      }
239 240 241 242 243
    } else {
      message.warning('请更改后再保存!');
    }
  };

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
  const optionsWithDisabled = [
    {
      label: 'BI操作权限',
      value: 'BI',
    },
    {
      label: '报表操作权限',
      value: 'Table',
    },
  ];

  const onChange4 = ({ target: { value } }) => {
    setGroupList([]);
    setKeepGroupList([]);
    setCheckAll({});
    if (value === 'BI') {
      getInitialData();
    } else {
      getTableData();
    }
    setValue4(value);
  };

267 268 269
  return (
    <>
      {/* 头部搜索框 */}
270 271
      <div className={styles.header}>
        <div>
272 273 274 275 276 277 278 279
          <Radio.Group
            options={optionsWithDisabled}
            onChange={onChange4}
            value={value4}
            optionType="button"
            buttonStyle="solid"
            style={{ marginRight: '20px' }}
          />
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
          <Checkbox
            style={{ marginRight: '20px' }}
            checked={checkAll?.全选}
            onChange={e => onAll(e)}
          >
            全选
          </Checkbox>
          <Checkbox
            checked={checkAll?.预览?.length === groupList?.length}
            onChange={e => onCheckAll(e, '预览')}
          >
            预览全选
          </Checkbox>
          <Checkbox
            checked={checkAll?.编辑?.length === groupList?.length}
            onChange={e => onCheckAll(e, '编辑')}
          >
            编辑全选
          </Checkbox>
          <Checkbox
            checked={checkAll?.发布?.length === groupList?.length}
            onChange={e => onCheckAll(e, '发布')}
          >
            发布全选
          </Checkbox>
          <Checkbox
            checked={checkAll?.删除?.length === groupList?.length}
            onChange={e => onCheckAll(e, '删除')}
          >
            删除全选
          </Checkbox>
          <Checkbox
            checked={checkAll?.导出?.length === groupList?.length}
            onChange={e => onCheckAll(e, '导出')}
          >
            导出全选
          </Checkbox>
        </div>
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
        <Popconfirm
          placement="bottomRight"
          title={
            <p>
              确定修改
              <span style={{ color: '#1685FF' }}>{value4 === 'BI' ? 'BI' : '报表'}</span>
              操作权限吗?
            </p>
          }
          okText="确认"
          cancelText="取消"
          onConfirm={() => {
            onFinish();
          }}
        >
          <Button type="primary" htmlType="submit">
            提交
          </Button>
        </Popconfirm>
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
      </div>
      <div className={styles.pushTestContent}>
        <div className={styles.leftContent}>
          {/* 复选框模块 */}
          <div className={styles.checkScrollBox}>
            <Spin spinning={loading}>
              <div className={styles.checkContainer}>
                {groupList?.map((item, index) => (
                  <div className={styles.checkBoxContent} key={item.projeectId}>
                    <div className={styles.checkContent}>
                      <div className={styles.topCheckbox}>
                        <Checkbox
                          indeterminate={
                            item.permission.length > 0 && item.permission.length < option?.length
                          }
                          onChange={e => onCheckAllChange(e, item.projeectId)}
                          checked={item.permission.length === option?.length}
                        >
                          {`${item.projeectId}-${item.projectName}`}
                        </Checkbox>
                      </div>
                      <div className={styles.bottomCheckbox}>
                        <CheckboxGroup
                          value={item.permission}
                          onChange={list => onChange(list, item.projeectId)}
                          style={{ display: 'flex', flexWrap: 'wrap' }}
                        >
                          {option.map(i => (
                            <Checkbox key={i} value={i}>
                              <span className={styles.fontlabel}>{i}</span>
                            </Checkbox>
                          ))}
                        </CheckboxGroup>
                      </div>
                    </div>
                  </div>
                ))}
              </div>
            </Spin>
          </div>
        </div>
      </div>
    </>
  );
};

export default OpePermissions;