From 77290689d1b1d442c3af9ee8c3de20c8352d8ca4 Mon Sep 17 00:00:00 2001
From: sin <2943460818@qq.com>
Date: Tue, 5 Mar 2019 21:18:54 +0800
Subject: [PATCH] =?UTF-8?q?-=20=E6=B7=BB=E5=8A=A0=E8=B5=84=E6=BA=90?=
 =?UTF-8?q?=E6=A8=A1=E5=9D=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 admin-web/src/models/resourceList.js        |  65 +++++
 admin-web/src/pages/Admin/ResourceList.js   | 256 ++++++++++++++++++++
 admin-web/src/pages/Admin/ResourceList.less |  15 ++
 admin-web/src/services/api.js               |   2 +
 admin-web/src/services/resource.js          |  26 ++
 5 files changed, 364 insertions(+)
 create mode 100644 admin-web/src/models/resourceList.js
 create mode 100644 admin-web/src/pages/Admin/ResourceList.js
 create mode 100644 admin-web/src/pages/Admin/ResourceList.less
 create mode 100644 admin-web/src/services/resource.js

diff --git a/admin-web/src/models/resourceList.js b/admin-web/src/models/resourceList.js
new file mode 100644
index 00000000..3fc3e459
--- /dev/null
+++ b/admin-web/src/models/resourceList.js
@@ -0,0 +1,65 @@
+import { message } from 'antd';
+import { addResource, updateResource, deleteResource, resourceTree } from '@/services/resource';
+
+export default {
+  namespace: 'resourceList',
+
+  state: {
+    list: [],
+  },
+
+  effects: {
+    *add({ payload }, { call, put }) {
+      const { callback, body } = payload;
+      const response = yield call(addResource, body);
+      if (callback) {
+        callback(response);
+      }
+      yield put({
+        type: 'tree',
+        payload: {},
+      });
+    },
+    *update({ payload }, { call, put }) {
+      const { callback, body } = payload;
+      const response = yield call(updateResource, body);
+      if (callback) {
+        callback(response);
+      }
+      yield put({
+        type: 'tree',
+        payload: {},
+      });
+    },
+    *delete({ payload }, { call, put }) {
+      const response = yield call(deleteResource, payload);
+      message.info('删除成功!');
+      yield put({
+        type: 'treeSuccess',
+        payload: {
+          list: response.data,
+        },
+      });
+    },
+    *tree({ payload }, { call, put }) {
+      const { queryParams } = payload;
+      const response = yield call(resourceTree, queryParams);
+      message.info('查询成功!');
+      yield put({
+        type: 'treeSuccess',
+        payload: {
+          list: response.data,
+        },
+      });
+    },
+  },
+
+  reducers: {
+    treeSuccess(state, { payload }) {
+      return {
+        ...state,
+        ...payload,
+      };
+    },
+  },
+};
diff --git a/admin-web/src/pages/Admin/ResourceList.js b/admin-web/src/pages/Admin/ResourceList.js
new file mode 100644
index 00000000..31d77285
--- /dev/null
+++ b/admin-web/src/pages/Admin/ResourceList.js
@@ -0,0 +1,256 @@
+/* eslint-disable */
+
+import React, { PureComponent, Fragment } from 'react';
+import { connect } from 'dva';
+import moment from 'moment';
+import { Card, Form, Input, Select, Button, Modal, message, Table, Divider } from 'antd';
+import PageHeaderWrapper from '@/components/PageHeaderWrapper';
+
+import styles from './ResourceList.less';
+
+const FormItem = Form.Item;
+const { Option } = Select;
+const types = ['未知', '菜单', '链接'];
+
+// 添加 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 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()}
+    >
+      <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="菜单展示名">
+        {form.getFieldDecorator('displayName', {
+          rules: [{ required: true, message: '请输入菜单展示名字!', min: 2 }],
+          initialValue: initValues.displayName,
+        })(<Input placeholder="请输入" />)}
+      </FormItem>
+      <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="资源名字">
+        {form.getFieldDecorator('name', {
+          rules: [{ required: true, message: '请输入资源名字!' }],
+          initialValue: initValues.name,
+        })(<Input placeholder="请输入" />)}
+      </FormItem>
+      <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="父级资源编号">
+        {form.getFieldDecorator('pid', {
+          rules: [{ required: true, message: '请输入父级编号!' }],
+          initialValue: initValues.pid,
+        })(<Input placeholder="请输入" />)}
+      </FormItem>
+      <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="排序">
+        {form.getFieldDecorator('sort', {
+          rules: [{ required: true, message: '请输入菜单排序!' }],
+          initialValue: initValues.sort,
+        })(<Input placeholder="请输入" />)}
+      </FormItem>
+      <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="资源类型">
+        {form.getFieldDecorator('type', {
+          rules: [{ required: true, message: '请选择资源类型!' }],
+          initialValue: 1,
+        })(
+          <Select showSearch style={selectStyle} placeholder="请选择">
+            <Option value={1}>菜单</Option>
+            <Option value={2}>Url</Option>
+          </Select>
+        )}
+      </FormItem>
+    </Modal>
+  );
+});
+
+@connect(({ resourceList, loading }) => ({
+  resourceList,
+  loading: loading.models.resourceList,
+}))
+@Form.create()
+class ResourceList extends PureComponent {
+  state = {
+    modalVisible: false,
+    modalType: 'add', //add update
+    initValues: {},
+  };
+
+  componentDidMount() {
+    const { dispatch } = this.props;
+    dispatch({
+      type: 'resourceList/tree',
+      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() {
+    const {
+      resourceList: { list },
+    } = this.props;
+    const { modalVisible, modalType, initValues } = this.state;
+    const parentMethods = {
+      handleAdd: this.handleAdd,
+      handleModalVisible: this.handleModalVisible,
+      modalType,
+      initValues,
+    };
+
+    const columns = [
+      {
+        title: 'id',
+        dataIndex: 'id',
+        render: text => <strong>{text}</strong>,
+      },
+      {
+        title: '显示名称',
+        dataIndex: 'displayName',
+        render: text => <a>{text}</a>,
+      },
+      {
+        title: '名称',
+        dataIndex: 'name',
+      },
+      {
+        title: 'pid',
+        dataIndex: 'pid',
+        sorter: true,
+        render: val => `${val}`,
+      },
+      {
+        title: '类型',
+        dataIndex: 'type',
+        render(val) {
+          return <span>{types[val]}</span>;
+        },
+      },
+      {
+        title: '资源地址',
+        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>
+          <Table columns={columns} dataSource={list} rowKey="id" />
+        </Card>
+        <CreateForm {...parentMethods} modalVisible={modalVisible} />
+      </PageHeaderWrapper>
+    );
+  }
+}
+
+export default ResourceList;
diff --git a/admin-web/src/pages/Admin/ResourceList.less b/admin-web/src/pages/Admin/ResourceList.less
new file mode 100644
index 00000000..ebb45c29
--- /dev/null
+++ b/admin-web/src/pages/Admin/ResourceList.less
@@ -0,0 +1,15 @@
+@import '~antd/lib/style/themes/default.less';
+@import '~@/utils/utils.less';
+
+.tableList {
+  .tableListOperator {
+    margin-bottom: 16px;
+    button {
+      margin-right: 8px;
+    }
+  }
+}
+
+.tableDelete {
+  color: red;
+}
diff --git a/admin-web/src/services/api.js b/admin-web/src/services/api.js
index 45c744dc..e9cde9e1 100644
--- a/admin-web/src/services/api.js
+++ b/admin-web/src/services/api.js
@@ -106,12 +106,14 @@ export async function updateFakeList(params) {
 export async function fakeAccountLogin(params) {
   return request(`/admin-api/admin/passport/login?${stringify(params)}`, {
     method: 'POST',
+    body: params,
   });
 }
 
 export async function fakeRegister(params) {
   return request(`/admin-api/admin/passport/login?${stringify(params)}`, {
     method: 'POST',
+    body: params,
   });
 }
 
diff --git a/admin-web/src/services/resource.js b/admin-web/src/services/resource.js
new file mode 100644
index 00000000..4ec1a0a4
--- /dev/null
+++ b/admin-web/src/services/resource.js
@@ -0,0 +1,26 @@
+import { stringify } from 'qs';
+import request from '@/utils/request';
+
+export async function addResource(params) {
+  return request(`/admin-api/admin/resource/add?${stringify(params)}`, {
+    method: 'POST',
+  });
+}
+
+export async function updateResource(params) {
+  return request(`/admin-api/admin/resource/update?${stringify(params)}`, {
+    method: 'POST',
+  });
+}
+
+export async function deleteResource(params) {
+  return request(`/admin-api/admin/resource/delete?${stringify(params)}`, {
+    method: 'POST',
+  });
+}
+
+export async function resourceTree(params) {
+  return request(`/admin-api/admin/resource/tree?${stringify(params)}`, {
+    method: 'GET',
+  });
+}
-- 
2.17.1