// pages/buy/cart.js import request from '../../utils/request' Page({ /** * 页面的初始数据 */ /** * 页面的初始数据 */ data: { adminShow: true,//编辑或完成 totalPrice: 0,//总金额 allselect:false,//是否全选 selectArr: [], //已选择的商品 cartData:[ { "id":1, "amount":2, "price":50, "name":"手机", "checked":false, "image":"https://images.unsplash.com/photo-1551214012-84f95e060dee?w=640" }, { "id":2, "amount":1, "price":10, "name":"显示器", "checked": false, "image":"https://images.unsplash.com/photo-1551214012-84f95e060dee?w=640" }, { "id":3, "amount":5, "price":120, "name":"可乐", "checked": false, "image":"https://images.unsplash.com/photo-1551214012-84f95e060dee?w=640" }, { "id":4, "amount":10, "price":50, "name":"手机", "checked": false, "image":"https://images.unsplash.com/photo-1551214012-84f95e060dee?w=640" }, { "id":4, "amount":1, "price":10, "name":"芝麻", "checked": false, "image":"https://images.unsplash.com/photo-1551214012-84f95e060dee?w=640" }, ], }, //计算价格 calculateTotal:function(){ var selectArr = this.data.selectArr; //已选择的商品 var totalPrice = 0; if(selectArr.length){ //如果存在商品就计算价格 for(var i=0;i{ if(res.retCode == 0){ // this.setData({ // cartData: res.body.cartItem // }) } }) }, //删除商品 deleteshopTap:function(){ //要删除 肯定是已经选中了的商品, 所以肯定在 selectArr里面 var cartData = this.data.cartData; //初始数据 var selectArr = this.data.selectArr; //已选择的商品数组 var that = this; if(selectArr.length){ //如果以选择的商品数组里有长度 for (var i = 0; i < cartData.length; i++) { for (var j = 0; j < selectArr.length; j++) { if (cartData[i].id == selectArr[j].id) { //把初始数据的对应id的数据删掉就好了 that.handleDelete(selectArr[j].id) cartData.splice(i, 1); } } } this.setData({ //重置一下数据 cartData: cartData, selectArr: [] //已选择的数组置空 }) this.calculateTotal(); //计算价格 } }, //编辑或完成 adminTap:function(){ //切换四个按钮的显示 this.setData({ adminShow: !this.data.adminShow }) }, //结算 toApply:function(){ console.log("已选择的商品:",this.data.selectArr) }, change(e){ console.log('1') this.setData({ test: e.detail.value }) }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.getCartInfo(options.id) }, getCartInfo(id){ const param = { "goodsId": id, "count": 1 } request._post('/firefly/cart/mycart',param, res=>{ if(res.retCode == 0){ this.setData({ cartData: res.body.cartItem }) } }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })