// pages/commentinfo/commentinfo.js import commentsApi from '../../api/comments'; import uploadApi from '../../api/upload' Page({ /** * 页面的初始数据 */ data: { shopStars: 5, productStars: 5, textWidth: 0, wordsNum: 0, limitNum: 70, outLimit: false, commentText: "", imgs: [], orderId: 0, productId: 0, shopId: 0, imgurls: [], }, // 上传图片 chooseImg: async function (e) { var that = this; var imgs = this.data.imgs; if (imgs.length >= 9) { this.setData({ lenMore: 1 }); setTimeout(function () { that.setData({ lenMore: 0 }); }, 2500); return false; } wx.chooseImage({ // count: 1, // 默认9 sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有 success: async function (res) { // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片 let tempFilePaths = res.tempFilePaths; console.log(tempFilePaths) let imgs = that.data.imgs; // console.log(tempFilePaths + '----'); for (var i = 0; i < tempFilePaths.length; i++) { if (imgs.length >= 9) { that.setData({ imgs: imgs }); return false; } else { var imgUrl = await uploadApi.uploadCommentImgs(tempFilePaths[i]); imgs.push(imgUrl); } } that.setData({ imgs }) } }); }, // 删除图片 deleteImg: function (e) { var imgs = this.data.imgs; var index = e.currentTarget.dataset.index; imgs.splice(index, 1); this.setData({ imgs: imgs }); }, // 预览图片 previewImg: function (e) { //获取当前图片的下标 var index = e.currentTarget.dataset.index; //所有图片 var imgs = this.data.imgs; wx.previewImage({ //当前显示图片 current: imgs[index], //所有图片 urls: imgs }) }, sendComment: async function (e) { let self = this; let wordsNum = this.data.wordsNum; let limitNum = this.data.limitNum; let content = this.data.commentText; let imgs = this.data.imgs; if (wordsNum > limitNum) { wx.showModal({ title: '字数超出限制!', content: '请按规定的字数评价。', showCancel: false, confirmText: "确定" }) return; } if (wordsNum == 0) { wx.showModal({ title: '请填写评论', content: '请谈谈你对产品的评价吧', showCancel: false, confirmText: "确定" }) return; } let storeScore = this.data.shopStars; let productScore = this.data.productStars; let imgUrls = imgs.join(','); let storeId = this.data.shopId; let productId = this.data.productId; let orderId = this.data.orderId; let code = await commentsApi.sendDiscuss(storeId, productId, orderId, productScore, storeScore, content, imgUrls); if (code == 1) { wx.showModal({ title: '评论成功!', content: '评论可以在我的评论中查看', showCancel: false, confirmText: "确定", success(res) { wx.switchTab({ url: '../personorder/personorder', }) } }) } else { wx.showModal({ title: '评论失败!', showCancel: false, confirmText: "确定" }) } }, //给店铺评分 changeShopStars: function (e) { this.setData({ shopStars: e.currentTarget.dataset.i }) }, //给产品评分 changeProductStars: function (e) { this.setData({ productStars: e.currentTarget.dataset.i }) }, //根据当前的评论字数更改字数的num changeComment: function (e) { var length = e.detail.cursor; var content = e.detail.value; if (length > this.data.limitNum) { this.setData({ wordsNum: length, outLimit: true, commentText: content }) return; } this.setData({ wordsNum: length, outLimit: false, commentText: content }) }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { var textWidth = wx.getSystemInfoSync().windowWidth * (750 / wx.getSystemInfoSync().windowWidth) * 0.9; this.setData({ textWidth }) console.log(options) var orderId = options.id; var shopId = options.shopId; var productId = options.productId; this.setData({ orderId, productId, shopId }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })