228 lines
5.5 KiB
JavaScript
Executable File
228 lines
5.5 KiB
JavaScript
Executable File
const app = getApp();
|
|
Component({
|
|
options: {
|
|
addGlobalClass: true,
|
|
multipleSlots: true
|
|
},
|
|
lifetimes: {
|
|
attached() {
|
|
let shopCartData = app.globalData.shopCart
|
|
var amount = 0
|
|
if (shopCartData.length > 0) {
|
|
shopCartData.forEach(it => {
|
|
if (it.shopId == this.properties.shopId) {
|
|
this.data.shopCart = this.data.shopCart.concat(it)
|
|
amount += it.buyNum
|
|
}
|
|
})
|
|
}
|
|
this.setData({
|
|
shopId: this.properties.shopId,
|
|
shopCart: this.data.shopCart,
|
|
buyAmount: amount
|
|
})
|
|
}
|
|
},
|
|
/**
|
|
* 组件的对外属性
|
|
*/
|
|
properties: {
|
|
shopCart: {
|
|
type: Array,
|
|
default: []
|
|
},
|
|
goods: {
|
|
type: Object,
|
|
default: null
|
|
},
|
|
shopId: {
|
|
type: String,
|
|
default: '',
|
|
},
|
|
showBtn: {
|
|
type: Boolean,
|
|
default: true
|
|
}
|
|
},
|
|
/**
|
|
* 组件的初始数据
|
|
*/
|
|
data: {
|
|
shopCart: [],
|
|
show: false,
|
|
buyAmount: 0
|
|
},
|
|
/**
|
|
* 组件的方法列表
|
|
*/
|
|
methods: {
|
|
//添加商品
|
|
addCart(event) {
|
|
var _self = this
|
|
let goods = event.currentTarget.dataset.goods
|
|
goods.buyNum = 0
|
|
if (goods != null) {
|
|
//判断是否存在
|
|
var isExist = false
|
|
for (var i = 0; i < _self.data.shopCart.length; ++i) {
|
|
var it = _self.data.shopCart[i]
|
|
if (it.goodsId == goods.goodsId) {
|
|
//判断是否超出库存
|
|
if (it.buyNum < goods.goodsTotal) {
|
|
it.buyNum += 1
|
|
} else {
|
|
wx.showToast({
|
|
title: '超出库存范围',
|
|
icon: 'error'
|
|
})
|
|
}
|
|
isExist = true
|
|
break;
|
|
}
|
|
}
|
|
if (!isExist) {
|
|
//不存在
|
|
goods.buyNum = 1
|
|
_self.data.shopCart = _self.data.shopCart.concat(goods)
|
|
}
|
|
let amount = 0
|
|
_self.data.shopCart.forEach(it => {
|
|
amount += it.buyNum
|
|
})
|
|
_self.setData({
|
|
shopCart: _self.data.shopCart,
|
|
buyAmount: amount
|
|
})
|
|
app.globalData.shopCart = _self.data.shopCart
|
|
this.triggerEvent('refreshShopCart')
|
|
}
|
|
},
|
|
//显示购物车列表
|
|
showCart() {
|
|
let cart = {
|
|
shopId: this.data.shopId,
|
|
shopCart: this.data.shopCart
|
|
}
|
|
this.triggerEvent('showcart', cart)
|
|
},
|
|
//立即订购
|
|
nowOrder(event) {
|
|
if (this.data.shopCart.length > 0) {
|
|
wx.navigateTo({
|
|
url: '/pages/shop/product/affirmorder?shopId=' + this.data.shopId,
|
|
})
|
|
} else {
|
|
wx.showToast({
|
|
title: '购物车空空如也',
|
|
icon: 'error'
|
|
})
|
|
}
|
|
},
|
|
onClose() {
|
|
this.setData({
|
|
show: false
|
|
})
|
|
},
|
|
//外部添加商品
|
|
listAddToCart(goods) {
|
|
let _self = this
|
|
goods.buyNum = 0
|
|
if (goods != null) {
|
|
//判断是否存在
|
|
var isExist = false
|
|
for (var i = 0; i < _self.data.shopCart.length; ++i) {
|
|
var it = _self.data.shopCart[i]
|
|
if (it.goodsId == goods.goodsId) {
|
|
//判断是否超出库存
|
|
if (it.buyNum < goods.goodsTotal) {
|
|
it.buyNum += 1
|
|
} else {
|
|
wx.showToast({
|
|
title: '超出库存范围',
|
|
icon: 'error'
|
|
})
|
|
}
|
|
isExist = true
|
|
break;
|
|
}
|
|
}
|
|
if (!isExist) {
|
|
//不存在
|
|
goods.buyNum = 1
|
|
_self.data.shopCart = _self.data.shopCart.concat(goods)
|
|
}
|
|
let amount = 0
|
|
_self.data.shopCart.forEach(it => {
|
|
amount += it.buyNum
|
|
})
|
|
_self.setData({
|
|
shopCart: _self.data.shopCart,
|
|
buyAmount: amount
|
|
})
|
|
app.globalData.shopCart = _self.data.shopCart
|
|
}
|
|
},
|
|
//移除商品
|
|
listRemoveCart(goods) {
|
|
let _self = this
|
|
goods.buyNum = 0
|
|
if (goods != null) {
|
|
//判断是否存在
|
|
var isExist = false
|
|
for (var i = 0; i < _self.data.shopCart.length; ++i) {
|
|
var it = _self.data.shopCart[i]
|
|
if (it.goodsId == goods.goodsId) {
|
|
//判断是否超出库存
|
|
if (it.buyNum < goods.goodsTotal) {
|
|
it.buyNum -= 1
|
|
} else {
|
|
wx.showToast({
|
|
title: '超出库存范围',
|
|
icon: 'error'
|
|
})
|
|
}
|
|
isExist = true
|
|
break;
|
|
}
|
|
}
|
|
let amount = 0
|
|
_self.data.shopCart.forEach(it => {
|
|
amount += it.buyNum
|
|
})
|
|
_self.setData({
|
|
shopCart: _self.data.shopCart,
|
|
buyAmount: amount
|
|
})
|
|
if (amount == 0) {
|
|
app.globalData.shopCart = []
|
|
_self.setData({
|
|
shopCart: []
|
|
})
|
|
} else {
|
|
app.globalData.shopCart = _self.data.shopCart
|
|
}
|
|
}
|
|
},
|
|
//刷新
|
|
refreshCart() {
|
|
let shopCartData = app.globalData.shopCart
|
|
this.setData({
|
|
shopCart: []
|
|
})
|
|
var amount = 0
|
|
if (shopCartData.length > 0) {
|
|
shopCartData.forEach(it => {
|
|
if (it.shopId == this.properties.shopId) {
|
|
this.data.shopCart = this.data.shopCart.concat(it)
|
|
amount += it.buyNum
|
|
}
|
|
})
|
|
}
|
|
this.setData({
|
|
shopId: this.properties.shopId,
|
|
shopCart: this.data.shopCart,
|
|
buyAmount: amount
|
|
})
|
|
}
|
|
}
|
|
}) |