const db = wx.cloud.database(); // 初始化数据库
Page({
data: {
images: [], // 上传的图片
fileIds: [],
},
submit: function () {
// 上传图片到云存储
let promiseArr = [];
for (let i = 0; i < this.data.images.length; i++) {
promiseArr.push(new Promise((reslove, reject) => {
let item = this.data.images[i];
let suffix = /\.\w+$/.exec(item)[0];
wx.cloud.uploadFile({
cloudPath: new Date().getTime() + suffix,
filePath: item,
success: res => {
this.setData({
fileIds: this.data.fileIds.concat(res.fileID)
});
reslove();
},
fail: console.error
})
}));
}
Promise.all(promiseArr).then(res => {
// 插入数据
db.collection('top_op').add({
data: {
fileIds: this.data.fileIds
}
}).then(res => {}).catch(err => {})
});
},
uploadImg: function () {
// 选择图片
wx.chooseImage({
count: 9,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success: res => {
// tempFilePath可以作为img标签的src属性显示图片
const tempFilePaths = res.tempFilePaths
console.log(tempFilePaths);
this.setData({
images: this.data.images.concat(tempFilePaths)
});
this.submit()
}
})
},
})