ResourceList.js 7.6 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 7 8 9 10 11 12 13 14 15 16 17 18
import {
  Card,
  Form,
  Input,
  Select,
  Button,
  Modal,
  message,
  Table,
  TreeSelect,
  Radio,
  Divider,
} from 'antd';
sin's avatar
sin committed
19 20 21 22
import PageHeaderWrapper from '@/components/PageHeaderWrapper';

import styles from './ResourceList.less';

23
const RadioGroup = Radio.Group;
sin's avatar
sin committed
24 25 26 27 28 29
const FormItem = Form.Item;
const { Option } = Select;
const types = ['未知', '菜单', '链接'];

// 添加 form 表单
const CreateForm = Form.create()(props => {
30 31 32 33 34 35 36 37 38
  const {
    modalVisible,
    form,
    handleAdd,
    handleModalVisible,
    modalType,
    initValues,
    selectTree,
  } = props;
sin's avatar
sin committed
39 40 41 42

  const okHandle = () => {
    form.validateFields((err, fieldsValue) => {
      if (err) return;
43 44 45 46 47
      let pid = fieldsValue.pid;
      if (fieldsValue.pid) {
        pid = pid.split('-')[1];
        fieldsValue.pid = pid;
      }
sin's avatar
sin committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
      form.resetFields();
      handleAdd({
        fields: fieldsValue,
        modalType,
        initValues,
      });
    });
  };

  const selectStyle = {
    width: 200,
  };

  const title = modalType === 'add' ? '添加一个 Resource' : '更新一个 Resource';
  const okText = modalType === 'add' ? '添加' : '更新';
  return (
    <Modal
      destroyOnClose
      title={title}
      visible={modalVisible}
      onOk={okHandle}
      okText={okText}
      onCancel={() => handleModalVisible()}
    >
72
      <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="类型">
73
        {form.getFieldDecorator('type', {
74 75 76 77 78 79 80 81 82
          initialValue: initValues.type || 1,
        })(
          <RadioGroup>
            <Radio value={1}>菜单</Radio>
            <Radio value={2}>按钮</Radio>
          </RadioGroup>
        )}
      </FormItem>
      <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="名称">
83
        {form.getFieldDecorator('displayName', {
sin's avatar
sin committed
84 85
          rules: [{ required: true, message: '请输入菜单展示名字!', min: 2 }],
          initialValue: initValues.displayName,
86
        })(<Input placeholder="显示名称" />)}
sin's avatar
sin committed
87
      </FormItem>
88
      <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="菜单KEY">
sin's avatar
sin committed
89 90 91
        {form.getFieldDecorator('name', {
          rules: [{ required: true, message: '请输入资源名字!' }],
          initialValue: initValues.name,
92
        })(<Input placeholder="菜单KEY 如:用户 USER" />)}
sin's avatar
sin committed
93
      </FormItem>
94 95 96 97 98 99
      <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="操作/路由">
        {form.getFieldDecorator('handler', {
          initialValue: initValues.handler,
        })(<Input placeholder="操作/路由" />)}
      </FormItem>
      <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="父级菜单">
sin's avatar
sin committed
100 101
        {form.getFieldDecorator('pid', {
          rules: [{ required: true, message: '请输入父级编号!' }],
sin's avatar
sin committed
102 103 104 105
          initialValue:
            initValues.pid === 0
              ? `根节点-${initValues.pid}`
              : `${initValues.displayName}-${initValues.pid}`,
106 107 108 109 110 111 112 113 114
        })(
          <TreeSelect
            showSearch
            style={{ width: 300 }}
            dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
            treeData={selectTree}
            placeholder="选择父级菜单"
          />
        )}
sin's avatar
sin committed
115
      </FormItem>
116
      <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="菜单排序">
sin's avatar
sin committed
117 118 119 120 121 122 123 124 125 126 127
        {form.getFieldDecorator('sort', {
          rules: [{ required: true, message: '请输入菜单排序!' }],
          initialValue: initValues.sort,
        })(<Input placeholder="请输入" />)}
      </FormItem>
    </Modal>
  );
});

@connect(({ resourceList, loading }) => ({
  resourceList,
sin's avatar
sin committed
128
  list: resourceList.list,
sin's avatar
sin committed
129 130 131 132 133 134 135 136 137 138 139 140 141
  loading: loading.models.resourceList,
}))
@Form.create()
class ResourceList extends PureComponent {
  state = {
    modalVisible: false,
    modalType: 'add', //add update
    initValues: {},
  };

  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
sin's avatar
sin committed
142
      type: 'resourceList/tree',
sin's avatar
sin committed
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 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 204
      payload: {},
    });
  }

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

  handleAdd = ({ fields, modalType, initValues }) => {
    const { dispatch } = this.props;
    if (modalType === 'add') {
      dispatch({
        type: 'resourceList/add',
        payload: {
          body: {
            ...fields,
          },
          callback: () => {
            message.success('添加成功');
            this.handleModalVisible();
          },
        },
      });
    } else {
      dispatch({
        type: 'resourceList/update',
        payload: {
          body: {
            ...initValues,
            ...fields,
          },
          callback: () => {
            message.success('更新成功');
            this.handleModalVisible();
          },
        },
      });
    }
  };

  handleDelete(row) {
    const { dispatch } = this.props;
    Modal.confirm({
      title: `确认删除?`,
      content: `${row.displayName}`,
      onOk() {
        dispatch({
          type: 'resourceList/delete',
          payload: {
            id: row.id,
          },
        });
      },
      onCancel() {},
    });
  }

  render() {
205 206
    const { list, resourceList } = this.props;
    const { selectTree } = resourceList;
207
    const { modalVisible, modalType, initValues } = this.state;
sin's avatar
sin committed
208 209 210 211 212 213 214 215 216
    const parentMethods = {
      handleAdd: this.handleAdd,
      handleModalVisible: this.handleModalVisible,
      modalType,
      initValues,
    };

    const columns = [
      {
217
        title: '编号',
sin's avatar
sin committed
218 219 220 221
        dataIndex: 'id',
        render: text => <strong>{text}</strong>,
      },
      {
222
        title: '显示名字',
sin's avatar
sin committed
223 224 225 226
        dataIndex: 'displayName',
        render: text => <a>{text}</a>,
      },
      {
227
        title: '菜单KEY',
sin's avatar
sin committed
228 229 230
        dataIndex: 'name',
      },
      {
231
        title: '父级编号',
sin's avatar
sin committed
232 233 234 235 236 237 238 239 240 241 242 243
        dataIndex: 'pid',
        sorter: true,
        render: val => `${val}`,
      },
      {
        title: '类型',
        dataIndex: 'type',
        render(val) {
          return <span>{types[val]}</span>;
        },
      },
      {
244
        title: '菜单/操作',
sin's avatar
sin committed
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 276 277 278 279 280 281 282 283
        dataIndex: 'handler',
        sorter: true,
        width: 300,
        render: val => <span>{val}</span>,
      },
      {
        title: '创建时间',
        dataIndex: 'createTime',
        sorter: true,
        render: val => <span>{moment(val).format('YYYY-MM-DD')}</span>,
      },
      {
        title: '操作',
        render: (text, record) => (
          <Fragment>
            <a onClick={() => this.handleModalVisible(true, 'update', record)}>更新</a>
            <Divider type="vertical" />
            <a className={styles.tableDelete} onClick={() => this.handleDelete(record)}>
              删除
            </a>
          </Fragment>
        ),
      },
    ];

    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>
284
          <Table defaultExpandAllRows={true} columns={columns} dataSource={list} rowKey="id" />
sin's avatar
sin committed
285
        </Card>
286
        <CreateForm {...parentMethods} selectTree={selectTree} modalVisible={modalVisible} />
sin's avatar
sin committed
287 288 289 290 291 292
      </PageHeaderWrapper>
    );
  }
}

export default ResourceList;