Files
CompanyProject/衣莎项目/源码/YiShaXiYi/mall/shopcart/shopcart.js
2024-09-27 01:32:49 +08:00

183 lines
4.4 KiB
JavaScript

// mall/shopcart/shopcart.js
const app = getApp();
const regeneratorRuntime = app.loadAsync()
const model = app.loadModel("index")
const http = app.loadHttp()
const util = app.loadUtils("util")
Page({
/**
* 组件的初始数据
*/
data: {
system: app.globalData.system, //设备相关信息
pageTitle: "购物车", //头部标题
isBack: true,//显示返回
right: [
{
text: '删除',
style: 'background-color: #F4333C; color: white',
}],
cartList: [],//购物车
total: 0,//购物车商品总价格
allShow: false,
isAllShow: false,//判断是否已经全选
postage:{},//邮费
},
//获取本地缓存中的购物车商品
async getCart() {
let cart = await model.shopCart.getCart()
cart.map((v, i) => {
cart[i].isShow = false
cart[i].allMon = (v.price * v.num).toFixed(2)
})
this.setData({
cartList: cart
})
this.getTotalPrice()
},
// 增加和减少商品数量
async handleChange1(e) {
let item = e.currentTarget.dataset.item
let value = e.detail.value
let cartList = this.data.cartList
item.num = value
// 更新购物车,返回最新购物车
let newCart = await model.shopCart.updateCart(item)
newCart.map((v, i) => {
for (let j = 0; j < cartList.length; j++) {
// 判断对应数据,来修改对应的isShow
if (util.Compare(v, cartList[j], "num")) {
newCart[i].isShow = cartList[j].isShow
newCart[i].allMon = (v.price * v.num).toFixed(2)
}
}
})
this.setData({
cartList: newCart
})
this.getTotalPrice()
},
// 侧滑删除事件
async onClick(e) {
let that = this
let item = e.currentTarget.dataset.item
item.num = 0
// 更新购物车,返回最新购物车
let newCart = await model.shopCart.updateCart(item)
newCart.map((v, i) => {
newCart[i].allMon = (v.price * v.num).toFixed(2)
})
that.setData({
cartList: newCart
})
that.getTotalPrice()
model.msg.dataMsg({
content: "删除成功",
type: "success"
})
},
// 计算选中的总价格
getTotalPrice() {
let cartList = this.data.cartList
let total = 0
cartList.map((v, i) => {
if (v.isShow) {
total += v.num * v.price
}
})
this.setData({
total: total.toFixed(2)
})
// 总价格同步到全局变量中
app.globalData.temCart.total = total.toFixed(2)
},
// 商品的勾选事件
checklist(e) {
let that = this
let index = e.currentTarget.dataset.index
let cartList = this.data.cartList
let allShow = false //如果为true说明全部数据都全选
let arr1 = []
if (cartList.length <= 0) {
return
}
// 如果为-1说明是全选
if (index == -1 && !this.data.isAllShow) {
this.setData({
isAllShow: true
})
cartList.map((v, i) => {
cartList[i].isShow = true
})
} else if (this.data.isAllShow && index == -1) {
this.setData({
isAllShow: false
})
cartList.map((v, i) => {
cartList[i].isShow = false
})
} else {
this.setData({
isAllShow: false
})
cartList[index].isShow = !cartList[index].isShow
}
// 每次勾选时判断是否已经全选
cartList.map((v, i) => {
arr1.push(v.isShow)
})
if (arr1.indexOf(false) == -1) {
allShow = true
}
this.setData({
cartList: cartList,
allShow: allShow
})
this.getTotalPrice()
},
// 商品提交事件
async placeOrder() {
let isLogin = await util.isLoginJump()
if (isLogin == 1) return;
let cartList = this.data.cartList
let newCart = []
cartList.map((v, i) => {
if (v.isShow) {
newCart.push(v)
}
})
app.globalData.temCart.allCart = newCart
if (newCart.length <= 0) {
return
}
wx.navigateTo({
url: "/mall/placeOrder/placeOrder"
})
},
// 获取当前邮费配置
async postage() {
let res = await http.http.request1({ url: `${http.api.postage}`, method: "POST", data: { type: 1 } })
if (res.data.status) {
this.setData({
postage: res.data.data
})
}
},
/**
* 生命周期函数--监听页面加载
*/
onLoad() {
this.postage()
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
this.setData({
total: 0,
allShow: false
})
this.getCart()
},
})