weiximei 5 years ago
parent
commit
34f604c19c

+ 19
- 3
src/components/XForm/FileUpload.jsx View File

@@ -2,6 +2,10 @@ import React, { useState, useEffect } from 'react'
2 2
 import { Upload, Button, Icon } from 'antd';
3 3
 import { uploaderProps } from '../../utils/upload';
4 4
 
5
+/**
6
+ * value 数据的接收格式 [{ url: xxxxx.mp4 }]
7
+ * @param {*} props 
8
+ */
5 9
 function fileUpload(props) {
6 10
 
7 11
   const { value } = props
@@ -16,7 +20,9 @@ function fileUpload(props) {
16 20
 
17 21
 
18 22
   function getFileList() {
19
-    return (value || []).map((img, inx) => ({ uid: inx, url: img, name: img.substring(img.lastIndexOf('/') + 1, img.length), status: 'done' }))
23
+    console.log('fileUpload: ', value)
24
+    // value 数据的接收格式 [{ url: xxxxx.mp4 }, { url: xxxxx.jpg }]
25
+    return (value || []).filter(f => f !== undefined).map((img, inx) => ({ uid: inx, url: img, name: img.substring(img.lastIndexOf('/') + 1, img.length), status: 'done' }))
20 26
   }
21 27
 
22 28
   function setDefaultValue() {
@@ -28,13 +34,23 @@ function fileUpload(props) {
28 34
   }
29 35
 
30 36
   function onFileChange({ file, fileList }) {
37
+    console.log(file, fileList)
31 38
     setDefaultFileList(fileList)
32 39
     if (file.status === 'uploading') {
33 40
       return
34 41
     }
35 42
 
36
-    if (file.status === 'done') {
37
-      props.onChange([].concat(file.response))
43
+    if (file.status === 'done' || file.status === 'removed') {
44
+
45
+      // 因为这个控件本身返回的数据格式 [{ ..., response: '服务器返回的数据' }]
46
+      // 但是 我这里自己用的时候 传入的数据是 [ 'xxxx.mp4', 'xxxx.jpg' ], 然后通过 getFileList() 转换成了 [{ url: 'xxx.mp4' }] 这样的格式
47
+
48
+      // 原因是因为 控件返回的数据 和 fileList 展示已经上传的数据 的格式字段不一样
49
+
50
+      const resFileList = fileList.filter(f => f.response !== undefined).map(i => i.response)
51
+      const tempFileList = fileList.filter(f => f.url !== undefined).map(i => i.url)
52
+      const resultList = tempFileList.concat(resFileList || [])
53
+      props.onChange(resultList)
38 54
     }
39 55
   }
40 56
 

+ 14
- 3
src/pages/building/list/add/components/base.jsx View File

@@ -45,6 +45,9 @@ function AddBuilding(props) {
45 45
 
46 46
   const [poi, setPoi] = useState([])
47 47
 
48
+  // 视频上传按钮是否 禁用
49
+  const [fileUploadDisabled, setFileUploadDisabled] = useState(false)
50
+
48 51
   // console.log('props.building: ', props.building)
49 52
   if (props.building.buildingId !== undefined) {
50 53
     const { buildingId } = props.building
@@ -75,6 +78,8 @@ function AddBuilding(props) {
75 78
       res.mapCoordinate = res.coordinate
76 79
       if (res.videoUrl) {
77 80
         res.videoUrl = [].concat(res.videoUrl)
81
+        // 视频上传按钮禁用
82
+        setFileUploadDisabled(true)
78 83
       }
79 84
       setPoi(res.mapJson || [])
80 85
       props.form.setFieldsValue(res)
@@ -101,6 +106,7 @@ function AddBuilding(props) {
101 106
     // 列表图
102 107
     data.listImg = data.listImage && data.listImage.map((item, index) => ({ imgType: 'list', url: item, orderNo: index + 1 }))
103 108
     if (data.videoUrl) {
109
+      console.log(data.videoUrl[0])
104 110
       data.videoUrl = data.videoUrl[0]
105 111
     }
106 112
     
@@ -120,9 +126,13 @@ function AddBuilding(props) {
120 126
 
121 127
   // 视频文件上传前 回调
122 128
   function fileUploadBeforeUpload(file, fileList) {
123
-    console.log(file, fileList)
129
+    // console.log('视频文件上传前 回调: ', file, fileList)
124 130
     return new Promise((resolve, reject) => {
125
-      if (file.type === 'video/mp4' || file.type === '.mp4') {
131
+      if (fileUploadDisabled) {
132
+        openNotificationWithIcon('error', '项目视频,只允许上传一个')
133
+        reject()
134
+      } else if (file.type === 'video/mp4' || file.type === '.mp4') {
135
+          setFileUploadDisabled(true)
126 136
           resolve(file)
127 137
       } else {
128 138
         openNotificationWithIcon('error', '项目视频,仅支持MP4格式')
@@ -271,7 +281,8 @@ function AddBuilding(props) {
271 281
           </Form.Item>
272 282
           <Form.Item label="项目视频" help="视频仅支持mp4格式,建议尺寸:1920*1080,比例16:9">
273 283
             {getFieldDecorator('videoUrl')(
274
-              <FileUpload accept=".mp4" beforeUpload={fileUploadBeforeUpload} label="上传视频"/>,
284
+              // disabled={fileUploadDisabled}
285
+              <FileUpload accept=".mp4" beforeUpload={fileUploadBeforeUpload} label="上传视频" />,
275 286
             )}
276 287
           </Form.Item>
277 288
           <Form.Item label="楼盘主图" help="建议图片尺寸:750*600px,比例5:4,格式:jpg,用于楼盘详情">