123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- var util = require('../../utils/util.js');
- var api = require('../../config/api.js');
- var app = getApp();
- import { Router } from '../../utils/router.js'
- Router({
- data: {
- cartGoods: [],
- cartTotal: {
- "goodsCount": 0,
- "goodsAmount": 0.00,
- "checkedGoodsCount": 0,
- "checkedGoodsAmount": 0.00
- },
- isEditCart: false,
- checkedAllStatus: true,
- editCartList: []
- },
- onLoad: function (options) {
- // 页面初始化 options为页面跳转所带来的参数
- // this.getCartList();
- getApp().checkNetStatu();
- },
- onReady: function () {
- // 页面渲染完成
- },
- onShow: function () {
- // 页面显示
- if (typeof this.getTabBar === 'function' && this.getTabBar()) {
- this.getTabBar().setData({
- selected: 2
- })
- }
- getApp().updateCardCount()
- let isLogin = wx.getStorageSync('isLogin')
- this.getCartList();
- },
- onPullDownRefresh(){
- this.getCartList();
- getApp().onPullDownRefresh()
- },
- onHide: function () {
- // 页面隐藏
- },
- onUnload: function () {
- // 页面关闭
- },
- getCartList: function () {
- let that = this;
- util.request(api.CartList).then(function (res) {
- if (res.errno === 0) {
- that.setData({
- cartGoods: res.data.cartList,
- cartTotal: res.data.cartTotal
- });
- }
- that.setData({
- checkedAllStatus: that.isCheckedAll()
- });
- });
- },
- isCheckedAll: function () {
- //判断购物车商品已全选
- return this.data.cartGoods.every(function (element, index, array) {
- if (element.checked == true) {
- return true;
- } else {
- return false;
- }
- });
- },
- checkedItem: function (event) {
- let itemIndex = event.currentTarget.dataset.itemIndex;
- let that = this;
- if (!this.data.isEditCart) {
- util.request(api.CartChecked, { productIds: that.data.cartGoods[itemIndex].product_id, isChecked: that.data.cartGoods[itemIndex].checked ? 0 : 1 }, "POST", "application/json").then(function (res) {
- if (res.errno === 0) {
- that.setData({
- cartGoods: res.data.cartList,
- cartTotal: res.data.cartTotal
- });
- }
- getApp().updateCardCount()
- that.setData({
- checkedAllStatus: that.isCheckedAll()
- });
- });
- } else {
- //编辑状态
- let tmpCartData = this.data.cartGoods.map(function (element, index, array) {
- if (index == itemIndex){
- element.checked = !element.checked;
- }
-
- return element;
- });
- that.setData({
- cartGoods: tmpCartData,
- checkedAllStatus: that.isCheckedAll(),
- 'cartTotal.checkedGoodsCount': that.getCheckedGoodsCount()
- });
- }
- },
- getCheckedGoodsCount: function(){
- let checkedGoodsCount = 0;
- this.data.cartGoods.forEach(function (v) {
- if (v.checked === true) {
- checkedGoodsCount += v.number;
- }
- });
- return checkedGoodsCount;
- },
- checkedAll: function () {
- let that = this;
- if (!this.data.isEditCart) {
- var productIds = this.data.cartGoods.map(function (v) {
- return v.product_id;
- });
- util.request(api.CartChecked, { productIds: productIds.join(','), isChecked: that.isCheckedAll() ? 0 : 1 }, "POST", "application/json").then(function (res) {
- if (res.errno === 0) {
- that.setData({
- cartGoods: res.data.cartList,
- cartTotal: res.data.cartTotal
- });
- }
- getApp().updateCardCount()
- that.setData({
- checkedAllStatus: that.isCheckedAll()
- });
- });
- } else {
- //编辑状态
- let checkedAllStatus = that.isCheckedAll();
- let tmpCartData = this.data.cartGoods.map(function (v) {
- v.checked = !checkedAllStatus;
- return v;
- });
- that.setData({
- cartGoods: tmpCartData,
- checkedAllStatus: that.isCheckedAll(),
- 'cartTotal.checkedGoodsCount': that.getCheckedGoodsCount()
- });
- }
- },
- editCart: function () {
- var that = this;
- if (this.data.isEditCart) {
- this.getCartList();
- this.setData({
- isEditCart: !this.data.isEditCart
- });
- } else {
- //编辑状态
- let tmpCartList = this.data.cartGoods.map(function (v) {
- v.checked = false;
- return v;
- });
- this.setData({
- editCartList: this.data.cartGoods,
- cartGoods: tmpCartList,
- isEditCart: !this.data.isEditCart,
- checkedAllStatus: that.isCheckedAll(),
- 'cartTotal.checkedGoodsCount': that.getCheckedGoodsCount()
- });
- }
- },
- toIndexPage: function () {
- global.type = 'all'
- wx.switchTab({
- url: "/pages/index/index"
- });
- },
- updateCart: function (itemIndex, tag) {
- let that = this;
- let cartItem = this.data.cartGoods[itemIndex];
- let { product_id, goods_id, id, number } = cartItem
- if(tag ==='add'){
- number += 1
- } else {
- if (cartItem.number - 1 < 1) {
- return wx.showToast({
- icon: 'none',
- title: '亲,不能再减少了',
- duration: 3000
- })
- } else {
- number = (cartItem.number - 1 > 1) ? cartItem.number - 1 : 1;
- }
- }
- util.request(api.CartUpdate, {
- productId: product_id,
- goodsId: goods_id,
- number: number,
- id: id
- }).then(function (res) {
- if (res.errno === 0) {
- cartItem.number = number
- that.setData({
- cartGoods: that.data.cartGoods
- });
- } else if (res.errno === 400){
- util.showErrorToast('库存不足', () => {
- that.setData({
- canAdd:false
- })
- });
- }
- getApp().updateCardCount()
- that.setData({
- checkedAllStatus: that.isCheckedAll()
- });
- });
- },
- cutNumber: function (event) {
- let itemIndex = event.target.dataset.itemIndex;
- // let cartItem = this.data.cartGoods[itemIndex];
- // let number = (cartItem.number - 1 > 1) ? cartItem.number - 1 : 1;
- // cartItem.number = number;
- // this.setData({
- // cartGoods: this.data.cartGoods
- // });
- this.updateCart(itemIndex, 'cut');
- // this.updateCart(cartItem.product_id, cartItem.goods_id, number, cartItem.id);
- },
- addNumber: function (event) {
- let itemIndex = event.target.dataset.itemIndex;
- // let cartItem = this.data.cartGoods[itemIndex];
- // let number = cartItem.number + 1;
- this.updateCart(itemIndex,'add');
- },
- checkoutOrder: function () {
- //获取已选择的商品
- let that = this;
- var checkedGoods = this.data.cartGoods.filter(function (element, index, array) {
- if (element.checked == true) {
- return true;
- } else {
- return false;
- }
- });
- if (checkedGoods.length <= 0) {
- util.showErrorToast('请选择商品');
- return false;
- }
- wx.navigateTo({
- url: '../shopping/checkout/checkout'
- })
- },
- deleteCart: function () {
- //获取已选择的商品
- let that = this;
- let productIds = this.data.cartGoods.filter(function (element, index, array) {
- if (element.checked == true) {
- return true;
- } else {
- return false;
- }
- });
- if (productIds.length <= 0) {
- util.showErrorToast('请选择商品');
- return false;
- }
- productIds = productIds.map(function (element, index, array) {
- if (element.checked == true) {
- return element.product_id;
- }
- });
- util.request(api.CartDelete, {
- productIds: productIds.join(',')
- }, 'POST', 'application/json').then(function (res) {
- if (res.errno === 0) {
- let cartList = res.data.cartList.map(v => {
- v.checked = false;
- return v;
- });
- that.setData({
- cartGoods: cartList,
- cartTotal: res.data.cartTotal
- });
- getApp().updateCardCount()
- }
- that.setData({
- checkedAllStatus: that.isCheckedAll()
- });
- });
- }
- })
|