You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

79 lines
1.8 KiB

const chooseUpload = (num, url, type, formData) => {
if (num == undefined || num == null || num == "")
num = 1
if (url == undefined || url == null || url == "")
url = "aos/file/batchUploadImage"
if (type == undefined || type == null || type == "")
type = "files"
if (formData == undefined || formData == null || formData == "")
formData = {}
let totalImgs = 0;
let httpUrl = [];
return new Promise((resolve) => {
uni.chooseImage({
count: num, //默认1
sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有
sourceType: ['album'], //从相册选择
success: function(res) {
let chooseImgs = res.tempFilePaths;
totalImgs = chooseImgs.length;
console.log("chooseImgs", res.tempFilePaths)
if (totalImgs > 0)
uni.showLoading({
title: '请稍后...',
mask: true
});
for (var i = 0; i < chooseImgs.length; i++) {
uni.uploadFile({
url: getApp().globalData.baseURL + url, //仅为示例,非真实的接口地址
filePath: chooseImgs[i],
name: type,
formData: formData,
success: (uploadFileRes) => {
let data = JSON.parse(uploadFileRes.data)
console.log("data", data)
if (data.success) {
if (data.data != undefined && data.data.length != 0)
// 必须用变量去接受
httpUrl = httpUrl.concat(data.data)
}
},
fail: (err) => {
console.log("err-->", err)
},
complete: () => {
totalImgs--;
if (totalImgs <= 0) {
uni.hideLoading();
resolve({
"num": chooseImgs.length,
"urls": httpUrl
})
}
}
});
}
},
fail: function(res) {
resolve({
"num": 0,
"urls": []
})
}
});
})
}
export default chooseUpload