RoleList.js 9.3 KB
Newer Older
sin's avatar
sin committed
1 2 3 4 5
/* eslint-disable */

import React, { PureComponent, Fragment } from 'react';
import { connect } from 'dva';
import moment from 'moment';
6
import { Card, Form, Input, Spin, Button, Modal, message, Table, Divider, Tree } from 'antd';
sin's avatar
sin committed
7 8
import PageHeaderWrapper from '@/components/PageHeaderWrapper';

sin's avatar
sin committed
9
import styles from './RoleList.less';
sin's avatar
sin committed
10 11

const FormItem = Form.Item;
sin's avatar
sin committed
12
const { TreeNode } = Tree;
13
const types = ['未知', '系统角色', '自定义角色'];
sin's avatar
sin committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

// 添加 form 表单
const CreateForm = Form.create()(props => {
  const { modalVisible, form, handleAdd, handleModalVisible, modalType, initValues } = props;

  const okHandle = () => {
    form.validateFields((err, fieldsValue) => {
      if (err) return;
      form.resetFields();
      handleAdd({
        fields: fieldsValue,
        modalType,
        initValues,
      });
    });
  };

  const title = modalType === 'add' ? '添加一个 Role' : '更新一个 Role';
  const okText = modalType === 'add' ? '添加' : '更新';
  return (
    <Modal
      destroyOnClose
      title={title}
      visible={modalVisible}
      onOk={okHandle}
      okText={okText}
      onCancel={() => handleModalVisible()}
    >
      <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="角色名">
        {form.getFieldDecorator('name', {
          rules: [{ required: true, message: '请输入角色名!', min: 2 }],
          initialValue: initValues.name,
        })(<Input placeholder="请输入" />)}
      </FormItem>
48 49 50 51 52 53
      <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="角色编码">
        {form.getFieldDecorator('code', {
          rules: [{ required: false }],
          initialValue: initValues.code,
        })(<Input placeholder="请输入" />)}
      </FormItem>
sin's avatar
sin committed
54 55 56 57
    </Modal>
  );
});

sin's avatar
sin committed
58
// 角色分配
sin's avatar
sin committed
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 86 87 88 89 90 91 92
const AssignModal = Form.create()(props => {
  const {
    modalVisible,
    form,
    handleOk,
    handleModalVisible,
    treeData,
    checkedKeys,
    loading,
    handleCheckBoxClick,
  } = props;

  const renderTreeNodes = data => {
    return data.map(item => {
      if (item.children) {
        return (
          <TreeNode title={item.title} key={item.key} dataRef={item}>
            {renderTreeNodes(item.children)}
          </TreeNode>
        );
      }
      return <TreeNode title={item.title} key={item.key} dataRef={item} />;
    });
  };

  const renderModalContent = treeData => {
    const RenderTreeNodes = renderTreeNodes(treeData);
    if (RenderTreeNodes) {
      return (
        <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="角色名">
          {form.getFieldDecorator('name', {})(
            <Tree
              defaultExpandAll={true}
              checkable={true}
sin's avatar
sin committed
93
              multiple={true}
sin's avatar
sin committed
94 95 96 97 98 99 100 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
              checkedKeys={checkedKeys}
              onCheck={handleCheckBoxClick}
            >
              {renderTreeNodes(treeData)}
            </Tree>
          )}
        </FormItem>
      );
    } else {
      return null;
    }
  };

  const okHandle = () => {
    form.validateFields((err, fieldsValue) => {
      if (err) return;
      form.resetFields();
      handleOk({
        fields: fieldsValue,
      });
    });
  };

  return (
    <Modal
      destroyOnClose
      title="更新权限"
      visible={modalVisible}
      onOk={okHandle}
      onCancel={() => handleModalVisible()}
    >
      <Spin spinning={loading}>{renderModalContent(treeData)}</Spin>
    </Modal>
  );
});

// roleList
sin's avatar
sin committed
131 132 133 134 135 136 137 138 139 140 141 142
@connect(({ roleList, loading }) => ({
  roleList,
  list: roleList.list,
  data: roleList,
  loading: loading.models.resourceList,
}))
@Form.create()
class RoleList extends PureComponent {
  state = {
    modalVisible: false,
    modalType: 'add', //add update
    initValues: {},
sin's avatar
sin committed
143 144
    roleAssignVisible: false,
    roleAssignRecord: {},
sin's avatar
sin committed
145 146 147 148 149 150 151 152
  };

  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'roleList/query',
      payload: {
        name: '',
153
        pageNo: 1,
sin's avatar
sin committed
154 155 156 157 158 159 160 161 162 163 164 165 166
        pageSize: 10,
      },
    });
  }

  handleModalVisible = (flag, modalType, initValues) => {
    this.setState({
      modalVisible: !!flag,
      initValues: initValues || {},
      modalType: modalType || 'add',
    });
  };

sin's avatar
sin committed
167 168 169 170 171
  handleAssignModalVisible = (flag, record) => {
    const { dispatch } = this.props;
    dispatch({
      type: 'roleList/queryRoleAssign',
      payload: {
172
        roleId: record.id,
sin's avatar
sin committed
173 174 175 176 177 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
      },
    });
    this.setState({
      roleAssignVisible: !!flag,
      roleAssignRecord: record,
    });
  };

  handleAssignModalVisibleClose(flag) {
    this.setState({
      roleAssignVisible: !!flag,
    });
  }

  handleAssignCheckBoxClick = checkedKeys => {
    const { dispatch } = this.props;
    const newCheckedKeys = checkedKeys.map(item => {
      return parseInt(item);
    });
    dispatch({
      type: 'roleList/changeCheckedKeys',
      payload: newCheckedKeys,
    });
  };

  handleAssignOK = () => {
    const { dispatch, data } = this.props;
    const { roleAssignRecord } = this.state;
    dispatch({
      type: 'roleList/roleAssignResource',
      payload: {
204
        roleId: roleAssignRecord.id,
sin's avatar
sin committed
205
        resourceIds: data.checkedKeys,
sin's avatar
sin committed
206
        roleTreeData: data.roleTreeData,
sin's avatar
sin committed
207 208 209 210 211
      },
    });
    this.handleAssignModalVisibleClose(false);
  };

sin's avatar
sin committed
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 275
  handleAdd = ({ fields, modalType, initValues }) => {
    const { dispatch, data } = this.props;
    const queryParams = {
      pageNo: data.pageNo,
      pageSize: data.pageSize,
    };
    if (modalType === 'add') {
      dispatch({
        type: 'roleList/add',
        payload: {
          body: {
            ...fields,
          },
          queryParams,
          callback: () => {
            message.success('添加成功');
            this.handleModalVisible();
          },
        },
      });
    } else {
      dispatch({
        type: 'roleList/update',
        payload: {
          body: {
            ...initValues,
            ...fields,
          },
          queryParams,
          callback: () => {
            message.success('更新成功');
            this.handleModalVisible();
          },
        },
      });
    }
  };

  handleDelete(row) {
    const { dispatch, data } = this.props;
    const queryParams = {
      pageNo: data.pageNo,
      pageSize: data.pageSize,
    };
    Modal.confirm({
      title: `确认删除?`,
      content: `${row.name}`,
      onOk() {
        dispatch({
          type: 'roleList/delete',
          payload: {
            body: {
              id: row.id,
            },
            queryParams,
          },
        });
      },
      onCancel() {},
    });
  }

  render() {
    const { list, data } = this.props;
sin's avatar
sin committed
276
    const { pageNo, pageSize, count, roleTreeData, checkedKeys, assignModalLoading } = data;
sin's avatar
sin committed
277 278
    const { modalVisible, modalType, initValues, roleAssignVisible } = this.state;

sin's avatar
sin committed
279 280 281 282 283 284 285 286
    const parentMethods = {
      handleAdd: this.handleAdd,
      handleModalVisible: this.handleModalVisible,
      modalType,
      initValues,
    };

    const columns = [
287 288 289 290 291
      // {
      //   title: 'id',
      //   dataIndex: 'id',
      //   render: text => <strong>{text}</strong>,
      // },
sin's avatar
sin committed
292 293 294 295
      {
        title: '名称',
        dataIndex: 'name',
      },
296 297 298 299
      {
        title: '编码',
        dataIndex: 'code',
      },
300 301 302 303 304 305 306
      {
        title: '类型',
        dataIndex: 'type',
        render(val) {
          return <span>{types[val]}</span>;
        },
      },
sin's avatar
sin committed
307 308 309
      {
        title: '创建时间',
        dataIndex: 'createTime',
310
        // sorter: true,
sin's avatar
sin committed
311 312 313 314
        render: val => <span>{moment(val).format('YYYY-MM-DD')}</span>,
      },
      {
        title: '操作',
sin's avatar
sin committed
315
        width: 200,
sin's avatar
sin committed
316 317
        render: (text, record) => (
          <Fragment>
318 319 320 321 322
            {record.type === 2 ? (
              <span>
                <a onClick={() => this.handleModalVisible(true, 'update', record)}>更新</a>
              </span>
            ) : null}
sin's avatar
sin committed
323
            <Divider type="vertical" />
sin's avatar
sin committed
324 325
            <a onClick={() => this.handleAssignModalVisible(true, record)}>分配权限</a>
            <Divider type="vertical" />
326 327 328 329 330 331 332
            {record.type === 2 ? (
              <span>
                <a className={styles.tableDelete} onClick={() => this.handleDelete(record)}>
                  删除
                </a>
              </span>
            ) : null}
sin's avatar
sin committed
333 334 335 336 337 338
          </Fragment>
        ),
      },
    ];

    const paginationProps = {
sin's avatar
sin committed
339 340 341
      current: pageNo,
      pageSize: pageSize,
      total: count,
sin's avatar
sin committed
342
    };
sin's avatar
sin committed
343

sin's avatar
sin committed
344 345 346 347 348 349 350 351 352 353
    return (
      <PageHeaderWrapper title="查询表格">
        <Card bordered={false}>
          <div className={styles.tableList}>
            <div className={styles.tableListOperator}>
              <Button
                icon="plus"
                type="primary"
                onClick={() => this.handleModalVisible(true, 'add', {})}
              >
354
                新建角色
sin's avatar
sin committed
355 356 357 358 359 360
              </Button>
            </div>
          </div>
          <Table columns={columns} dataSource={list} rowKey="id" />
        </Card>
        <CreateForm {...parentMethods} modalVisible={modalVisible} />
sin's avatar
sin committed
361 362
        <AssignModal
          loading={assignModalLoading}
sin's avatar
sin committed
363
          treeData={roleTreeData}
sin's avatar
sin committed
364 365 366 367 368 369
          checkedKeys={checkedKeys}
          handleOk={this.handleAssignOK}
          modalVisible={roleAssignVisible}
          handleCheckBoxClick={this.handleAssignCheckBoxClick}
          handleModalVisible={() => this.handleAssignModalVisibleClose(false)}
        />
sin's avatar
sin committed
370 371 372 373 374 375
      </PageHeaderWrapper>
    );
  }
}

export default RoleList;