Your Name 2 anni fa
parent
commit
2b4208b126

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

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 Vedi File

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 Vedi File

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

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

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
+}