张延森 5 years ago
parent
commit
7d8a1a9fea

+ 41
- 27
src/components/XForm/ImageListUpload.jsx View File

@@ -3,63 +3,77 @@ import { Upload, Icon, Modal } from 'antd';
3 3
 import './style.less';
4 4
 import { uploaderProps } from '../../utils/upload';
5 5
 
6
-function getBase64(file) {
7
-  return new Promise((resolve, reject) => {
8
-    const reader = new FileReader();
9
-    reader.readAsDataURL(file);
10
-    reader.onload = () => resolve(reader.result);
11
-    reader.onerror = error => reject(error);
12
-  });
13
-}
14
-
15 6
 class ImageListUpload extends React.Component {
16 7
   state = {
17 8
     previewVisible: false,
18 9
     previewImage: '',
19
-    fileList: [],
10
+    loadding: false,
20 11
   };
21 12
 
13
+  getFileList = () => {
14
+    return (this.props.value || []).map((img, inx) => ({ uid: inx, url: img, status: 'done' }))
15
+  }
16
+
22 17
   handleCancel = () => this.setState({ previewVisible: false });
23 18
 
24 19
   handlePreview = async file => {
25
-    if (!file.url && !file.preview) {
26
-      file.preview = await getBase64(file.originFileObj);
27
-    }
28
-
29 20
     this.setState({
30
-      previewImage: file.url || file.preview,
21
+      previewImage: file.url ,
31 22
       previewVisible: true,
32 23
     });
33 24
   };
34 25
 
35
-  handleChange = ({ fileList }) => {
36
-    this.setState({ fileList })
37
-    if (typeof this.props.onChange === 'function') {
38
-      // return item.response.url
39
-      const images = fileList.filter(item => item.status === 'done')
40
-      this.props.onChange(images.map(item => item.response.url))
26
+  handleChange = (e) => {
27
+    if (e.file.status === "uploading") {
28
+      this.setState({ loading: true });
29
+      return;
30
+    }
31
+
32
+    if (e.file.state === 'removed') {
33
+      const fileList = (this.props.value || []).filter(x => x != e.file.url);
34
+      this.props.onChange(fileList);
41 35
     }
36
+
37
+    // if (e.file.status === "done") {
38
+    //   this.setState({
39
+    //     loading: false,
40
+    //   })
41
+
42
+    //   if (e.file.response && e.file.response.url) {
43
+    //     if (typeof this.props.onChange === 'function') {
44
+    //       const fileList = this.getFileList()
45
+    //       this.props.onChange([...fileList || [], e.file.response.url]);
46
+    //     }
47
+    //   }
48
+    // }
42 49
   };
43 50
 
44
-  render() {
45
-    const { previewVisible, previewImage, fileList } = this.state;
51
+  handleUploadSucess = (url) => {
52
+    this.setState({ loading: false });
53
+    if (typeof this.props.onChange === 'function') {
54
+      const fileList = this.props.value || [];
55
+      this.props.onChange([...fileList, url]);
56
+    }
57
+  }
46 58
 
47
-    const { value } = this.props
59
+  render() {
60
+    const { previewVisible, previewImage } = this.state;
61
+    const fileList = this.getFileList();
48 62
 
49 63
     const uploadButton = (
50 64
       <div>
51
-        <Icon type="plus" />
52
-        <div className="ant-upload-text">Upload</div>
65
+        <Icon style={{ fontSize: '2em', color: '#aaa' }} type={this.state.loading ? "loading" : "plus"}  />
53 66
       </div>
54 67
     );
55 68
     return (
56 69
       <div className="clearfix">
57 70
         <Upload
58 71
           listType="picture-card"
59
-          fileList={fileList || value}
72
+          fileList={fileList}
60 73
           onPreview={this.handlePreview}
61 74
           onChange={this.handleChange}
62 75
           { ...uploaderProps }
76
+          onSuccess={this.handleUploadSucess}
63 77
         >
64 78
           {fileList.length >= 8 ? null : uploadButton}
65 79
         </Upload>

+ 25
- 14
src/components/XForm/ImageUpload.jsx View File

@@ -16,24 +16,35 @@ class ImageUpload extends React.Component {
16 16
       this.setState({ loading: true });
17 17
       return;
18 18
     }
19
+    
20
+    if (e.file.state === 'removed') {
21
+      this.props.onChange();
22
+    }
19 23
 
20
-    if (info.file.status === "done") {
21
-      this.setState({
22
-        loading: false,
23
-      })
24
+    // if (info.file.status === "done") {
25
+    //   this.setState({
26
+    //     loading: false,
27
+    //   })
24 28
 
25
-      if (info.file.response && info.file.response.url) {
26
-        this.setState({
27
-          imageUrl: info.file.response.thumbUrl,
28
-        });
29
+    //   if (info.file.response && info.file.response.url) {
30
+    //     this.setState({
31
+    //       imageUrl: info.file.response.thumbUrl,
32
+    //     });
29 33
 
30
-        if (typeof this.props.onChange === 'function') {
31
-          this.props.onChange(info.file.response.url);
32
-        }
33
-      }
34
-    }
34
+    //     if (typeof this.props.onChange === 'function') {
35
+    //       this.props.onChange(info.file.response.url);
36
+    //     }
37
+    //   }
38
+    // }
35 39
   };
36 40
 
41
+  handleUploadSucess = (url) => {
42
+    this.setState({ loading: false });
43
+    if (typeof this.props.onChange === 'function') {
44
+      this.props.onChange(url);
45
+    }
46
+  }
47
+
37 48
   render() {
38 49
     const uploadButton = (
39 50
       <div>
@@ -50,8 +61,8 @@ class ImageUpload extends React.Component {
50 61
         showUploadList={false}
51 62
         beforeUpload={this.props.beforeUpload}
52 63
         onChange={this.handleChange}
53
-
54 64
         {...uploaderProps}
65
+        onSuccess={this.handleUploadSucess}
55 66
       >
56 67
         {(this.state.imageUrl || value) ? (
57 68
           <img src={this.state.imageUrl || value} alt="avatar" style={{ width: "100%" }} />

+ 6
- 2
src/services/apis.js View File

@@ -2,10 +2,14 @@ const prefix = '/api/admin'
2 2
 
3 3
 export default {
4 4
   image: {
5
-    upload: {
5
+    uploadForAnt: {
6 6
       url: `${prefix}/antd/image`,
7 7
       method: 'POST',
8
-    }
8
+    },
9
+    upload: {
10
+      url: `${prefix}/image`,
11
+      method: 'POST',
12
+    },
9 13
   },
10 14
   user: {
11 15
     current: {

+ 33
- 5
src/utils/upload.js View File

@@ -1,11 +1,39 @@
1
-import apis from '../services/apis';
1
+import { fetch, apis } from './request';
2
+import mixStr from './mixStr';
3
+
4
+const getToken = () => mixStr(window.localStorage.getItem('test-foobar'))
5
+
6
+const uploadImage = fetch(apis.image.upload)
2 7
 
3 8
 const uploaderProps = {
4 9
   name: 'file',
5
-  action: apis.image.upload.url,
6
-  headers: {
7
-    Authorization: `Bearer ${window.localStorage.getItem('x-token')}`
8
-  }
10
+  // action: apis.image.uploadForAnt.url,
11
+  accept: '.png, .jpg, .jpeg, .gif',
12
+  // headers: {
13
+  //   Authorization: `Bearer ${getToken()}`
14
+  // },
15
+  customRequest({
16
+    action,
17
+    file,
18
+    headers,
19
+    onError,
20
+    onProgress,
21
+    onSuccess,
22
+    withCredentials,
23
+  }) {
24
+    const data = new FormData()
25
+    data.append('file', file)
26
+
27
+    uploadImage({ data }).then((img) => {
28
+      onSuccess(img, file);
29
+    }).catch(onError);
30
+
31
+    return {
32
+      abort() {
33
+        console.log('upload progress is aborted.');
34
+      },
35
+    };
36
+  },
9 37
 }
10 38
 
11 39
 export { uploaderProps }