weiximei 5 years ago
parent
commit
34f604c19c

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

2
 import { Upload, Button, Icon } from 'antd';
2
 import { Upload, Button, Icon } from 'antd';
3
 import { uploaderProps } from '../../utils/upload';
3
 import { uploaderProps } from '../../utils/upload';
4
 
4
 
5
+/**
6
+ * value 数据的接收格式 [{ url: xxxxx.mp4 }]
7
+ * @param {*} props 
8
+ */
5
 function fileUpload(props) {
9
 function fileUpload(props) {
6
 
10
 
7
   const { value } = props
11
   const { value } = props
16
 
20
 
17
 
21
 
18
   function getFileList() {
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
   function setDefaultValue() {
28
   function setDefaultValue() {
28
   }
34
   }
29
 
35
 
30
   function onFileChange({ file, fileList }) {
36
   function onFileChange({ file, fileList }) {
37
+    console.log(file, fileList)
31
     setDefaultFileList(fileList)
38
     setDefaultFileList(fileList)
32
     if (file.status === 'uploading') {
39
     if (file.status === 'uploading') {
33
       return
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
 
45
 
46
   const [poi, setPoi] = useState([])
46
   const [poi, setPoi] = useState([])
47
 
47
 
48
+  // 视频上传按钮是否 禁用
49
+  const [fileUploadDisabled, setFileUploadDisabled] = useState(false)
50
+
48
   // console.log('props.building: ', props.building)
51
   // console.log('props.building: ', props.building)
49
   if (props.building.buildingId !== undefined) {
52
   if (props.building.buildingId !== undefined) {
50
     const { buildingId } = props.building
53
     const { buildingId } = props.building
75
       res.mapCoordinate = res.coordinate
78
       res.mapCoordinate = res.coordinate
76
       if (res.videoUrl) {
79
       if (res.videoUrl) {
77
         res.videoUrl = [].concat(res.videoUrl)
80
         res.videoUrl = [].concat(res.videoUrl)
81
+        // 视频上传按钮禁用
82
+        setFileUploadDisabled(true)
78
       }
83
       }
79
       setPoi(res.mapJson || [])
84
       setPoi(res.mapJson || [])
80
       props.form.setFieldsValue(res)
85
       props.form.setFieldsValue(res)
101
     // 列表图
106
     // 列表图
102
     data.listImg = data.listImage && data.listImage.map((item, index) => ({ imgType: 'list', url: item, orderNo: index + 1 }))
107
     data.listImg = data.listImage && data.listImage.map((item, index) => ({ imgType: 'list', url: item, orderNo: index + 1 }))
103
     if (data.videoUrl) {
108
     if (data.videoUrl) {
109
+      console.log(data.videoUrl[0])
104
       data.videoUrl = data.videoUrl[0]
110
       data.videoUrl = data.videoUrl[0]
105
     }
111
     }
106
     
112
     
120
 
126
 
121
   // 视频文件上传前 回调
127
   // 视频文件上传前 回调
122
   function fileUploadBeforeUpload(file, fileList) {
128
   function fileUploadBeforeUpload(file, fileList) {
123
-    console.log(file, fileList)
129
+    // console.log('视频文件上传前 回调: ', file, fileList)
124
     return new Promise((resolve, reject) => {
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
           resolve(file)
136
           resolve(file)
127
       } else {
137
       } else {
128
         openNotificationWithIcon('error', '项目视频,仅支持MP4格式')
138
         openNotificationWithIcon('error', '项目视频,仅支持MP4格式')
271
           </Form.Item>
281
           </Form.Item>
272
           <Form.Item label="项目视频" help="视频仅支持mp4格式,建议尺寸:1920*1080,比例16:9">
282
           <Form.Item label="项目视频" help="视频仅支持mp4格式,建议尺寸:1920*1080,比例16:9">
273
             {getFieldDecorator('videoUrl')(
283
             {getFieldDecorator('videoUrl')(
274
-              <FileUpload accept=".mp4" beforeUpload={fileUploadBeforeUpload} label="上传视频"/>,
284
+              // disabled={fileUploadDisabled}
285
+              <FileUpload accept=".mp4" beforeUpload={fileUploadBeforeUpload} label="上传视频" />,
275
             )}
286
             )}
276
           </Form.Item>
287
           </Form.Item>
277
           <Form.Item label="楼盘主图" help="建议图片尺寸:750*600px,比例5:4,格式:jpg,用于楼盘详情">
288
           <Form.Item label="楼盘主图" help="建议图片尺寸:750*600px,比例5:4,格式:jpg,用于楼盘详情">