记录代码(购物车相关)小程序 vantweapp

import { request } from "../../../request/index";
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog';
// pages/my/ShoppingCart/ShoppingCart.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    disabled:false,
    result: [],
    checked:false,
    valid_goods_list:[],
    invalid_goods_list:[],
    num:0,
    tag:0,
    total_price:0,
    dataList:[],
    deldataList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onChange2(event) {
    this.setData({
      checked: event.detail,
    });
    if(event.detail){
      this.setData({
        result:this.data.dataList,
        tag:1
      })
    }else{
      this.setData({
        result:[],
        tag:0
      })
    }
    this.selectCart();
    this.getPrice()
  },
  getPrice(){
    console.log(123)
    let that = this;
    let totalPrice = 0;
    let num = 0;
    function checkAdult(e) {
      // console.log(e.cart_id,that.data.result.includes(String(e.cart_id)))
      return that.data.result.includes(String(e.cart_id))
    }
    // console.log(this.data.valid_goods_list.filter(checkAdult))
    this.data.valid_goods_list.filter(checkAdult).forEach((e) => {
      // console.log(e)
      num += e.goods_num;
      totalPrice += (e.goods_num * e.market_price)
    })
    this.setData({
      total_price:parseFloat(totalPrice).toFixed(2),
      num
    })
  },
  onStepper(e){
    console.log(e)
    // addToCart
    // minusToCart
    // wx.showLoading({
    //   title: '加载中...',
    //   mask:true
    // })
    this.setData({
      disabled:true
    });
    
    let index = e.currentTarget.dataset.index;
    console.log(this.data.valid_goods_list[index].goods_num,e.detail)
    if(this.data.valid_goods_list[index].goods_num > e.detail){
      this.minusToCart(e.currentTarget.dataset.cart_id).then((res) => {
        // this.cedshi(e)
        this.data.valid_goods_list[index].goods_num = e.detail;
        this.setData({
          valid_goods_list:this.data.valid_goods_list
        })
        this.setData({
          disabled:false
        })
        this.getPrice()
        // wx.hideLoading({
        //   complete: (res) => {},
        // })
      }).catch(()=>{
        this.setData({
          disabled:false
        })
      })
    }else{
      this.addToCart(e.currentTarget.dataset.cart_id).then((res) => {
        this.data.valid_goods_list[index].goods_num = e.detail;
        this.setData({
          valid_goods_list:this.data.valid_goods_list
        })
        this.setData({
          disabled:false
        })
        this.getPrice()
        // wx.hideLoading({
        //   complete: (res) => {},
        // })
      }).catch(()=>{
        this.setData({
          disabled:false
        })
      })
    }
   
  },
  onChange1(event) {
    this.setData({
      result: event.detail,
    });
    console.log(event)
    if(event.detail.length == this.data.dataList.length){
      this.setData({
        checked:true,
        tag:1
      })
      // this.selectCart()
    }else{
      this.setData({
        checked:false,
        tag:0
      })
    }
    this.getPrice()
    // this.selectCart()
  },
  onChange(event) {
    let cart_id = event.currentTarget.dataset.cart_id;
    let is_select = (this.data.result.includes(String(cart_id)) ? 1 : 0)
    // console.log(this.data.result.includes(String(cart_id)))
    this.checkOneCart(cart_id,is_select)
  },
  onLoad: function (options) {
  },
  checkOneCart(cart_id,is_select){
    request({
      url: 'cart/checkOneCart',
      data: {
        token:wx.getStorageSync('token'),
        cart_id,
        is_select
      }
    }).then(res => {
      if (res.data.status == 1) {
        
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 1500
        })
      }
    })
  },
  selectCart(){
    wx.showLoading({
      title: '加载中...',
    })
    request({
      url: 'cart/selectCart',
      data: {
        token:wx.getStorageSync('token'),
        areaId:wx.getStorageSync('areaId'),
        tag:this.data.tag
      }
    }).then(res => {
      if (res.data.status == 1) {
        
         wx.hideLoading({
           complete: (res) => {},
         })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 1500
        })
      }
    })
  },
  onClose(event) {
    const { position, instance } = event.detail;
    console.log(event)
    switch (position) {
      case 'left':
      case 'cell':
        instance.close();
        break;
      case 'right':
        Dialog.confirm({
          message: '确定删除吗?',
        }).then(() => {
          instance.close();
        });
        break;
    }
  },
  sc(e){
    console.log(e);
    Dialog.confirm({
      message: '确定移入收藏夹吗?',
    }).then(() => {
      request({
        url: 'index/collect',
        data: {
          type:4,
          token:wx.getStorageSync('token'),
          id:e.currentTarget.dataset.id
        }
      }).then(res => {
        if (res.data.status == 1) {
          //  this.cartList()
          wx.showToast({
            title: res.data.msg,
            duration: 1500
          })
        } else {
          
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 1500
          })
        }
      })
    }).catch(() => {
      // on cancel
      // console.log(22)

      wx.showToast({
        title: '取消收藏',
        icon:"none"
      })
    });
  },
  del(e){
    console.log(e);
    Dialog.confirm({
      message: '确定删除吗?',
    }).then(() => {
      // console.log(11)
      request({
        url: 'cart/cartDelete',
        data: {
          token:wx.getStorageSync('token'),
          cart_id:e.currentTarget.dataset.cart_id
        }
      }).then(res => {
        if (res.data.status == 1) {
           this.cartList()
        } else {
          
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 1500
          })
        }
      })
    }).catch(() => {
      // on cancel
      // console.log(22)

      wx.showToast({
        title: '取消删除',
        icon:"none"
      })
    });
  },
  cartList(){
    wx.showLoading({
      title: '加载中...',
    })
    request({
      url: 'cart/cartList',
      data: {
        token:wx.getStorageSync('token'),
        areaId:wx.getStorageSync('areaId')
      }
    }).then(res => {
      if (res.data.status == 1) {
        this.data.dataList = [];
        this.data.deldataList = [];
        res.data.data.valid_goods_list.forEach(element => {
          this.data.dataList.push(String(element.cart_id))
        })
        res.data.data.invalid_goods_list.forEach(element => {
          this.data.deldataList.push(String(element.cart_id))
        })
        
         this.setData({
          dataList:this.data.dataList,
          deldataList:this.data.deldataList,
          valid_goods_list:res.data.data.valid_goods_list,
          invalid_goods_list:res.data.data.invalid_goods_list,
          num:res.data.data.num,
          tag:res.data.data.tag,
          total_price:res.data.data.total_price,
         })
         if(res.data.data.tag == 1){
            this.setData({
              checked:true,
              result:this.data.dataList
           })
         }else{
          res.data.data.valid_goods_list.forEach(element => {
            if(element.is_select){
              this.data.result.push(String(element.cart_id))
            }
          })
          this.setData({
            result:this.data.result
          })
         }
        //  this.selectCart()
         wx.hideLoading({
           complete: (res) => {},
         })
         wx.stopPullDownRefresh({
           complete: (res) => {},
         })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 1500
        })
      }
    })
  },
  clear(){
    Dialog.confirm({
      message: '确定删除吗?',
    }).then(() => {
    request({
      url: 'cart/cartDelete',
      data: {
        token:wx.getStorageSync('token'),
        cart_id:this.data.deldataList
      }
    }).then(res => {
      if (res.data.status == 1) {
         this.cartList()
      } else {
      
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 1500
        })
      }
    })
  }).catch(() => {
    // on cancel
    // console.log(22)

    wx.showToast({
      title: '取消删除',
      icon:"none"
    })
  });
  },
  addToCart(cart_id){
   return new Promise((r,j)=>{
    request({
      url: 'cart/addToCart',
      data: {
        token:wx.getStorageSync('token'),
        cart_id
      }
    }).then(res => {
      if (res.data.status == 1) {
        r(res)
      } else {
        j()
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 1500
        })
      }
    })
   });
    
  },
  minusToCart(cart_id){
    return new Promise((r,j)=>{ request({
      url: 'cart/minusToCart',
      data: {
        token:wx.getStorageSync('token'),
        cart_id
      }
    }).then(res => {
      if (res.data.status == 1) {
        r(res)
      } else {
        j()
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 1500
        })
      }
    })
   });

  },
  gotoOrder(){
    if(this.data.num <= 0){
      wx.showToast({
        title: '请至少选择一件商品',
        icon:"none"
      })
    }else{
      wx.navigateTo({
        url: '../../ShopMall/order/order?cart_id=' + JSON.stringify(this.data.result),
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.cartList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      valid_goods_list:[],
      invalid_goods_list:[],
      num:0,
      tag:0,
      total_price:0,
      result:[],
      dataList:[]
    })
    this.cartList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值