353 lines
8.0 KiB
JavaScript
353 lines
8.0 KiB
JavaScript
// pages/cart/cart.js
|
|
const app = getApp();
|
|
Page({
|
|
|
|
/**
|
|
* 页面的初始数据
|
|
*/
|
|
data: {
|
|
checkNum: 0,//勾选数量
|
|
checkAll: false,//全选
|
|
cartData: [],
|
|
login: false,
|
|
price:0,
|
|
|
|
},
|
|
|
|
//商品详情
|
|
goodsDetails(e) {
|
|
let id = e.currentTarget.dataset.id;
|
|
wx.navigateTo({
|
|
url: '/pages/goodsDetails/goodsDetails?goodsId=' + id,
|
|
})
|
|
},
|
|
|
|
|
|
//去购物
|
|
goShop(e) {
|
|
wx.switchTab({
|
|
url: '/pages/classify/classify'
|
|
})
|
|
},
|
|
onLoad: function (options) {
|
|
if (options.pId) {
|
|
wx.setStorage({
|
|
data: options.pId,
|
|
key: 'pId',
|
|
})
|
|
}
|
|
},
|
|
|
|
onShow: function () {
|
|
wx.getStorage({
|
|
key: 'user',
|
|
success: s => {
|
|
wx.showLoading({
|
|
title: '加载中..',
|
|
})
|
|
wx.request({
|
|
url: app.globalData.ip + '/wisdommining/api/goods/getCart',
|
|
method: 'POST',
|
|
header: {
|
|
"content-type": "application/x-www-form-urlencoded",
|
|
},
|
|
data: {
|
|
userId: s.data.id
|
|
},
|
|
success: res => {
|
|
wx.hideLoading()
|
|
console.log(res)
|
|
if (res.data.code == 1) {
|
|
this.setData({
|
|
cartData: res.data.value,
|
|
})
|
|
//判断全选 数量
|
|
this.judgeCheckAll();
|
|
if (res.data.value.length == 0) {
|
|
app.globalData.cartNum = '';
|
|
wx.hideTabBarRedDot({
|
|
index: 3
|
|
})
|
|
} else {
|
|
app.globalData.cartNum = res.data.value.length.toString()
|
|
//购物车数量显示
|
|
wx.setTabBarBadge({
|
|
index: 3,
|
|
text: app.globalData.cartNum
|
|
})
|
|
}
|
|
} else {
|
|
wx.showModal({
|
|
title: '提示!',
|
|
content: res.data.message,
|
|
showCancel: false,
|
|
})
|
|
}
|
|
}
|
|
})
|
|
}
|
|
})
|
|
},
|
|
|
|
//结算 recommendedBulyPrice
|
|
pay() {
|
|
let goodsList = this.data.cartData;
|
|
let goods = [];
|
|
|
|
for (let i = 0; i < goodsList.length; i++) {
|
|
goodsList[i].wisdGoods.wisdGoodsSpec.price = goodsList[i].wisdGoods.wisdGoodsSpec.specBulyPrice == null ? goodsList[i].wisdGoods.wisdGoodsSpec.specPrice : goodsList[i].wisdGoods.wisdGoodsSpec.specBulyPrice
|
|
if (goodsList[i].check) {
|
|
goods.push({
|
|
isDiscount: goodsList[i].wisdGoods.isDiscount,
|
|
goodsPhoto: goodsList[i].wisdGoods.goodsPhoto,
|
|
goodsName: goodsList[i].wisdGoods.goodsName,
|
|
goodsId: goodsList[i].wisdGoods.id,
|
|
num: goodsList[i].goodsNum,
|
|
specs: goodsList[i].wisdGoods.wisdGoodsSpec
|
|
})
|
|
}
|
|
}
|
|
if (goods.length == 0) {
|
|
return
|
|
}
|
|
wx.navigateTo({
|
|
url: '/pages/cartPay/cartPay?goods=' + JSON.stringify(goods) + '&isCar=1',
|
|
})
|
|
},
|
|
|
|
//加 减
|
|
add(e) {
|
|
let id = e.currentTarget.dataset.id;
|
|
let type = e.currentTarget.dataset.type;
|
|
let index = e.currentTarget.dataset.index;
|
|
let cartData = this.data.cartData;
|
|
if (type == 2 && cartData[index].goodsNum <= 1) {
|
|
wx.showToast({
|
|
title: '不能再减了~',
|
|
icon: 'none',
|
|
duration: 1000
|
|
})
|
|
return
|
|
}
|
|
wx.showLoading({
|
|
title: '加载中..',
|
|
})
|
|
wx.request({
|
|
url: app.globalData.ip + '/wisdommining/api/goods/updateCartNum',
|
|
method: 'POST',
|
|
header: {
|
|
"content-type": "application/x-www-form-urlencoded",
|
|
},
|
|
data: {
|
|
id: id,
|
|
type: type,
|
|
},
|
|
success: res => {
|
|
wx.hideLoading()
|
|
console.log(res)
|
|
if (res.data.code == 1) {
|
|
if(type==1){
|
|
cartData[index].goodsNum++
|
|
}else{
|
|
cartData[index].goodsNum--
|
|
}
|
|
this.setData({
|
|
cartData:cartData
|
|
})
|
|
//判断全选 数量
|
|
this.judgeCheckAll();
|
|
} else {
|
|
wx.showModal({
|
|
title: '提示!',
|
|
content: res.data.message,
|
|
showCancel: false,
|
|
})
|
|
}
|
|
}
|
|
})
|
|
},
|
|
|
|
//全选
|
|
checkAll() {
|
|
this.setData({
|
|
checkAll: !this.data.checkAll,
|
|
})
|
|
let cartData = this.data.cartData;
|
|
for (let i = 0; i < cartData.length; i++) {
|
|
if (this.data.checkAll) {
|
|
cartData[i].check = true;
|
|
} else {
|
|
cartData[i].check = false;
|
|
}
|
|
}
|
|
this.setData({
|
|
cartData: cartData,
|
|
|
|
// total: total.toFixed(2),
|
|
// totalIntegral: total,
|
|
})
|
|
//判断全选 数量
|
|
this.judgeCheckAll();
|
|
},
|
|
|
|
//勾选
|
|
check(e) {
|
|
let index = e.currentTarget.dataset.index;
|
|
let cartData = this.data.cartData;
|
|
cartData[index].check = !cartData[index].check;
|
|
this.setData({
|
|
cartData: cartData,
|
|
})
|
|
//判断全选 数量
|
|
this.judgeCheckAll();
|
|
},
|
|
|
|
|
|
|
|
//判断全选 数量
|
|
judgeCheckAll() {
|
|
|
|
let cartData = this.data.cartData;
|
|
let all = 0;
|
|
let checkNum = 0;
|
|
let price = 0;
|
|
for (let i = 0; i < cartData.length; i++) {
|
|
if (!cartData[i].check) {
|
|
//有未选中
|
|
all = 1;
|
|
} else {
|
|
|
|
|
|
let p = cartData[i].wisdGoods.wisdGoodsSpec.specBulyPrice==null?cartData[i].wisdGoods.wisdGoodsSpec.specPrice:cartData[i].wisdGoods.wisdGoodsSpec.specBulyPrice
|
|
checkNum++
|
|
|
|
|
|
price = price + parseFloat((cartData[i].goodsNum*p ).toFixed(2))
|
|
console.log(p)
|
|
|
|
}
|
|
}
|
|
this.setData({
|
|
checkNum: checkNum,
|
|
price:price
|
|
})
|
|
if (all === 0) {
|
|
this.setData({
|
|
checkAll: true,
|
|
})
|
|
} else {
|
|
this.setData({
|
|
checkAll: false,
|
|
})
|
|
}
|
|
},
|
|
|
|
//删除
|
|
del() {
|
|
if (this.data.checkNum == 0) {
|
|
wx.showToast({
|
|
title: '请先选择商品~',
|
|
icon: 'none',
|
|
duration: 500
|
|
})
|
|
return
|
|
}
|
|
wx.showModal({
|
|
title: '提示',
|
|
content: '您确认删除当前购物车中商品么?',
|
|
success: c => {
|
|
if (c.confirm) {
|
|
let shopCartId = '';
|
|
for (var i = 0; i < this.data.cartData.length; i++) {
|
|
if (this.data.cartData[i].check) {
|
|
shopCartId = shopCartId + this.data.cartData[i].id + ',';
|
|
}
|
|
}
|
|
// shopCartId = shopCartId.substr(1);
|
|
console.log(shopCartId)
|
|
wx.showLoading({
|
|
title: '删除中..',
|
|
mask: true,
|
|
})
|
|
wx.request({
|
|
url: app.globalData.ip + '/wisdommining/api/goods/delCart',
|
|
method: 'DELETE',
|
|
header: {
|
|
"content-type": "application/x-www-form-urlencoded",
|
|
},
|
|
data: {
|
|
ids: shopCartId,
|
|
},
|
|
success: res => {
|
|
console.log(res)
|
|
wx.hideLoading()
|
|
if (res.data.code == 1) {
|
|
wx.showToast({
|
|
title: '删除成功',
|
|
success: e => {
|
|
setTimeout(() => {
|
|
this.onShow();
|
|
}, 1500)
|
|
}
|
|
})
|
|
} else {
|
|
wx.showModal({
|
|
content: res.data.message,
|
|
showCancel: false,
|
|
})
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
})
|
|
},
|
|
|
|
|
|
/**
|
|
* 生命周期函数--监听页面初次渲染完成
|
|
*/
|
|
onReady: function () {
|
|
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面隐藏
|
|
*/
|
|
onHide: function () {
|
|
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面卸载
|
|
*/
|
|
onUnload: function () {
|
|
|
|
},
|
|
|
|
/**
|
|
* 页面相关事件处理函数--监听用户下拉动作
|
|
*/
|
|
onPullDownRefresh: function () {
|
|
|
|
},
|
|
|
|
/**
|
|
* 页面上拉触底事件的处理函数
|
|
*/
|
|
onReachBottom: function () {
|
|
|
|
},
|
|
|
|
/**
|
|
* 用户点击右上角分享
|
|
*/
|
|
onShareAppMessage: function () {
|
|
app.share();
|
|
return {
|
|
path: '/pages/index/index?pId=' + app.globalData.userId,
|
|
title: '三品慧采小程序等你来~'
|
|
}
|
|
},
|
|
|
|
}) |