Skip to content

Commit c2f3c8d

Browse files
committed
新增upload组件的before-upload钩子
1 parent 4ed03bb commit c2f3c8d

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

pages.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
"easycom": {
33
"^u-(.*)": "@/uview-ui/components/u-$1/u-$1.vue"
44
},
5-
"condition": { //模式配置,仅开发期间生效
6-
"current": 0, //当前激活的模式(list 的索引项)
7-
"list": [{
8-
"name": "test", //模式名称
9-
"path": "pages/componentsA/select/index", //启动页面,必选
10-
"query": "id=1&name=2" //启动参数,在页面的onLoad函数里面得到
11-
}]
12-
},
5+
// "condition": { //模式配置,仅开发期间生效
6+
// "current": 0, //当前激活的模式(list 的索引项)
7+
// "list": [{
8+
// "name": "test", //模式名称
9+
// "path": "pages/componentsA/select/index", //启动页面,必选
10+
// "query": "id=1&name=2" //启动参数,在页面的onLoad函数里面得到
11+
// }]
12+
// },
1313
"pages": [
1414
// 演示-组件
1515
{

pages/componentsB/upload/index.vue

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
:percent="item.progress"></u-line-progress>
1515
</view>
1616
</view>
17-
<u-upload ref="uUpload" :custom-btn="customBtn" :show-upload-list="showUploadList" :action="action" :auto-upload="autoUpload" :file-list="fileList"
17+
<u-upload :beforeUpload="beforeUpload" ref="uUpload" :custom-btn="customBtn" :show-upload-list="showUploadList" :action="action" :auto-upload="autoUpload" :file-list="fileList"
1818
:show-progress="showProgress" :deletable="deletable" :max-count="maxCount" @on-list-change="onListChange">
1919
<view v-if="customBtn" slot="addBtn" class="slot-btn" hover-class="slot-btn__hover" hover-stay-time="150">
2020
<u-icon name="photo" size="60" :color="$u.color['lightColor']"></u-icon>
@@ -53,7 +53,7 @@
5353
export default {
5454
data() {
5555
return {
56-
action: 'http://192.168.100.17/index.php/index/index/upload',
56+
action: 'http://www.tp5.com',
5757
// 预置上传列表
5858
fileList: [],
5959
// fileList: [{
@@ -70,6 +70,9 @@
7070
maxCount: 2,
7171
lists: [], // 组件内部的文件列表
7272
}
73+
},
74+
onLoad() {
75+
7376
},
7477
methods: {
7578
reUpload() {
@@ -136,6 +139,9 @@
136139
onListChange(lists) {
137140
// console.log('onListChange', lists);
138141
this.lists = lists;
142+
},
143+
beforeUpload(index, lists) {
144+
return true;
139145
}
140146
}
141147
}

uview-ui/components/u-upload/u-upload.vue

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ export default {
334334
this.uploadFile(index);
335335
},
336336
// 上传图片
337-
uploadFile(index = 0) {
337+
async uploadFile(index = 0) {
338338
if (this.disabled) return;
339339
if (this.uploading) return;
340340
// 全部上传完成
@@ -353,13 +353,22 @@ export default {
353353
return;
354354
}
355355
// 执行before-upload钩子
356-
// if(this.beforeUpload && typeof(this.beforeUpload) === 'function') {
357-
// let beforeResponse = this.beforeUpload(index, this.lists);
358-
// if (beforeResponse && beforeResponse.then) {
359-
360-
// }
361-
// }
362-
356+
if(this.beforeUpload && typeof(this.beforeUpload) === 'function') {
357+
// 执行回调,同时传入索引和文件列表当作参数
358+
let beforeResponse = this.beforeUpload(index, this.lists);
359+
// 判断是否返回了promise
360+
if (!!beforeResponse && typeof beforeResponse.then === 'function') {
361+
await beforeResponse.then(res => {
362+
// promise返回成功,不进行动作,继续上传
363+
}).catch(err => {
364+
// 进入catch回调的话,继续下一张
365+
return this.uploadFile(index + 1);
366+
})
367+
} else if(beforeResponse === false) {
368+
// 如果返回false,继续下一张图片的上传
369+
return this.uploadFile(index + 1);
370+
}
371+
}
363372
this.lists[index].error = false;
364373
this.uploading = true;
365374
// 创建上传对象

0 commit comments

Comments
 (0)