@@ -541,25 +541,20 @@ export default {
541
541
if (this .beforeUpload && typeof this .beforeUpload === ' function' ) {
542
542
if (typeof this .beforeUpload === ' function' ) {
543
543
const result = await new Promise ((resolve ) => {
544
- let a
544
+ let beforeUploadResult
545
545
try {
546
- a = this .beforeUpload (item, (data ) => {
547
- if (! data) {
548
- resolve (false )
549
- } else {
550
- resolve (true )
551
- }
546
+ beforeUploadResult = this .beforeUpload (item, (data ) => {
547
+ resolve (!! data)
552
548
})
553
549
} catch (err) {
554
550
resolve (false )
555
551
}
556
552
// promise 模式
557
- if (a != null && typeof a .then === ' function' ) {
558
- a .then ((remoteData ) => {
559
- if (! remoteData) {
560
- resolve (false )
561
- }
562
- resolve (true )
553
+ if (beforeUploadResult != null && typeof beforeUploadResult .then === ' function' ) {
554
+ beforeUploadResult .then ((remoteData ) => {
555
+ resolve (!! remoteData)
556
+ }).catch (() => {
557
+ resolve (false )
563
558
})
564
559
}
565
560
})
@@ -568,39 +563,34 @@ export default {
568
563
return false
569
564
}
570
565
}
571
-
572
- // 除 promise 和 函数回调外其他形式都不支持
573
- this .$message .error (' 执行自定义上传出错, 检查远程方法是否是promise或者函数' )
574
- return false
575
566
}
576
567
// 如果是用户自定义方法
577
- // 出于简化 api 的考虑, 只允许单个文件上传
568
+ // 出于简化 api 的考虑, 只允许单个文件上传, 不进行代理
578
569
if (this .remoteFuc && typeof this .remoteFuc === ' function' ) {
579
- return new Promise ((resolve ) => {
580
- const a = this . remoteFuc ( item . file , ( data ) => {
581
- reduceResult (item, data)
582
- if ( ! data) {
583
- this . $message . error ( ' 执行自定义上传出错 ' )
584
- resolve ( false )
585
- } else {
586
- resolve (item )
587
- }
588
- })
570
+ const result = await new Promise ((resolve ) => {
571
+ let remoteFucResult
572
+ try {
573
+ remoteFucResult = this . remoteFuc ( item . file , ( remoteData ) => {
574
+ resolve (remoteData || false )
575
+ } )
576
+ } catch (err) {
577
+ this . $message . error ( ' 执行自定义上传出错 ' )
578
+ resolve ( false )
579
+ }
589
580
// promise 模式
590
- if (a != null && typeof a .then === ' function' ) {
591
- a .then ((remoteData ) => {
592
- reduceResult (item, remoteData)
593
- if (! remoteData) {
594
- resolve (false )
595
- }
596
- resolve (item)
581
+ if (remoteFucResult != null && typeof remoteFucResult .then === ' function' ) {
582
+ remoteFucResult .then ((remoteData ) => {
583
+ resolve (remoteData || false )
584
+ }).catch (() => {
585
+ resolve (false )
597
586
})
598
587
}
599
588
})
600
-
601
- // 除 promise 和 函数回调外其他形式都不支持
602
- this .$message .error (' 执行自定义上传出错, 检查远程方法是否是promise或者函数' )
603
- return false
589
+ reduceResult (item, result)
590
+ if (! result) {
591
+ return false
592
+ }
593
+ return item
604
594
}
605
595
606
596
// 使用内置上传
0 commit comments