-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupLoad.js
More file actions
97 lines (81 loc) · 1.61 KB
/
Copy pathupLoad.js
File metadata and controls
97 lines (81 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
var app = getApp()
var apiDomain = app.globalData.mysite
var app = getApp()
var imagePaths = [] //图片路径数组
var index = 0 // 要上传图片的索引
var cb = function() {} //回调函数
var inProgress = false // 是否正在上传图片中
var sucFun = function(path) {
cb({
success: true,
path: path,
index: index
})
++index
if (index < imagePaths.length) {
setTimeout(uploadFile, 50)
} else { //上传结束
inProgress = false
}
}
var errorFun = function() {
cb({
success: false,
path: null,
index: index
})
++index
if (index < imagePaths.length) {
setTimeout(uploadFile, 50)
} else { //上传结束
inProgress = false
}
}
var uploadFile = function() {
wx.uploadFile({
url: apiDomain + '/api/file/upload',
filePath: imagePaths[index],
name: 'file',
formData: {
token: app.globalData.token
},
success: function(resp) {
if (resp.statusCode == 200) {
var data = {}
try {
data = JSON.parse(resp.data)
sucFun(data.path)
} catch (e) {
throw "error:" + e.message + ", invalid json: " + resp.data
}
} else {
errorFun()
}
},
fail: function() {
errorFun()
}
})
}
//cb 是上传结束的回调函数
exports.uploadMany = function(arr, funCB) {
if (inProgress) {
wx.showToast({
title: '还有图片在上传中'
})
return;
}
inProgress = true
imagePaths = arr //图片路径数组
index = 0 // 要上传图片的索引
if (imagePaths.length == 0) {
wx.showToast({
title: '数组不能为空'
})
return;
}
if (funCB != null && typeof funCB == 'function') {
cb = funCB
}
setTimeout(uploadFile, 50)
}