// 七牛云上传多张图片 默认上传图片 type = 1 视频
qiniuToUpload(type, callFn){
let that = this
that.request('get', '/shifu/index/api_upload_qiniu_get_token', {}, function (res) {
if (res.error_code == 0) {
that.globalData.upload_host = res.data.upload_host
that.globalData.img_domain = res.data.host
that.uploadFile(res.data.token, type, callFn)
} else {
wx.showToast({
title: res.error_message,
icon: 'none'
})
}
}, (err) => {})
}
// 上传 默认上传图片; type = 1 是视频
uploadFile(token, type, callFn) {
let that = this
if (type == 1) {
wx.chooseVideo({
success(res) {
const tempFilePaths = res.tempFilePath
wx.showLoading({
title: '拼命上传中',
})
wx.uploadFile({
url: that.globalData.upload_host,
filePath: tempFilePaths,
name: 'file',
formData: {
'token': that.data.token
},
success(res) {
const data = JSON.parse(res.data)
callFn && callFn(data);
},
fail: (res) => {
wx.showToast({
title: res.errMsg,
icon: 'none'
})
},
complete() {
wx.hideLoading()
}
})
}
})
}
else {
wx.chooseImage({
count: 9,
success(res) {
console.log(res)
// tempFilePath可以作为img标签的src属性显示图片
const tempFilePaths = res.tempFilePaths
wx.showLoading({
title: '拼命上传中',
})
for (let i = 0; i < tempFilePaths.length; i++) {
wx.uploadFile({
url: that.globalData.upload_host,
filePath: tempFilePaths[i],
name: 'file',
formData: {
'token': token,
},
success(res) {
console.log(res)
const data = JSON.parse(res.data)
callFn && callFn(data);
},
fail: (res) => {
wx.showToast({
title: res.errMsg,
icon: 'none'
})
},
complete() {
if (i == tempFilePaths.length - 1) {
wx.hideLoading()
}
}
})
}
}
})
}
}
// 请求方法封装
request(type,url,obj,callback) {
wx.showLoading({
title: '拼命加载中',
})
let that = this
let openid = wx.getStorageSync('openid') ? wx.getStorageSync('openid'): that.globalData.openid;
let appid = that.globalData.appid
// 不是登录接口
if(url!='/api/login') {
// 视接口所需参数而定
obj=Object.assign(obj,{'openid':openid});
obj=Object.assign(obj,{'appid':appid});
}
let param=obj;
wx.request({
method: type,
dataType: 'json',
url: that.globalData.requestUrl + url,
data: param,
header: {
'content-type': 'application/json',
},
success: function (res) {
callback(res.data);
},
complete: function() {
wx.hideLoading({
success: (res) => {},
})
}
})
}
小程序公共上传方法
最新推荐文章于 2024-07-26 17:59:15 发布