|
@@ -7,14 +7,14 @@ import { Button, message, Modal, Upload } from 'antd'
|
|
|
import { ModalProps } from 'antd/lib/modal'
|
|
|
import { UploadChangeParam } from 'antd/lib/upload'
|
|
|
import { UploadFile } from 'antd/lib/upload/interface'
|
|
|
-import React, { useEffect, useState } from 'react'
|
|
|
+import React, { ReactElement, useEffect, useState } from 'react'
|
|
|
const { Dragger } = Upload
|
|
|
|
|
|
interface iUploadModalProps extends ModalProps {
|
|
|
onCreate: (files: iFile[]) => void
|
|
|
onShow: (visible: boolean) => void
|
|
|
}
|
|
|
-const UploadModal:React.FC<iUploadModalProps> = (props) => {
|
|
|
+const UploadModal: React.FC<iUploadModalProps> = (props) => {
|
|
|
const { onCreate, onShow, ...otherProps } = props
|
|
|
const [ files, setFiles ] = useState<iFile[]>([])
|
|
|
const [ fileList, setFileList ] = useState<Array<UploadFile>>([])
|
|
@@ -36,23 +36,18 @@ const UploadModal:React.FC<iUploadModalProps> = (props) => {
|
|
|
}, [ props.visible ])
|
|
|
// 上传文件改变时的状态
|
|
|
const onChange = (info: UploadChangeParam) => {
|
|
|
- // console.log(info)
|
|
|
+
|
|
|
+ console.log(info)
|
|
|
|
|
|
const { status } = info.file
|
|
|
- // if (status !== 'uploading') {
|
|
|
- // }
|
|
|
if (status === 'done') {
|
|
|
- // console.log(fileList)
|
|
|
|
|
|
- // const newFileList = [ ...fileList, info.file ]
|
|
|
- // console.log(fileList)
|
|
|
- // newFileList.push(info.file)
|
|
|
- setFileList(info.fileList.map(item => ({ ...item, url: consts.OSS_PATH.REVIEW + item.url })))
|
|
|
- // const newFiles = [ ...files, { createTime: new Date(), filepath: info.file.url,filename: info.file.name } ]
|
|
|
- setFiles(info.fileList.map(item => ({ createTime: new Date(), filepath: item.url,filename: item.name })))
|
|
|
+ setFiles(info.fileList.map(item => ({ createTime: new Date(), filepath: item.url, filename: item.name })))
|
|
|
} else if (status === 'error') {
|
|
|
message.error(`${info.file.name} 上传失败`)
|
|
|
}
|
|
|
+ setFileList(info.fileList)
|
|
|
+
|
|
|
}
|
|
|
// 初始化、获取签名
|
|
|
const initOssData = async () => {
|
|
@@ -79,29 +74,27 @@ const UploadModal:React.FC<iUploadModalProps> = (props) => {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- const transformFile = (file: any) => {
|
|
|
- file.url = OSSData.dir + file.name
|
|
|
- return file
|
|
|
- }
|
|
|
// 上传前的回调
|
|
|
- const beforeUpload = (file: any) => {
|
|
|
+ const beforeUpload = async (file: any) => {
|
|
|
const { UPLOAD_LIMIT } = consts
|
|
|
const isLt30M = file.size / 1024 / 1024 < UPLOAD_LIMIT
|
|
|
if (!isLt30M) {
|
|
|
message.error("上传附件大小限制在30MB")
|
|
|
- return false
|
|
|
+ return Promise.reject()
|
|
|
}
|
|
|
const expire = parseInt(OSSData.expire) * 1000
|
|
|
if (expire < Date.now()) {
|
|
|
initOssData()
|
|
|
- return false
|
|
|
+ return Promise.reject()
|
|
|
}
|
|
|
|
|
|
const reg = new RegExp(consts.UPLOAD_WHITE)
|
|
|
if (!reg.test(file.name)) {
|
|
|
- return false
|
|
|
+ return Promise.reject()
|
|
|
}
|
|
|
- return true
|
|
|
+
|
|
|
+ file.url = OSSData.dir + file.name
|
|
|
+ return Promise.resolve(file)
|
|
|
}
|
|
|
|
|
|
// 移除文件
|
|
@@ -112,6 +105,12 @@ const UploadModal:React.FC<iUploadModalProps> = (props) => {
|
|
|
setFileList(newFileList)
|
|
|
setFiles(newFiles)
|
|
|
}
|
|
|
+
|
|
|
+ // const customRenderItem = (originNode: ReactElement, file: UploadFile, fileList?: object[]): React.ReactNode => {
|
|
|
+ // console.log(originNode)
|
|
|
+
|
|
|
+ // return originNode
|
|
|
+ // }
|
|
|
return (
|
|
|
<Modal
|
|
|
{...otherProps}
|
|
@@ -122,7 +121,7 @@ const UploadModal:React.FC<iUploadModalProps> = (props) => {
|
|
|
<ZhCloseButton onClick={() => onShow(false)} size="small" className="pi-mg-right-5">关闭</ZhCloseButton>
|
|
|
<Button type="primary" size="small" onClick={() => onCreate(files)}>确认</Button>
|
|
|
</div>}
|
|
|
- >
|
|
|
+ >
|
|
|
<Dragger
|
|
|
name="file"
|
|
|
fileList={fileList}
|
|
@@ -131,12 +130,12 @@ const UploadModal:React.FC<iUploadModalProps> = (props) => {
|
|
|
onChange={onChange}
|
|
|
beforeUpload={beforeUpload}
|
|
|
data={getExtraData}
|
|
|
- transformFile={transformFile}
|
|
|
+ // itemRender={customRenderItem}
|
|
|
// accept={consts.UPLOAD_WHITE}
|
|
|
onRemove={onRemove}
|
|
|
- >
|
|
|
+ >
|
|
|
<p className="ant-upload-drag-icon">
|
|
|
- <InboxOutlined />
|
|
|
+ <InboxOutlined />
|
|
|
</p>
|
|
|
<p className="ant-upload-text">把文件拖入指定区域,完成上传,同样支持点击上传。</p>
|
|
|
<p className="ant-upload-hint">
|