Commit 0516c083 authored by zhangwenshuai's avatar zhangwenshuai

fix upload

parent 2c82bb10
......@@ -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<any>();
// 自定义七牛文件唯一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 (
<div className={s.container}>
<Upload
className={s.upload}
listType="picture"
action="https://upload-z1.qiniup.com/" // 七牛上传地址
data={getData}
multiple
fileList={fileList}
{...selfProps}
data={getData}
beforeUpload={beforeUpload}
onChange={changeFileList}
onRemove={onRemove}
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