Your Name 2 years ago
parent
commit
2b4208b126

+ 16
- 0
src/components/Uploader/ImageUploader.jsx View File

@@ -0,0 +1,16 @@
1
+import React from 'react';
2
+import Uploader from '.';
3
+
4
+export default (props) => {
5
+  const {value, onChange} = props;
6
+
7
+  const onDelete = (e) => {
8
+    const { file } = e.detail
9
+    const newVals = value.filter(x => x.url != file.url);
10
+    onChange(newVals);
11
+  }
12
+  
13
+  return (
14
+    <Uploader deletable fileList={value} onDelete={onDelete} onChange={onChange} />
15
+  )
16
+}

+ 21
- 0
src/components/Uploader/index.jsx View File

@@ -0,0 +1,21 @@
1
+import React from 'react';
2
+import { Uploader } from '@antmjs/vantui';
3
+import { uploadFiles } from '@/utils/request';
4
+
5
+export default (props) => {
6
+  const { fileList = [] } = props;
7
+
8
+  const onAfterRead = (e) => {
9
+    const { file, name } = e.detail
10
+
11
+    uploadFiles([file.url]).then(res => {
12
+      if (props.onChange) {
13
+        props.onChange(fileList.concat([{url: res[0], name}]));
14
+      }
15
+    });
16
+  }
17
+
18
+  return (
19
+    <Uploader onAfterRead={onAfterRead} {...props} />
20
+  )
21
+}

+ 45
- 18
src/pages/issue/add/index.jsx View File

@@ -1,38 +1,61 @@
1 1
 import React from 'react';
2 2
 import Taro from '@tarojs/taro';
3 3
 import { View } from '@tarojs/components';
4
-import { Notify, Field, Cell, CellGroup, Uploader } from '@antmjs/vantui';
4
+import { Button, Notify, Field, Cell, CellGroup, Uploader } from '@antmjs/vantui';
5 5
 import Page from '@/layouts/index';
6 6
 import LocType from '@/components/locType';
7 7
 import IssueType from '@/components/issueType';
8 8
 import Map from '@/components/map';
9
+import ImageUploader from '@/components/Uploader/ImageUploader';
9 10
 import getAuthorize from '@/utils/authorize';
10 11
 import { ROLE_INSPECTOR } from '@/utils/user';
11 12
 import mapIcon from '@/assets/icons/marker.png';
12 13
 
13 14
 export default (props) => {
14 15
 
16
+  const [formData, setFormData] = React.useState({
17
+    typeId: undefined,
18
+    typeName: undefined,
19
+    locId: undefined,
20
+    locName: undefined,
21
+    location: undefined,
22
+    addr: undefined,
23
+    content: undefined,
24
+    images: [],
25
+  });
15 26
   const [showLocType, setShowLocType] = React.useState(false);
16 27
   const [showIssueType, setShowIssueType] = React.useState(false);
17
-  const [loc, setLoc] = React.useState();
18
-  const [locType, setLocType] = React.useState();
19
-  const [issueType, setIssueType] = React.useState();
20
-  const [address, setAddress] = React.useState();
21 28
 
22 29
   const onLocTypeChange = (_, it) => {
23
-    setLocType(it);
30
+    setFormData({
31
+      ...formData,
32
+      locId: it.typeId,
33
+      locName: it.name,
34
+    });
24 35
     setShowLocType(false);
25 36
   }
37
+
26 38
   const onIssueTypeChange = (_, it) => {
27
-    setIssueType(it);
39
+    setFormData({
40
+      ...formData,
41
+      typeId: it.typeId,
42
+      typeName: it.name,
43
+    });
28 44
     setShowIssueType(false);
29 45
   }
46
+
47
+  const onFieldChange = (field, value) => {
48
+    setFormData({
49
+      ...formData,
50
+      [field]: value,
51
+    })
52
+  }
30 53
   
31 54
   React.useMemo(() => {
32 55
     getAuthorize('scope.userLocation').then(() => {
33 56
       Taro.getLocation({
34 57
         success(res) {
35
-          setLoc(`${res.longitude},${res.latitude}`);
58
+          onFieldChange('location', `${res.longitude},${res.latitude}`);
36 59
         },
37 60
         fail() {
38 61
           Notify.show({
@@ -53,32 +76,32 @@ export default (props) => {
53 76
     <Page roles={[ROLE_INSPECTOR]}>
54 77
       <LocType
55 78
         show={showLocType}
56
-        value={locType?.typeId}
79
+        value={formData.addr}
57 80
         onCancel={() => setShowLocType(false)}
58 81
         onChange={onLocTypeChange}
59 82
       />
60 83
 
61 84
       <IssueType
62 85
         show={showIssueType}
63
-        value={issueType?.typeId}
86
+        value={formData.typeName}
64 87
         onCancel={() => setShowIssueType(false)}
65 88
         onChange={onIssueTypeChange}
66 89
       />
67 90
 
68
-      <Map location={loc} />
91
+      <Map location={formData.location} />
69 92
 
70 93
       <Field
71
-        value={address}
94
+        value={formData.addr}
72 95
         leftIcon={mapIcon}
73 96
         placeholder="请输入地址"
74
-        onChange={e => setAddress(e.detail)}
97
+        onChange={e => onFieldChange('addr', e.detail)}
75 98
       />
76 99
 
77 100
       <CellGroup style={{marginTop: '20px'}}>
78 101
         <Cell
79 102
           isLink
80 103
           title="点位"
81
-          value={locType?.name}
104
+          value={formData.locName}
82 105
           onClick={() => setShowLocType(true)}
83 106
         />
84 107
 
@@ -88,6 +111,8 @@ export default (props) => {
88 111
           type="textarea"
89 112
           placeholder="请输入问题描述"
90 113
           autosize={{ minHeight: '120px' }}
114
+          value={formData.content}
115
+          onChange={e => onFieldChange('content', e.detail)}
91 116
         />
92 117
       </CellGroup>
93 118
       
@@ -95,7 +120,7 @@ export default (props) => {
95 120
         isLink
96 121
         title="问题分类"
97 122
         style={{marginTop: '20px'}}
98
-        value={issueType?.name}
123
+        value={formData.typeName}
99 124
         onClick={() => setShowIssueType(true)}
100 125
       />
101 126
 
@@ -105,12 +130,14 @@ export default (props) => {
105 130
 
106 131
         <Cell
107 132
           renderTitle={
108
-            <Uploader
109
-              deletable
110
-            />
133
+            <ImageUploader value={formData.images} onChange={e => onFieldChange('images',e)} />
111 134
           }
112 135
         />
113 136
       </CellGroup>
137
+
138
+      <View style={{margin: '20px auto', padding: '0 1em'}}>
139
+        <Button block type="primary">提交</Button>
140
+      </View>
114 141
     </Page>
115 142
   )
116 143
 }

+ 32
- 0
src/utils/request.js View File

@@ -72,3 +72,35 @@ export default function request(api, options = {}) {
72 72
 
73 73
   });
74 74
 }
75
+
76
+
77
+export const uploadFiles = async (files, url) => {
78
+  const uploads = []
79
+  const token = getToken();
80
+  for (var i = 0; i < files.length; i++) {
81
+    uploads[i] = new Promise((resolve, reject) => {
82
+      Taro.uploadFile({
83
+        url: url || `${HOST}/api/ma/image`,
84
+        filePath: files[i],
85
+        header: {
86
+          'authorization': token,
87
+        },
88
+        name: 'file',
89
+        success: function (res) {
90
+          // debugger
91
+          const _data = JSON.parse(res.data)
92
+          // if (_data.code !== CODE_SUCCESS) {
93
+          //   reject(new Error(_data.message))
94
+          // }
95
+
96
+          resolve(_data.data)
97
+        },
98
+        fail(err) {
99
+          reject(err)
100
+        }
101
+      })
102
+    })
103
+  }
104
+
105
+  return Promise.all(uploads)
106
+}