Files
xuan_wechat/pages/bag/bag.js
2024-03-04 15:13:17 +08:00

313 lines
8.7 KiB
JavaScript

var goodsIndex = '',
sellerIndex = ''
Page({
/**
* 页面的初始数据
*/
data: {
bags : [], // 购物车列表
isUser : false, // 是否登录
bagId : '',
allCheckbox : false,
bagNumber : 0,
allPrice : '0.00',
bagOrderLoading : false
},
/**
* 生命周期函数 - 页面显示
*/
onShow(){
let token = wx.getStorageSync('token') || null
this.setData({
isUser : token != null,
bagOrderLoading : false,
allCheckbox : false,
bagNumber : 0,
allPrice : '0.00',
bagId : '',
})
if(token != null){
wx.showLoading({
title: '加载中...',
mask : true
})
wx.$api.bag.list().then(res => {
res.data.map(val => {
val.shop.mallState = false
val.items.map(val => {
val.state = false
})
})
this.setData({
pagesLoding: false,
bags : res.data
})
wx.hideLoading()
})
}
},
/**
* 商品数量加减
*/
goodsNumber(e){
goodsIndex = e.currentTarget.dataset.goods
sellerIndex = e.currentTarget.dataset.seller
let goodsNumber = this.data.bags[sellerIndex].items[goodsIndex].qty
if (e.currentTarget.dataset.type == 'plus'){
goodsNumber = goodsNumber + 1
}else{
if (goodsNumber > 1){
goodsNumber = goodsNumber - 1
}else{
wx.showToast({
title : '商品数量不能小于1',
icon : 'none'
})
return
}
}
this.setnumNumber(goodsIndex, sellerIndex, goodsNumber)
},
/**
* 输入商品数量
*/
goodsNumberInput(e){
goodsIndex = e.currentTarget.dataset.goods
sellerIndex = e.currentTarget.dataset.seller
let setnumNumber = this.data.bags[sellerIndex].items[goodsIndex].number,
inputValue = Number()
if (e.detail.value > 0){
inputValue = Number(e.detail.value)
}else{
wx.showToast({
title : '商品数量不能小于1',
icon : 'none'
})
inputValue = Number(setnumNumber)
}
this.setnumNumber(goodsIndex, sellerIndex, inputValue)
},
/**
* 更新商品数量
*/
setnumNumber(goodsIndex, sellerIndex, setnumNumber){
wx.showLoading({
title: '加载中',
})
let atGoods = this.data.bags
wx.$api.bag.putNum(atGoods[sellerIndex].items[goodsIndex].cart_id, {
qty : setnumNumber,
sku_id: atGoods[sellerIndex].items[goodsIndex].sku_id
}).then(res => {
atGoods[sellerIndex].items[goodsIndex].qty = res.data
this.setData({
bags: atGoods
})
this.totalPrice()
wx.hideLoading()
})
},
/**
* 单选
*/
checkbox(e){
goodsIndex = e.currentTarget.dataset.goods
sellerIndex = e.currentTarget.dataset.seller
console.log(e.currentTarget.dataset)
let goodsList = this.data.bags,
checkbox = goodsList[sellerIndex].items[goodsIndex].state,
seller = goodsList[sellerIndex].items,
sellerLength = 0
goodsList[sellerIndex].items[goodsIndex].state = !checkbox
for (let i in seller){
if (seller[i].state){
sellerLength++
if (sellerLength == goodsList[sellerIndex].items.length){
goodsList[sellerIndex].shop.mallState = true
}
}else{
goodsList[sellerIndex].shop.mallState = false
this.setData({
allCheckbox: false
})
}
}
this.allCheckbox('checkbox')
this.setData({
bags: goodsList
})
this.totalPrice()
},
/**
* 店铺全选
*/
sellerCheckbox(e){
sellerIndex = e.currentTarget.dataset.seller
let goodsList = this.data.bags,
allCheckbox = this.data.allCheckbox,
seller = goodsList[sellerIndex].shop.mallState,
sellerLengh = 0
goodsList[sellerIndex].shop.mallState = !seller
for (let i in goodsList[sellerIndex].items){
goodsList[sellerIndex].items[i].state = !seller
}
for (let j in goodsList){
if (goodsList[j].shop.mallState){
sellerLengh++
if (sellerLengh == goodsList.length){
console.log('全选')
allCheckbox = true
}else{
console.log('取消全选')
allCheckbox = false
}
}
}
if(sellerLengh == 0) {allCheckbox = false}
this.allCheckbox('checkbox')
this.setData({
bags : goodsList,
allCheckbox : allCheckbox
})
this.totalPrice()
},
/**
* 全选
*/
allCheckbox(type){
console.log(type)
let goodsLenght = 0,
allCheckbox = this.data.allCheckbox,
goodsList = this.data.bags
if (type == 'checkbox'){
for (let j in goodsList) {
if (goodsList[j].shop.mallState) {
goodsLenght++
if (goodsLenght == goodsList.length) {
allCheckbox = true
}
}
}
}else{
allCheckbox = !allCheckbox
for (var i in goodsList){
goodsList[i].shop.mallState = allCheckbox
for (let k in goodsList[i].items){
goodsList[i].items[k].state = allCheckbox
}
}
}
this.setData({
allCheckbox : allCheckbox,
bags : goodsList
})
this.totalPrice()
},
/**
* 计算价格
*/
totalPrice() {
let bagNumber = 0,
allPrice = 0,
bagIdArr = [],
goodsList = this.data.bags
for (var i in goodsList){
for (let j in goodsList[i].items){
if (goodsList[i].items[j].state){
bagNumber = bagNumber + goodsList[i].items[j].qty
allPrice = allPrice + (goodsList[i].items[j].qty * goodsList[i].items[j].price)
bagIdArr.push(goodsList[i].items[j].cart_id)
}
}
}
this.setData({
bagNumber : bagNumber,
allPrice : allPrice.toFixed(2),
bagId : bagIdArr.join(",")
})
},
/**
* 购物车提交
*/
bagOrder(){
this.setData({
bagOrderLoading:true
})
if (this.data.bagId != ''){
wx.navigateTo({
url: './bagConfirm/bagConfirm?skuId=' + this.data.bagId
})
// setTimeout(() => {
// this.setData({
// bagOrderLoading: false
// })
// }, 2000)
}else{
wx.showToast({
title : '请选择结算商品',
icon : 'none'
})
this.setData({
bagOrderLoading: false
})
}
},
/**
* 菜单
*/
actionSheet(e){
goodsIndex = e.currentTarget.dataset.goods
sellerIndex = e.currentTarget.dataset.seller
let goodsList = this.data.bags,
cartId = this.data.bags[sellerIndex].items[goodsIndex].cart_id
wx.showActionSheet({
itemList: ['删除'],
success : res=>{
wx.showLoading({
title: '加载中',
})
wx.$api.bag.del(cartId).then(res => {
goodsList[sellerIndex].items.splice([goodsIndex],1)
if (goodsList[sellerIndex].items.length == 0){
goodsList.splice([sellerIndex], 1)
}
this.setData({
bags: goodsList
})
this.totalPrice()
wx.showToast({
title: '已删除'
})
})
}
})
},
})