在开发小程序中需要做一个用户评论上传图片的功能,首先考虑到微信的wx.uploadFile接口只能一次上传一张,而功能需求是需要单张或者多张上传,最常用的方法是用for循环去上传
这样:
//上传图片,这里使用了vant图片上传的组件,类似于wx.chooseImage,其实方法都是一样的这里只是把上传的图片路径暂时保存在本地
afterRead(event) {
console.log(event)
const {
file } = event.detail;
// 当设置 mutiple 为 true 时, file 为数组格式,否则为对象格式
this.data.fileList.push({
url: file.path ,size: file.size})
console.log(this.data.fileList)
this.setData({
fileList:this.data.fileList
})
},
//这是删除一张图片的方法
deleteRead:function(event){
console.log(event)
// 删除指定下标的图片对象
var that= this;
wx.showModal({
title: '要删除这张图片吗?',
content: '',
cancelText: '取消',
confirmText: '确定',
success: res => {
if (res.confirm) {
that.data.fileList.splice(event.detail.index, 1);
that.setData({
fileList: that.data.fileList
})
}
}
})
},
//下面重点来了
//提交评论
bindFormSubmit: function(e) {
var that = this;
let dataset = e.detail.value.textarea;//评论内容
let miaoshuVal = that.data.miaoshuVal;//描述评分
let wuliuVal = that.data.wuliuVal;//物流评分
let fileList = that.data.fileList;//图片
var skushowpricekey = "";
console.log(miaoshuVal,wuliuVal)
if(dataset == ""){
wx.showModal({
title: '提示',
content: '请填写评价内容',
showCancel: false
})
return;
}
if(miaoshuVal == 0 || wuliuVal ==0 )