之前在开发的过程中遇到过临时网络图片无法多次使用的问题,这时把图片通过云存储保存起来就能长期使用啦,代码如下:
async myevent(e) {
const {
avatar,
nickname
} = e.detail;
let cloudPath = "nickimg/" + new Date().getTime() + "-" + Math.floor(Math.random() * 1000) + '.png';
const avatarUrl = await this.uploadFile(cloudPath, avatar);
let userInfo = {
avatar: avatarUrl.fileID,
nickname: nickname
}
db.collection('users').where({
_openid: app.globalData.openid
}).update({
data: {
...userInfo
}
})
console.log("users表更新成功");
app.globalData.nickname = nickname;
app.globalData.avatar = avatarUrl.fileID;
this.setData({
nickname: nickname,
avatar: avatarUrl.fileID
})
},
这里主要是通过uploadFile(cloudPath, avatar)来把图片通过按照指定的路径存储到云存储里,然后通过avatarUrl.fileID来拿到云存储里的图片路径,最后存到你对应的云数据库的表里面