var app = getApp(); var WXAPI = require('../../utils/server.js'); var that; Page({ data: { windowHeight: 'auto', commentList: [], images: [], imageWidth: 0, remind: '加载中', addHide: 0, }, //页面加载完成函数 onReady: function () { var that = this; }, onLoad: function (options) { that = this; var orderId = options.id; // var user_id = app.globalData.userInfo.openid; // 微信id // var pid = options.pid; // 商品id // var attribute_id = options.attribute_id; // 属性id // if (pid) { // pid = pid; // } else { // pid = ''; // } // if (attribute_id) { // attribute_id = attribute_id; // } else { // attribute_id = ''; // } WXAPI.commentInfo({ access_id: app.globalData.accessId, store_id: 239, storeType: 1, language: '', order_details_id: orderId, }).then(res => { if (res.code == 200) { var data = res.data var commentList = data.commentList; if (commentList.length > 1) { // 初始化评论选项为好评 for (var i = 0, len = commentList.length; i < len; i++) { commentList[i].commentType = 'GOOD'; commentList[i].images = []; commentList[i].id = i; commentList[i].addHide = commentList[i].images.length; } } else { commentList[0].commentType = 'GOOD'; commentList[0].images = ''; commentList[0].id = 0; commentList[0].addHide = commentList[0].images.length; } that.setData({ commentList: commentList, orderId: orderId, remind: '' }); } }) }, onShow: function () { // 页面显示 }, chooseImage: function (e) { var id = e.target.id; var that = this; // 选择图片 }, //删除图片 delete: function (e) { var id = e.currentTarget.id; var index = e.currentTarget.dataset.index; var commentList = that.data.commentList; var images = commentList[id].images; images.splice(index, 1); commentList[id].images = images; commentList[id].addHide = images.length; that.setData({ commentList: commentList, }); }, previewImage: function (e) { //数组的id var id = e.currentTarget.id; //整个数据 var commentList = that.data.commentList; //取单个数据 var images = commentList[id].images // 预览图集 wx.previewImage({ urls: images }); }, //保存评论内容 setcon: function (e) { var commentList = this.data.commentList; var index = parseInt(e.currentTarget.dataset.index); commentList[index].content = e.detail.value; this.setData({ commentList: commentList }); }, selectCommentType: function (e) { var commentList = this.data.commentList; var index = parseInt(e.currentTarget.dataset.index); commentList[index].commentType = e.currentTarget.dataset.type; this.setData({ 'commentList': commentList }); }, //保存图片 saveimg: function (id, key) { var commentList = that.data.commentList; var images = commentList[key].images; // 提交图片,事先遍历图集数组 var formData = { 'id': id }; for (var i = 0, len = images.length; i < len; i++) { that.upload_file('&action=product&m=t_comment&type=file', images[i], 'imgFile', formData); } }, submitComment: function (e) { var vm = this; var commentList = []; for (var i = 0, len = vm.data.commentList.length; i < len; i++) { commentList.push({ commodityId: vm.data.commentList[i].commodityId, // 商品id images: vm.data.commentList[i].images, // 商品id content: e.detail.value['content_' + i], // 评论内容 score: vm.data.commentList[i].commentType, // 评论类型(好评、中评、差评) size: vm.data.commentList[i].size, // 属性名称 attribute_id: vm.data.commentList[i].sid, // 属性名称 orderId: vm.data.orderId, // 订单号 userid: app.globalData.userInfo.openid, // 微信id pingid: vm.data.commentList[i].id // 第几条数据 }); } wx.request({ url: app.d.ceshiUrl + '&action=product&m=t_comment&type=json', method: 'post', data: { comments: commentList }, header: { 'content-type': 'application/json' // 默认值 }, success: function (res) { var status = res.data.status; if (status == 1) { var arrid = res.data.arrid; //获取ID后转至上传图片 for (var i = 0, len = arrid.length; i < len; i++) { that.saveimg(arrid[i], i); } wx.showToast({ title: res.data.succ, duration: 2000 }); wx.navigateBack({ delta: 1 }); } else { wx.showToast({ title: res.data.err, duration: 2000 }); } }, error: function (e) { wx.showToast({ title: '网络异常!', duration: 2000 }); } }); }, //上传文件 upload_file: function (url, filePath, name, formData, success, fail) { wx.uploadFile({ url: app.d.ceshiUrl + url, filePath: filePath, name: name, header: { 'content-type': 'multipart/form-data' }, // 设置请求的 header formData: formData, // HTTP 请求中其他额外的 form data success: function (res) { console.log(res); }, fail: function (res) { console.log(res); } }) }, })