RoleList.js 8.5 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;
sin's avatar
sin committed
13 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 48 49 50

// 添加 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>
    </Modal>
  );
});

sin's avatar
sin committed
51
// 角色分配
sin's avatar
sin committed
52 53 54 55 56 57 58 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
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
86
              multiple={true}
sin's avatar
sin committed
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 114 115 116 117 118 119 120 121 122 123
              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
124 125 126 127 128 129 130 131 132 133 134 135
@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
136 137
    roleAssignVisible: false,
    roleAssignRecord: {},
sin's avatar
sin committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
  };

  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'roleList/query',
      payload: {
        name: '',
        pageNo: 0,
        pageSize: 10,
      },
    });
  }

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

sin's avatar
sin committed
160 161 162 163 164 165 166 167 168 169 170 171 172 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
  handleAssignModalVisible = (flag, record) => {
    const { dispatch } = this.props;
    dispatch({
      type: 'roleList/queryRoleAssign',
      payload: {
        id: record.id,
      },
    });
    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: {
        id: roleAssignRecord.id,
        resourceIds: data.checkedKeys,
sin's avatar
sin committed
199
        roleTreeData: data.roleTreeData,
sin's avatar
sin committed
200 201 202 203 204
      },
    });
    this.handleAssignModalVisibleClose(false);
  };

sin's avatar
sin committed
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 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
  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
269
    const { pageNo, pageSize, count, roleTreeData, checkedKeys, assignModalLoading } = data;
sin's avatar
sin committed
270 271
    const { modalVisible, modalType, initValues, roleAssignVisible } = this.state;

sin's avatar
sin committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
    const parentMethods = {
      handleAdd: this.handleAdd,
      handleModalVisible: this.handleModalVisible,
      modalType,
      initValues,
    };

    const columns = [
      {
        title: 'id',
        dataIndex: 'id',
        render: text => <strong>{text}</strong>,
      },
      {
        title: '名称',
        dataIndex: 'name',
      },
      {
        title: '创建时间',
        dataIndex: 'createTime',
        sorter: true,
        render: val => <span>{moment(val).format('YYYY-MM-DD')}</span>,
      },
      {
        title: '操作',
sin's avatar
sin committed
297
        width: 200,
sin's avatar
sin committed
298 299 300 301
        render: (text, record) => (
          <Fragment>
            <a onClick={() => this.handleModalVisible(true, 'update', record)}>更新</a>
            <Divider type="vertical" />
sin's avatar
sin committed
302 303
            <a onClick={() => this.handleAssignModalVisible(true, record)}>分配权限</a>
            <Divider type="vertical" />
sin's avatar
sin committed
304 305 306 307 308 309 310 311 312
            <a className={styles.tableDelete} onClick={() => this.handleDelete(record)}>
              删除
            </a>
          </Fragment>
        ),
      },
    ];

    const paginationProps = {
sin's avatar
sin committed
313 314 315
      current: pageNo,
      pageSize: pageSize,
      total: count,
sin's avatar
sin committed
316
    };
sin's avatar
sin committed
317

sin's avatar
sin committed
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
    return (
      <PageHeaderWrapper title="查询表格">
        <Card bordered={false}>
          <div className={styles.tableList}>
            <div className={styles.tableListOperator}>
              <Button
                icon="plus"
                type="primary"
                onClick={() => this.handleModalVisible(true, 'add', {})}
              >
                新建
              </Button>
            </div>
          </div>
          <Table columns={columns} dataSource={list} rowKey="id" />
        </Card>
        <CreateForm {...parentMethods} modalVisible={modalVisible} />
sin's avatar
sin committed
335 336
        <AssignModal
          loading={assignModalLoading}
sin's avatar
sin committed
337
          treeData={roleTreeData}
sin's avatar
sin committed
338 339 340 341 342 343
          checkedKeys={checkedKeys}
          handleOk={this.handleAssignOK}
          modalVisible={roleAssignVisible}
          handleCheckBoxClick={this.handleAssignCheckBoxClick}
          handleModalVisible={() => this.handleAssignModalVisibleClose(false)}
        />
sin's avatar
sin committed
344 345 346 347 348 349
      </PageHeaderWrapper>
    );
  }
}

export default RoleList;