From 39a7397dfcfac3ac5595725bcb4b6f6c230f3f77 Mon Sep 17 00:00:00 2001 From: liaoxuezhi <2betop.cn@gmail.com> Date: Tue, 30 Aug 2022 12:49:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A4=A7=E6=96=87=E4=BB=B6=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E5=88=86=E5=9D=97=E6=94=AF=E6=8C=81=E9=87=8D=E8=AF=95?= =?UTF-8?q?=20(#5258)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mock/upload/{chunkApi.json => chunk.json} | 0 .../{finishChunkApi.json => finishChunk.json} | 0 .../{startChunkApi.json => startChunk.json} | 0 .../amis/src/renderers/Form/InputFile.tsx | 92 +++++++++++++------ 4 files changed, 62 insertions(+), 30 deletions(-) rename mock/cfc/mock/upload/{chunkApi.json => chunk.json} (100%) rename mock/cfc/mock/upload/{finishChunkApi.json => finishChunk.json} (100%) rename mock/cfc/mock/upload/{startChunkApi.json => startChunk.json} (100%) diff --git a/mock/cfc/mock/upload/chunkApi.json b/mock/cfc/mock/upload/chunk.json similarity index 100% rename from mock/cfc/mock/upload/chunkApi.json rename to mock/cfc/mock/upload/chunk.json diff --git a/mock/cfc/mock/upload/finishChunkApi.json b/mock/cfc/mock/upload/finishChunk.json similarity index 100% rename from mock/cfc/mock/upload/finishChunkApi.json rename to mock/cfc/mock/upload/finishChunk.json diff --git a/mock/cfc/mock/upload/startChunkApi.json b/mock/cfc/mock/upload/startChunk.json similarity index 100% rename from mock/cfc/mock/upload/startChunkApi.json rename to mock/cfc/mock/upload/startChunk.json diff --git a/packages/amis/src/renderers/Form/InputFile.tsx b/packages/amis/src/renderers/Form/InputFile.tsx index 2f92d5623..75d263043 100644 --- a/packages/amis/src/renderers/Form/InputFile.tsx +++ b/packages/amis/src/renderers/Form/InputFile.tsx @@ -5,7 +5,13 @@ import isPlainObject from 'lodash/isPlainObject'; import ImageControl from './InputImage'; import {Payload, ApiObject, ApiString, ActionObject} from 'amis-core'; import {qsstringify, createObject, guid, isEmpty} from 'amis-core'; -import {buildApi, isEffectiveApi, normalizeApi, isApiOutdated, isApiOutdatedWithData} from 'amis-core'; +import { + buildApi, + isEffectiveApi, + normalizeApi, + isApiOutdated, + isApiOutdatedWithData +} from 'amis-core'; import {Icon} from 'amis-ui'; import {TooltipWrapper, Button} from 'amis-ui'; import DropZone from 'react-dropzone'; @@ -1148,8 +1154,13 @@ export default class FileControl extends React.Component { fd.append(config.fieldName || 'file', blob, file.name); return self - ._send(file, api, fd, {}, progress => - updateProgress(task.partNumber, progress) + ._send( + file, + api, + fd, + {}, + progress => updateProgress(task.partNumber, progress), + 3 ) .then(ret => { state.loaded++; @@ -1188,35 +1199,57 @@ export default class FileControl extends React.Component { }); } - _send( + async _send( file: FileX, api: ApiObject | ApiString, data?: any, options?: object, - onProgress?: (progress: number) => void + onProgress?: (progress: number) => void, + maxRetryLimit = 0 ): Promise { const env = this.props.env; + const __ = this.props.translate; if (!env || !env.fetcher) { throw new Error('fetcher is required'); } - return env.fetcher(api, data, { - method: 'post', - ...options, - withCredentials: true, - cancelExecutor: (cancelExecutor: () => void) => { - // 记录取消器,取消的时候要调用 - this.fileUploadCancelExecutors.push({ - file: file, - executor: cancelExecutor - }); - }, - onUploadProgress: onProgress - ? (event: {loaded: number; total: number}) => - onProgress(event.loaded / event.total) - : undefined - }); + try { + const result = await env.fetcher(api, data, { + method: 'post', + ...options, + withCredentials: true, + cancelExecutor: (cancelExecutor: () => void) => { + // 记录取消器,取消的时候要调用 + this.fileUploadCancelExecutors.push({ + file: file, + executor: cancelExecutor + }); + }, + onUploadProgress: onProgress + ? (event: {loaded: number; total: number}) => + onProgress(event.loaded / event.total) + : undefined + }); + + if (!result.ok) { + throw new Error(result.msg || __('File.errorRetry')); + } + + return result; + } catch (error) { + if (maxRetryLimit > 0) { + return this._send( + file, + api, + data, + options, + onProgress, + maxRetryLimit - 1 + ); + } + throw error; + } } removeFileCanelExecutor(file: any, execute = false) { @@ -1542,19 +1575,18 @@ export default class FileControl extends React.Component { sizeMutable: false, renderDescription: false, shouldComponentUpdate: (props: any, prevProps: any) => - !!isEffectiveApi(props.receiver, props.data) && ( - isApiOutdated( - props.receiver, - prevProps.receiver, - props.data, - prevProps.data - ) || + !!isEffectiveApi(props.receiver, props.data) && + (isApiOutdated( + props.receiver, + prevProps.receiver, + props.data, + prevProps.data + ) || isApiOutdatedWithData( props.receiver, prevProps.receiver, props.data, prevProps.data - ) - ) + )) }) export class FileControlRenderer extends FileControl {}