Commit 0516c083 authored by zhangwenshuai's avatar zhangwenshuai

fix upload

parent 2c82bb10
...@@ -11,10 +11,8 @@ message.config({ ...@@ -11,10 +11,8 @@ message.config({
}); });
const UploadCom = (props) => { const UploadCom = (props) => {
const { const { onChange, CDN_HOST, getFormat, setFormat, data, domain } = props;
onChange, CDN_HOST, getFormat, setFormat, data, domain, const selfProps = antiAssign(props, ['onChange', 'CDN_HOST', 'data']);
} = props;
const selfProps = antiAssign(props, ['onChange', 'CDN_HOST']);
const getFormatFileList = (fileList = []) => { const getFormatFileList = (fileList = []) => {
if (!fileList) { if (!fileList) {
return []; return [];
...@@ -39,8 +37,8 @@ const UploadCom = (props) => { ...@@ -39,8 +37,8 @@ const UploadCom = (props) => {
if (typeof setFormat === 'function') { if (typeof setFormat === 'function') {
return setFormat(fileList); return setFormat(fileList);
} }
const data = file.response || {}; const resData = file.response || {};
const url = `${domain ? `https://${domain}` : CDN_HOST}/${data.key}`; const url = `${domain ? `https://${domain}` : CDN_HOST}/${resData.key}`;
return { return {
uid: file.uid, uid: file.uid,
name: file.name, name: file.name,
...@@ -55,19 +53,26 @@ const UploadCom = (props) => { ...@@ -55,19 +53,26 @@ const UploadCom = (props) => {
}, [props.value]); }, [props.value]);
const previewModel = useRef<any>(); const previewModel = useRef<any>();
// 自定义七牛文件唯一key // 自定义七牛文件唯一key
const getKey = (file:any) => { const getKey = (file: any) => {
if (!file) return; if (!file) return;
const suffix = file.name.match(/\.\w+$/)[0]; const suffix = file.name.match(/\.\w+$/)[0];
const rand6 = getRandom(); const rand6 = getRandom();
const time = moment().format('YYYYMMDDHHmmss'); const time = moment().format('YYYYMMDDHHmmss');
return time + rand6 + suffix; return time + rand6 + suffix;
}; };
const [extraData, setExtraData] = useState({});
// 七牛上传额外数据,token和key // 七牛上传额外数据,token和key
const getData = async (file:any) => { const getToken = async () => {
let extraData = data || {}; let extData = data || {};
if (typeof data === 'function') { 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 { return {
...extraData, ...extraData,
key: getKey(file), key: getKey(file),
...@@ -107,20 +112,27 @@ const UploadCom = (props) => { ...@@ -107,20 +112,27 @@ const UploadCom = (props) => {
} }
}; };
const onRemove = (file) => { 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); onSaveFileList(newFileList);
}; };
const beforeUpload = async () => {
await getToken();
return true;
};
return ( return (
<div className={s.container}> <div className={s.container}>
<Upload <Upload
className={s.upload} className={s.upload}
listType="picture" listType="picture"
action="https://upload-z1.qiniup.com/" // 七牛上传地址 action="https://upload-z1.qiniup.com/" // 七牛上传地址
data={getData}
multiple multiple
fileList={fileList} fileList={fileList}
{...selfProps} {...selfProps}
data={getData}
beforeUpload={beforeUpload}
onChange={changeFileList} onChange={changeFileList}
onRemove={onRemove} onRemove={onRemove}
previewFile={previewFile} previewFile={previewFile}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment