From f7f8e12ac9edea4a34ad7c1647ba29d320ff7498 Mon Sep 17 00:00:00 2001 From: chenos Date: Thu, 31 Dec 2020 14:12:25 +0800 Subject: [PATCH] fix: multiple files upload bugs --- packages/app/src/api/collections/authors.ts | 4 +- packages/app/src/api/collections/books.ts | 9 ++++ .../components/form.fields/upload/index.tsx | 47 +++++++++++++++---- 3 files changed, 50 insertions(+), 10 deletions(-) diff --git a/packages/app/src/api/collections/authors.ts b/packages/app/src/api/collections/authors.ts index cfb41205d3..a568a3726f 100644 --- a/packages/app/src/api/collections/authors.ts +++ b/packages/app/src/api/collections/authors.ts @@ -31,8 +31,8 @@ export default { }, }, { - interface: 'number', - title: '年龄', + interface: 'attachment', + title: '头像', component: { showInTable: true, showInDetail: true, diff --git a/packages/app/src/api/collections/books.ts b/packages/app/src/api/collections/books.ts index 4fe96f1314..5f651e94b8 100644 --- a/packages/app/src/api/collections/books.ts +++ b/packages/app/src/api/collections/books.ts @@ -17,6 +17,15 @@ export default { showInForm: true, }, }, + { + interface: 'attachment', + title: '封面', + component: { + showInTable: true, + showInDetail: true, + showInForm: true, + }, + }, // { // interface: 'linkTo', // title: '作者', diff --git a/packages/app/src/components/form.fields/upload/index.tsx b/packages/app/src/components/form.fields/upload/index.tsx index d62596f7de..e4e55dd819 100644 --- a/packages/app/src/components/form.fields/upload/index.tsx +++ b/packages/app/src/components/form.fields/upload/index.tsx @@ -107,7 +107,7 @@ export const getImageByUrl = (url, options) => { function toFileObject(item) { console.log(item); - if (typeof item !== 'object') { + if (typeof item === 'number') { return { id: item, } @@ -118,6 +118,7 @@ function toFileObject(item) { if (item.response && item.response.data) { return toFileObject(item.response.data); } + // if (item.org) return { id: item.id, uid: `${item.id}`, @@ -137,6 +138,39 @@ function toFileList(value: any) { return value.map(toFileObject); } +function toValue(item) { + if (typeof item === 'number') { + return { + id: item, + } + } + if (item.id && item.uid && item.url) { + return item; + } + if (item.response && item.response.data) { + return toValue(item.response.data); + } + if (item.originFileObj) { + return item; + } + return { + id: item.id, + uid: `${item.id}`, + name: item.title, + url: item.url, + }; +} + +function toValues(fileList) { + if (!fileList) { + return []; + } + if (!Array.isArray(fileList) && typeof fileList === 'object') { + fileList = [fileList]; + } + return fileList.map(toValue); +} + export const Upload = connect({ getProps: mapStyledProps })((props) => { @@ -145,13 +179,10 @@ export const Upload = connect({ const uploadProps = { name: 'file', action: `${process.env.API}/attachments:upload`, - onChange({ file, fileList }) { - if (['done', 'removed'].indexOf(file.status) !== -1) { - const values = toFileList(fileList); - console.log(values); - setFileList(values); - onChange(values.map(item => item.id)); - } + onChange({ fileList }) { + console.log(fileList); + setFileList((fileList)); + onChange(toValues(fileList)); }, }; return (