微信购物车案例
1.读取本地缓存的购物车数据 微信缓存数据
2. 渲染出读取的购物车物品实例
const carts = wx.getStorageSync('carts')
this.setData({
carts,
})
3. 添加新的物品
3.1 商品已存在 数量+1
3.2 商品未存在 重新存储最新的数据
addChart() {
let Carts = wx.getStorageSync('carts') || []
let index = Carts.findIndex((v) => v.goods_id == this.data.goods_id)
if (index === -1) {
this.GoodsInfo.num = 1
this.GoodsInfo.cheackd = true
Carts.push(this.GoodsInfo)
} else {
Carts[index].num++
}
wx.setStorageSync('carts', Carts)
let allcheack = Carts.every(item=>item.cheackd)
wx.setStorageSync('allcheack',allcheack)
wx.showToast({
title: '添加成功!',
icon: 'success',
duration: 1500,
mask: true,
})
},
4.全选与反选
allcheacks(){
let newdata = this.data.carts
this.setData({
allcheack:!this.data.allcheack
})
if(!this.data.allcheack){
newdata.forEach((item)=>{
item.cheackd = false
})
this.setData({
totalMoenys:0
})
}else{
newdata.forEach((item)=>{
item.cheackd = true
})
}
let totalnum = newdata.filter((item) => item.cheackd == true).length
this.setData({
carts:newdata,
totalnum
})
wx.setStorageSync('carts', newdata)
this.totallMoney()
wx.setStorageSync('allcheack', this.data.allcheack)
},
5. 计算金额和显示数量
totallMoney(){
let totalMoenys = this.data.carts.map(item=>{
return {cheackd:item.cheackd , total:(item.num*item.goods_price)}
}).filter(item =>(item.cheackd == true)).reduce((v,c)=> v + c.total,0)
this.setData({
totalMoenys,
})
},
6. 加1 或减 1
eidit(e){
let {type}= e.currentTarget.dataset
let {cid}= e.currentTarget.dataset
let newCarts = this.data.carts
newCarts.forEach(item=>{
if(item.goods_id == cid){
if(type == 1){
item.num >= item.goods_number ? item.num =item.goods_number :item.num++
this.totallMoney()
} else if(type == 2){
item.num <= 1 ? (item.num = 1) : item.num--
this.totallMoney()
}
}
this.setData({
carts:newCarts
})
wx.setStorageSync('carts', newCarts)
})
},
7. 选择某一个支付
cheackOne(e){
let {cid} = e.currentTarget.dataset
let newCarts = this.data.carts
newCarts.forEach(item=>{
if(item.goods_id == cid){
item.cheackd = !item.cheackd
}
})
let totalnum = newdata.filter((item) => item.cheackd == true).length
let allcheack = newCarts.every(item=>item.cheackd)
this.setData({
carts:newCarts,
allcheack,
totalnum
})
this.totallMoney()
wx.setStorageSync('carts', newCarts)
wx.setStorageSync('allcheack', this.data.allcheack)
},
8.删除
deleted(e) {
deleted(e) {
let that = this
wx.showModal({
title: ' 删除提示!',
content: '确定要删除此商品?',
showCancel: true,
cancelText: '取消',
cancelColor: '#000000',
confirmText: '确定',
confirmColor: '#3CC51F',
success: (result) => {
if (result.confirm) {
let { cid } = e.currentTarget.dataset
let newCarts = this.data.carts
for (let i = 0; i < newCarts.length; i++) {
if (newCarts[i].goods_id == cid) {
newCarts.splice(i, 1)
break
}
}
let totalnum = newdata.filter((item) => item.cheackd == true).length
this.setData({
carts: newCarts,
totalnum
})
this.totallMoney()
wx.setStorageSync('carts', newCarts)
let allcheack = newCarts.length!=0?newCarts.every(item=>item.cheackd):false
wx.setStorageSync('allcheack',allcheack)
this.setData({
allcheack
})
},
}
},
})
},