小程序云开发中上传多图时,使用时间戳为文件命名时会有重复命名,怎么解决这个问题?
doUpload(filePath) {
const that = this;
var timestamp = (new Date()).valueOf();
const cloudPath = timestamp + '.png';
wx.cloud.uploadFile({
cloudPath,
filePath
}).then(res => {
console.log('[上传文件] 成功:', res)
const {
params
} = that.data;
const {
imgUrl
} = params;
imgUrl.push(res.fileID);
params['imgUrl'] = imgUrl;
that.setData({
imgUrl,
});
}).catch(error => {
console.error('[上传文件] 失败:', error);
wx.showToast({
icon: 'none',
title: '上传失败',
duration: 1000
})
})
},
chooseImage: function () {
const that = this;
// 选择图片
wx.chooseImage({
count: 5,
sizeType: ['compressed'],
sourceType: ['album', 'camera'],
success: function (res) {
const filePath = res.tempFilePaths;
//将选择的图片上传
filePath.forEach((path, _index) => {
that.doUpload(path);
});
const {
tempFilePaths
} = that.data;
that.setData({
tempFilePaths: tempFilePaths.concat(filePath)
}, () => {
console.log(that.data.tempFilePaths)
})
},
fail: e => {
console.error(e)
}
})
},