@@ -259,7 +259,13 @@ export default {
259259 default () {
260260 return Promise .resolve ()
261261 }
262- }
262+ },
263+
264+ /**
265+ * 自定义上传, 使用此函数则不采用默认 AliOSS 上传行为
266+ * 返回 Promise, 接收 resolve 参数为 url
267+ */
268+ httpRequest: Function
263269 },
264270 data () {
265271 return {
@@ -284,18 +290,6 @@ export default {
284290 }
285291 },
286292 mounted () {
287- if (
288- ! this .region ||
289- ! this .bucket ||
290- ! this .accessKeyId ||
291- ! this .accessKeySecret
292- ) {
293- console .error (
294- ' 必要参数不能为空: region bucket accessKeyId accessKeySecret'
295- )
296- return
297- }
298-
299293 if (this .accept && ! mimeTypeFullRegex .test (this .accept )) {
300294 console .warn (
301295 ' 请设置正确的`accept`属性, 可参考:' ,
@@ -307,6 +301,20 @@ export default {
307301 },
308302 methods: {
309303 newClient () {
304+ if (this .httpRequest ) return
305+
306+ if (
307+ ! this .region ||
308+ ! this .bucket ||
309+ ! this .accessKeyId ||
310+ ! this .accessKeySecret
311+ ) {
312+ console .error (
313+ ' 必要参数不能为空: region bucket accessKeyId accessKeySecret'
314+ )
315+ return
316+ }
317+
310318 // https://help.aliyun.com/document_detail/32069.html?spm=a2c4g.11186623.6.801.LllSVA
311319 this .client = new AliOSS ({
312320 region: this .region ,
@@ -401,55 +409,63 @@ export default {
401409
402410 key = ` ${ name .substring (0 , pos)} -${ new Date ().getTime ()}${ suffix} `
403411
404- await this .client
405- .multipartUpload (this .dir + key, file, this .uploadOptions )
406- .then (res => {
407- // 协议无关
408- let url = doubleSlash
409-
410- // 上传时阿里 OSS 会对文件名 encode,但 res.name 没有 encode
411- // 因此要 encode res.name,否则会因为文件名不同,导致 404
412- const filename = encodePath (res .name )
413-
414- if (this .customDomain ) {
415- if (this .customDomain .indexOf (doubleSlash) > - 1 )
416- url = ` ${ this .customDomain } /${ filename} `
417- else {
418- url += ` ${ this .customDomain } /${ filename} `
419- }
412+ if (this .httpRequest ) {
413+ try {
414+ const url = await this .httpRequest (file)
415+ if (typeof url === ' string' && / ^ (https? :)? \/\/ / .test (url)) {
416+ this .$emit (
417+ ' input' ,
418+ this .multiple ? this .uploadList .concat (url) : url
419+ )
420+ currentUploads .push (url)
420421 } else {
421- url += ` ${ this .bucket } .${ this .region } .aliyuncs.com/${ filename} `
422+ console .error (
423+ ` \` Promise.resolve\` 接收的参数应该是超链接(url), 当前为 ${ typeof url} .`
424+ )
422425 }
423- this .$emit (
424- ' input' ,
425- this .multiple ? this .uploadList .concat (url) : url
426- )
427- currentUploads .push (url)
428- })
429- .catch (err => {
430- // TODO 似乎可以干掉?🤔
431- reset ()
432- this .uploading = false
433-
434- // 捕获超时异常
435- if (e .code === ' ConnectionTimeoutError' ) {
436- /**
437- * 上传超时事件
438- */
439- this .$emit (' timeout' )
440- }
441- if (this .client .isCancel ()) {
442- /**
443- * 上传操作被取消事件
444- */
445- this .$emit (' cancel' )
446- } else {
447- /**
448- * 上传失败事件
449- */
450- this .$emit (' fail' )
451- }
452- })
426+ } catch (error) {
427+ this .handleCatchError (error)
428+ }
429+ } else {
430+ await this .client
431+ .multipartUpload (this .dir + key, file, this .uploadOptions )
432+ .then (res => {
433+ // 协议无关
434+ let url = doubleSlash
435+
436+ // 上传时阿里 OSS 会对文件名 encode,但 res.name 没有 encode
437+ // 因此要 encode res.name,否则会因为文件名不同,导致 404
438+ const filename = encodePath (res .name )
439+
440+ if (this .customDomain ) {
441+ if (this .customDomain .indexOf (doubleSlash) > - 1 )
442+ url = ` ${ this .customDomain } /${ filename} `
443+ else {
444+ url += ` ${ this .customDomain } /${ filename} `
445+ }
446+ } else {
447+ url += ` ${ this .bucket } .${ this .region } .aliyuncs.com/${ filename} `
448+ }
449+ this .$emit (
450+ ' input' ,
451+ this .multiple ? this .uploadList .concat (url) : url
452+ )
453+ currentUploads .push (url)
454+ })
455+ .catch (err => {
456+ // TODO 似乎可以干掉?🤔
457+ reset ()
458+
459+ if (this .client .isCancel ()) {
460+ /**
461+ * 上传操作被取消事件
462+ */
463+ this .$emit (' cancel' )
464+ }
465+
466+ this .handleCatchError (err)
467+ })
468+ }
453469
454470 this .newClient ()
455471 }
@@ -494,6 +510,17 @@ export default {
494510 },
495511 removeHighlight () {
496512 this .isHighlight = false
513+ },
514+ handleCatchError (error ) {
515+ this .uploading = false
516+
517+ if (error .code === ' ConnectionTimeoutError' ) {
518+ // 上传超时事件
519+ this .$emit (' timeout' )
520+ } else {
521+ // 上传失败
522+ this .$emit (' fail' )
523+ }
497524 }
498525 }
499526}
0 commit comments