From 0516c083adb2e46c79ffa7df787c2138a85047af Mon Sep 17 00:00:00 2001 From: zhangwenshuai Date: Tue, 2 Jun 2020 22:35:07 +0800 Subject: [PATCH] fix upload --- .../component/base/extra/upload/index.tsx | 36 ++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/components/apolloTable/component/base/extra/upload/index.tsx b/components/apolloTable/component/base/extra/upload/index.tsx index 34564c1..b8957e8 100644 --- a/components/apolloTable/component/base/extra/upload/index.tsx +++ b/components/apolloTable/component/base/extra/upload/index.tsx @@ -11,10 +11,8 @@ message.config({ }); const UploadCom = (props) => { - const { - onChange, CDN_HOST, getFormat, setFormat, data, domain, - } = props; - const selfProps = antiAssign(props, ['onChange', 'CDN_HOST']); + const { onChange, CDN_HOST, getFormat, setFormat, data, domain } = props; + const selfProps = antiAssign(props, ['onChange', 'CDN_HOST', 'data']); const getFormatFileList = (fileList = []) => { if (!fileList) { return []; @@ -39,8 +37,8 @@ const UploadCom = (props) => { if (typeof setFormat === 'function') { return setFormat(fileList); } - const data = file.response || {}; - const url = `${domain ? `https://${domain}` : CDN_HOST}/${data.key}`; + const resData = file.response || {}; + const url = `${domain ? `https://${domain}` : CDN_HOST}/${resData.key}`; return { uid: file.uid, name: file.name, @@ -55,19 +53,26 @@ const UploadCom = (props) => { }, [props.value]); const previewModel = useRef(); // 自定义七牛文件唯一key - const getKey = (file:any) => { + const getKey = (file: any) => { if (!file) return; const suffix = file.name.match(/\.\w+$/)[0]; const rand6 = getRandom(); const time = moment().format('YYYYMMDDHHmmss'); return time + rand6 + suffix; }; + const [extraData, setExtraData] = useState({}); // 七牛上传额外数据,token和key - const getData = async (file:any) => { - let extraData = data || {}; + const getToken = async () => { + let extData = data || {}; if (typeof data === 'function') { - extraData = await data(); + const res = await data(); + if (res && res.success) { + extData = res.data; + } } + setExtraData(extData); + }; + const getData = (file: any) => { return { ...extraData, key: getKey(file), @@ -107,20 +112,27 @@ const UploadCom = (props) => { } }; const onRemove = (file) => { - const newFileList = fileList.filter((item) => { return item.uid !== file.uid; }); + const newFileList = fileList.filter((item) => { + return item.uid !== file.uid; + }); onSaveFileList(newFileList); }; + const beforeUpload = async () => { + await getToken(); + return true; + }; return (