张延森 5 years ago
parent
commit
7d8a1a9fea

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

3
 import './style.less';
3
 import './style.less';
4
 import { uploaderProps } from '../../utils/upload';
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
 class ImageListUpload extends React.Component {
6
 class ImageListUpload extends React.Component {
16
   state = {
7
   state = {
17
     previewVisible: false,
8
     previewVisible: false,
18
     previewImage: '',
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
   handleCancel = () => this.setState({ previewVisible: false });
17
   handleCancel = () => this.setState({ previewVisible: false });
23
 
18
 
24
   handlePreview = async file => {
19
   handlePreview = async file => {
25
-    if (!file.url && !file.preview) {
26
-      file.preview = await getBase64(file.originFileObj);
27
-    }
28
-
29
     this.setState({
20
     this.setState({
30
-      previewImage: file.url || file.preview,
21
+      previewImage: file.url ,
31
       previewVisible: true,
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
     const uploadButton = (
63
     const uploadButton = (
50
       <div>
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
       </div>
66
       </div>
54
     );
67
     );
55
     return (
68
     return (
56
       <div className="clearfix">
69
       <div className="clearfix">
57
         <Upload
70
         <Upload
58
           listType="picture-card"
71
           listType="picture-card"
59
-          fileList={fileList || value}
72
+          fileList={fileList}
60
           onPreview={this.handlePreview}
73
           onPreview={this.handlePreview}
61
           onChange={this.handleChange}
74
           onChange={this.handleChange}
62
           { ...uploaderProps }
75
           { ...uploaderProps }
76
+          onSuccess={this.handleUploadSucess}
63
         >
77
         >
64
           {fileList.length >= 8 ? null : uploadButton}
78
           {fileList.length >= 8 ? null : uploadButton}
65
         </Upload>
79
         </Upload>

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

16
       this.setState({ loading: true });
16
       this.setState({ loading: true });
17
       return;
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
   render() {
48
   render() {
38
     const uploadButton = (
49
     const uploadButton = (
39
       <div>
50
       <div>
50
         showUploadList={false}
61
         showUploadList={false}
51
         beforeUpload={this.props.beforeUpload}
62
         beforeUpload={this.props.beforeUpload}
52
         onChange={this.handleChange}
63
         onChange={this.handleChange}
53
-
54
         {...uploaderProps}
64
         {...uploaderProps}
65
+        onSuccess={this.handleUploadSucess}
55
       >
66
       >
56
         {(this.state.imageUrl || value) ? (
67
         {(this.state.imageUrl || value) ? (
57
           <img src={this.state.imageUrl || value} alt="avatar" style={{ width: "100%" }} />
68
           <img src={this.state.imageUrl || value} alt="avatar" style={{ width: "100%" }} />

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

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

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

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
 const uploaderProps = {
8
 const uploaderProps = {
4
   name: 'file',
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
 export { uploaderProps }
39
 export { uploaderProps }