一,上传文件
//cloudPath: 'comp_img/' + new Date().getTime() + parseInt(Math.random() * 10) + avatar.match(/\.[^.]+?$/)[0],//.match(/\.[^.]+?$/)[0]=图片类型后缀,,new Date().getTime()=时间戳,,parseInt(Math.random() * 10)=10以内的随机整数
wx.cloud.uploadFile({
cloudPath: 'comp_img/' + app.globalData.openid,//头像类的唯一图片,就用openid命名,再上传就会覆盖原来的,避免残留!'comp_img/'是文件夹名称!
filePath: avatar, // 文件路径
success: res => {
console.log('[上传文件] 成功:', res)
//云函
wx.cloud.callFunction({
name: 'updateUserInfo',
data: {
comp_img: res.fileID,
},
complete: lete => {
console.log('updateUserInfo=' + JSON.stringify(lete))
}
})
that.setData({
comp_img: avatar
})
console.log(that.data);
},
fail: e => {
console.error('[上传文件] 失败:', e)
wx.showToast({
icon: 'none',
title: '上传失败',
})
},
complete: () => {
wx.hideLoading()
}
})
根据图片ID取回本地路径
let openid = app.globalData.openid;
const db = wx.cloud.database();
const Users = db.collection('Users');
Users.where({ _openid: openid }).get({
success: function (res) {
console.log(res)
that.checkED(arrSex,res.data[0].comp_sex);
that.setData({
arrSex: arrSex,
comp_name: res.data[0].comp_name,
comp_sex: res.data[0].comp_sex,
comp_email: res.data[0].comp_email,
comp_mobile: res.data[0].comp_mobile,
//comp_work_pcp: res.data[0].citykey,
address: res.data[0].comp_work_address,
})
wx.cloud.downloadFile({
fileID: res.data[0].comp_img, // 文件 ID
success: res => {
// 返回临时文件路径
that.setData({comp_img:res.tempFilePath})
},
fail: console.error
})
},
fail: function (err) {
console.error('err:', err)
},
complete: function (ok) {
},
});