// customized/pages/made/car/car.js let app = getApp(); let host = app.globalData.servsers; // 请求的url Page({ /** * 页面的初始数据 */ data: { carList:[],// 购物车列表数组 host: app.globalData.servsers, amount:0, //总价 checkAll:false, // 全选 }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { let that = this; that.getCarlist(that) }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { }, subtraction(e){ // 减少 let that = this; console.log(e) let index = e.target.dataset.index,carList = that.data.carList; if (carList[index].customGoodsNum > 1){ // 表示最小数量,不可减少 carList[index].customGoodsNum--; that.setData({ carList: carList }) } that.totalPrice(that) }, add(e) { // 减少 let that = this; console.log(e) let index = e.target.dataset.index, carList = that.data.carList; carList[index].customGoodsNum++; that.setData({ carList: carList }) that.totalPrice(that) }, checkboxChange(e){ let index = e.target.dataset.index, that = this, carList = that.data.carList; if (e.detail.value[0]){ // 表示价格增加 that.data.carList[index].checked = true; // 表示选中 } else { that.data.carList[index].checked = false; // 表示选中 } that.setData({ carList: carList }) that.totalPrice(that) }, getCarlist(that){ wx.request({ url: host +'/queryShopCar', method:'post', header: { 'content-type': 'application/x-www-form-urlencoded' }, data:{ userId: 'oovpNwjlsY6xx8ceCebFa1dOLd9E' //app.globalData.user_id, }, success:function(res){ console.log('购物车列表数据',res) let carList = [],imgArray=[]; res.data.forEach((el,index)=>{ imgArray =el.customGoodsImg.split(','); carList.push(el); carList[index]['imgArray'] = imgArray; carList[index]['checked'] = false; }) that.setData({ carList: carList }) console.log(that.data.carList,"carListcarList") } }) }, totalPrice(that){ let amount = 0,checkAll = true; that.data.carList.forEach(res=>{ if(res.checked){ amount += res.customgoodsPriceNow * res.customGoodsNum } if (!res.checked) { checkAll = false } }) that.setData({ amount:amount.toFixed(2), checkAll: checkAll }) } })