张延森 5 gadus atpakaļ
vecāks
revīzija
d5d5be0bc8

+ 117
- 0
src/components/PaginationWrapper/index.jsx Parādīt failu

@@ -0,0 +1,117 @@
1
+import React, { useState } from 'react';
2
+import PropTypes from 'prop-types';
3
+import { Pagination } from 'antd';
4
+import classNames from 'classnames'
5
+import { useQuery } from '../../utils/hooks';
6
+import { ObjSpEqual } from '../../utils/utils'
7
+import Styles from './style.less'
8
+
9
+class PaginationWrapper extends React.PureComponent {
10
+
11
+  state = {
12
+    data: {
13
+      current: 1,
14
+      total: 0,
15
+      pages: 0,
16
+    },
17
+    loadding: false,
18
+  }
19
+
20
+  componentDidMount () {
21
+    this.fetchData(1)
22
+  }
23
+
24
+  componentDidUpdate(prevProps) {
25
+    if (!ObjSpEqual(prevProps.params, this.props.params)) {
26
+      this.fetchData(1)
27
+    }
28
+  } 
29
+
30
+  fetchData = (page) => {
31
+    this.setState({ loadding: true })
32
+    
33
+    const requestParams = this.props.params || {}
34
+    this.props.request({
35
+      ...requestParams,
36
+      params: {
37
+        pageNum: page,              // 有的接口是 pageNum
38
+        pageNumber: page,  // 有的接口是 pageNumber
39
+        ...(requestParams.params || {}),
40
+      }
41
+    }).then((res) => {
42
+      this.setState({ loadding: false, data: res })
43
+    })
44
+  }
45
+
46
+  handlePageChange = (page) => {
47
+    this.fetchData(page)
48
+  }
49
+
50
+  render() {
51
+    const { className, style, bodyClass, footerClass, render } = this.props
52
+    const { data } = this.state
53
+
54
+    return (
55
+      <div className={classNames(Styles['pg-wrapper'], className)} style={style}>
56
+        <div className={classNames(Styles.body, bodyClass)}>
57
+          {render(data)}
58
+        </div>
59
+        <div className={classNames(Styles.footer, footerClass)}>
60
+          <Pagination showQuickJumper defaultCurrent={1} total={data.total} onChange={this.handlePageChange} current={data.current}/>
61
+        </div>
62
+      </div>
63
+    )
64
+  }
65
+}
66
+
67
+PaginationWrapper.propTypes = {
68
+  request: PropTypes.func.isRequired,
69
+  render: PropTypes.func.isRequired,
70
+  params: PropTypes.object,
71
+  className: PropTypes.oneOfType([
72
+    PropTypes.string,
73
+    PropTypes.array,
74
+    PropTypes.object,
75
+  ]),
76
+  bodyClass: PropTypes.oneOfType([
77
+    PropTypes.string,
78
+    PropTypes.array,
79
+    PropTypes.object,
80
+  ]),
81
+  footerClass: PropTypes.oneOfType([
82
+    PropTypes.string,
83
+    PropTypes.array,
84
+    PropTypes.object,
85
+  ]),
86
+  style: PropTypes.object,
87
+}
88
+
89
+// /**
90
+//  * 分页封装
91
+//  * @param {page, params, request, render, className, style, bodyClass, footerClass} props 
92
+//  */
93
+// function PaginationWrapper(props) {
94
+//   const defaultPage = props.page || 1
95
+//   const [pageNum, setPage] = useState(defaultPage)
96
+//   const requestParams = props.params || {}
97
+//   const [data = {}, loading, err] = useQuery({ ...requestParams, params: { ...(requestParams.params || {}), pageNum, pageNumber: pageNum }}, props.request)
98
+
99
+//   // if (typeof props.setData === 'function') {
100
+//   //   props.setData(data)
101
+//   // }
102
+
103
+//   console.log('------render PaginationWrapper-------', pageNum, data, loading)
104
+
105
+//   return (
106
+//     <div className={classNames(Styles['pg-wrapper'], props.className)} style={props.style}>
107
+//       <div className={classNames(Styles.body, props.bodyClass)}>
108
+//         {props.render(data)}
109
+//       </div>
110
+//       <div className={classNames(Styles.footer, props.footerClass)}>
111
+//         <Pagination showQuickJumper defaultCurrent={defaultPage} total={data.total} onChange={pg => setPage(pg)} current={data.current}/>
112
+//       </div>
113
+//     </div>
114
+//   )
115
+// }
116
+
117
+export default PaginationWrapper

+ 10
- 0
src/components/PaginationWrapper/style.less Parādīt failu

@@ -0,0 +1,10 @@
1
+.pg-wrapper {
2
+  .body {}
3
+
4
+  .footer {
5
+    margin-top: 30px;
6
+    display: flex;
7
+    align-items: center;
8
+    flex-direction: row-reverse;
9
+  }
10
+}

+ 81
- 0
src/components/SearchForm/index.jsx Parādīt failu

@@ -0,0 +1,81 @@
1
+import React from 'react';
2
+import PropTyps from 'prop-types';
3
+import { Form, Button } from 'antd';
4
+
5
+function isFunc(fn) { return typeof fn === 'function' };
6
+
7
+class SearchForm extends React.PureComponent {
8
+  handleSubmit = e => {
9
+    e.preventDefault();
10
+    this.props.form.validateFields((err, values) => {
11
+      if (!err && isFunc(this.props.onSearch)) {
12
+        this.props.onSearch(values)
13
+        return
14
+      }
15
+
16
+      if (err) {
17
+        console.error(err)
18
+        if (isFunc(this.props.onError)) {
19
+          this.props.onError(err)
20
+        }
21
+        return
22
+      }
23
+    });
24
+  };
25
+
26
+  handleReset = e => {
27
+    this.props.form.resetFields()
28
+    if (isFunc(this.props.onReset)) {
29
+      this.props.onReset()
30
+    }
31
+  }
32
+
33
+  render () {
34
+    const { form, hideResetBtn } = this.props;
35
+
36
+    return (
37
+      <Form layout="inline" onSubmit={this.handleSubmit} colon={false}>
38
+        {
39
+          (this.props.fields || []).map((field, index) => {
40
+            const { element, render, name, label, value } = field || {}
41
+
42
+            if (!element && !render) return null;
43
+
44
+            const conifg = value  ? { initialValue: value } : {}
45
+            const Item = isFunc(render) ? render(this.props) : element
46
+
47
+            return <Form.Item label={label} key={`fm-${index}`}>{form.getFieldDecorator(name, conifg)(Item)}</Form.Item>
48
+          })
49
+        }
50
+        <Form.Item>
51
+          <Button type="primary" htmlType="submit">搜索</Button>
52
+        </Form.Item>
53
+        { hideResetBtn && (
54
+          <Form.Item>
55
+            <Button type="default" onClick="handleReset">重置</Button>
56
+          </Form.Item>
57
+        )}
58
+      </Form>
59
+    );
60
+  }
61
+}
62
+
63
+// 提供给父组件 onChange 事件
64
+function onValuesChange(props, changedValues, allValues) {
65
+  if (isFunc(props.onChange)) {
66
+    props.onChange(changedValues, allValues)
67
+  }
68
+}
69
+
70
+const EnhanceForm = Form.create({ onValuesChange })(SearchForm)
71
+
72
+EnhanceForm.propTypes = {
73
+  onSearch: PropTyps.func.isRequired,
74
+  onReset: PropTyps.func,
75
+  onError: PropTyps.func,
76
+  onChange: PropTyps.func,
77
+  fields: PropTyps.arrayOf(PropTyps.object),
78
+  hideResetBtn: PropTyps.bool,
79
+}
80
+
81
+export default EnhanceForm;

+ 1
- 1
src/layouts/BasicLayout.jsx Parādīt failu

@@ -27,7 +27,7 @@ const footerRender = () => {
27 27
         color: 'rgba(102, 102, 102, 1)',
28 28
       }}
29 29
     >
30
-      copy Right @ 知与行
30
+      Copy Right @ 知与行
31 31
     </div>
32 32
   )
33 33
 }

+ 66
- 30
src/pages/system/messageList.jsx Parādīt failu

@@ -1,32 +1,41 @@
1 1
 import React, { useState, useEffect } from 'react';
2 2
 import { Form, Input, Button, Icon, Select, message, Table, Divider, Tag, Pagination, Modal, DatePicker } from 'antd';
3
-import { FormattedMessage } from 'umi-plugin-react/locale';
3
+// import { FormattedMessage } from 'umi-plugin-react/locale';
4 4
 import styles from '../style/GoodsList.less';
5 5
 import router from 'umi/router';
6 6
 import moment from 'moment';
7
-import SelectCity from '../../components/SelectButton/CitySelect'
8
-import BuildSelect from '../../components/SelectButton/BuildSelect'
9
-import apis from '../../services/apis';
10
-import request from '../../utils/request'
7
+// import SelectCity from '../../components/SelectButton/CitySelect'
8
+// import BuildSelect from '../../components/SelectButton/BuildSelect'
9
+// import apis from '../../services/apis';
10
+// import request from '../../utils/request'
11
+import { fetch, apis } from '../../utils/request'
12
+// import { useQuery } from '../../utils/hooks'
13
+import PaginationWrapper from '../../components/PaginationWrapper'
14
+import SearchForm from '../../components/SearchForm'
11 15
 
12
-const { Option } = Select;
13
-const { MonthPicker, RangePicker, WeekPicker } = DatePicker;
16
+// const { Option } = Select;
17
+// const { MonthPicker, RangePicker, WeekPicker } = DatePicker;
18
+
19
+const getMessageList = fetch(apis.system.taCustomerMessage)
14 20
 
15 21
 const header = (props) => {
16
-  const [ data, setData ] = useState({})
17
-//   const [page, changePage] = useState({})
22
+  // const [ data, setData ] = useState({})
23
+// //   const [page, changePage] = useState({})
24
+
25
+//   useEffect(() => {
26
+//     getList({ pageNum: 1, pageSize: 10 });
27
+//   },[])
18 28
 
19
-  useEffect(() => {
20
-    getList({ pageNum: 1, pageSize: 10 });
21
-  },[])
29
+//   // 查询列表
30
+//   const getList = (params) => {
31
+//     request({ ...apis.system.taCustomerMessage, params: { ...params },}).then((data) => {
32
+//         console.log(data)
33
+//         setData(data)
34
+//     })
35
+//   }
22 36
 
23
-  // 查询列表
24
-  const getList = (params) => {
25
-    request({ ...apis.system.taCustomerMessage, params: { ...params },}).then((data) => {
26
-        console.log(data)
27
-        setData(data)
28
-    })
29
-  }
37
+  // const [pageNum, setPage] = useState(1)
38
+  // const [data, loading, err] = useQuery({params: { pageNum }}, getMessageList)
30 39
 
31 40
   const columns = [
32 41
     {
@@ -51,22 +60,49 @@ const header = (props) => {
51 60
   ];
52 61
    
53 62
   
54
-  const changePageNum = (pageNumber) => {
55
-      getList({ pageNum: pageNumber, pageSize: 10 })
56
-  }
63
+  // const changePageNum = (pageNumber) => {
64
+  //     getList({ pageNum: pageNumber, pageSize: 10 })
65
+  // }
66
+
67
+  
68
+  console.log('------render messagelist-------')
57 69
  
58 70
 
59
-  const { getFieldDecorator } = props.form
71
+  // const { getFieldDecorator } = props.form
60 72
   return (
61
-
62 73
     <>
63
-      <Table rowKey="messageList" dataSource={data.records} columns={columns} pagination={false}/>
64
-      <div style={{ display: 'flex', justifyContent: 'flex-end', marginTop: '30px' }}>
65
-        <Pagination showQuickJumper defaultCurrent={1} total={data.total} onChange={changePageNum} current={data.current}/>
66
-      </div>
74
+    <SearchForm
75
+      onSearch={console.log}
76
+      fields={[
77
+        {
78
+          label: 'Foo',
79
+          name: 'foo',
80
+          element: <Input />,
81
+        },
82
+        {
83
+          label: 'Bar',
84
+          name: 'bar',
85
+          element: (<Select style={{ width: '200px' }}>
86
+            <Select.Option value={1}>a</Select.Option>
87
+            <Select.Option value={2}>b</Select.Option>
88
+          </Select>)
89
+        },
90
+      ]}
91
+    ></SearchForm>
92
+    <PaginationWrapper
93
+      request={getMessageList}
94
+      render={data => <Table rowKey={record => record.messageId} dataSource={data.records} columns={columns} pagination={false}/>}
95
+    >
96
+    </PaginationWrapper>
67 97
     </>
98
+    // <>
99
+    //   <Table rowKey="messageList" dataSource={data.records} columns={columns} pagination={false}/>
100
+    //   <div style={{ display: 'flex', justifyContent: 'flex-end', marginTop: '30px' }}>
101
+    //     <Pagination showQuickJumper defaultCurrent={1} total={data.total} onChange={pg => setPage(pg)} current={data.current}/>
102
+    //   </div>
103
+    // </>
68 104
   )
69 105
 }
70
-const WrappedHeader = Form.create({ name: 'header' })(header);
106
+// const WrappedHeader = Form.create({ name: 'header' })(header);
71 107
 
72
-export default WrappedHeader
108
+export default header

+ 39
- 0
src/utils/hooks.js Parādīt failu

@@ -0,0 +1,39 @@
1
+import React, { useState, useEffect } from 'react';
2
+
3
+function ObjEqual(a, b) {
4
+  if (typeof(a) !== 'object' || typeof(b) !== 'object') {
5
+    return false
6
+  }
7
+
8
+  return JSON.stringify(a) === JSON.stringify(b)
9
+}
10
+
11
+export function useQuery(params, request) {
12
+  const [preParams, setParams] = useState({})
13
+  const [data, setData] = useState({})
14
+  const [error, setError] = useState()
15
+  const [loading, setLoading] = useState(false)
16
+
17
+  useEffect(() => {
18
+    if (ObjEqual(preParams, params)) {
19
+      return
20
+    }
21
+
22
+    setLoading(true)
23
+    setParams(params)
24
+    request(params).then((res) => {
25
+      setData(res)
26
+      setError(void 0)
27
+      setLoading(false)
28
+    }).catch((err) => {
29
+      setError(err)
30
+      setLoading(false)
31
+    })
32
+  })
33
+
34
+  return [
35
+    data,
36
+    loading,
37
+    error,
38
+  ]
39
+}

+ 4
- 0
src/utils/utils.js Parādīt failu

@@ -21,3 +21,7 @@ export const isAntDesignProOrDev = () => {
21 21
   return isAntDesignPro();
22 22
 };
23 23
 export const getPageQuery = () => parse(window.location.href.split('?')[1]);
24
+
25
+export function ObjSpEqual(a = {}, b = {}) {
26
+  return JSON.stringify(a) === JSON.stringify(b)
27
+}