import { request } from "../../../request/index";
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({
data: {
disabled:false,
result: [],
checked:false,
valid_goods_list:[],
invalid_goods_list:[],
num:0,
tag:0,
total_price:0,
dataList:[],
deldataList:[]
},
onChange2(event) {
this.setData({
checked: event.detail,
});
if(event.detail){
this.setData({
result:this.data.dataList,
tag:1
})
}else{
this.setData({
result:[],
tag:0
})
}
this.selectCart();
this.getPrice()
},
getPrice(){
console.log(123)
let that = this;
let totalPrice = 0;
let num = 0;
function checkAdult(e) {
return that.data.result.includes(String(e.cart_id))
}
this.data.valid_goods_list.filter(checkAdult).forEach((e) => {
num += e.goods_num;
totalPrice += (e.goods_num * e.market_price)
})
this.setData({
total_price:parseFloat(totalPrice).toFixed(2),
num
})
},
onStepper(e){
console.log(e)
this.setData({
disabled:true
});
let index = e.currentTarget.dataset.index;
console.log(this.data.valid_goods_list[index].goods_num,e.detail)
if(this.data.valid_goods_list[index].goods_num > e.detail){
this.minusToCart(e.currentTarget.dataset.cart_id).then((res) => {
this.data.valid_goods_list[index].goods_num = e.detail;
this.setData({
valid_goods_list:this.data.valid_goods_list
})
this.setData({
disabled:false
})
this.getPrice()
}).catch(()=>{
this.setData({
disabled:false
})
})
}else{
this.addToCart(e.currentTarget.dataset.cart_id).then((res) => {
this.data.valid_goods_list[index].goods_num = e.detail;
this.setData({
valid_goods_list:this.data.valid_goods_list
})
this.setData({
disabled:false
})
this.getPrice()
}).catch(()=>{
this.setData({
disabled:false
})
})
}
},
onChange1(event) {
this.setData({
result: event.detail,
});
console.log(event)
if(event.detail.length == this.data.dataList.length){
this.setData({
checked:true,
tag:1
})
}else{
this.setData({
checked:false,
tag:0
})
}
this.getPrice()
},
onChange(event) {
let cart_id = event.currentTarget.dataset.cart_id;
let is_select = (this.data.result.includes(String(cart_id)) ? 1 : 0)
this.checkOneCart(cart_id,is_select)
},
onLoad: function (options) {
},
checkOneCart(cart_id,is_select){
request({
url: 'cart/checkOneCart',
data: {
token:wx.getStorageSync('token'),
cart_id,
is_select
}
}).then(res => {
if (res.data.status == 1) {
} else {
wx.showToast({
title: res.data.msg,
icon: 'none',
duration: 1500
})
}
})
},
selectCart(){
wx.showLoading({
title: '加载中...',
})
request({
url: 'cart/selectCart',
data: {
token:wx.getStorageSync('token'),
areaId:wx.getStorageSync('areaId'),
tag:this.data.tag
}
}).then(res => {
if (res.data.status == 1) {
wx.hideLoading({
complete: (res) => {},
})
} else {
wx.showToast({
title: res.data.msg,
icon: 'none',
duration: 1500
})
}
})
},
onClose(event) {
const { position, instance } = event.detail;
console.log(event)
switch (position) {
case 'left':
case 'cell':
instance.close();
break;
case 'right':
Dialog.confirm({
message: '确定删除吗?',
}).then(() => {
instance.close();
});
break;
}
},
sc(e){
console.log(e);
Dialog.confirm({
message: '确定移入收藏夹吗?',
}).then(() => {
request({
url: 'index/collect',
data: {
type:4,
token:wx.getStorageSync('token'),
id:e.currentTarget.dataset.id
}
}).then(res => {
if (res.data.status == 1) {
wx.showToast({
title: res.data.msg,
duration: 1500
})
} else {
wx.showToast({
title: res.data.msg,
icon: 'none',
duration: 1500
})
}
})
}).catch(() => {
wx.showToast({
title: '取消收藏',
icon:"none"
})
});
},
del(e){
console.log(e);
Dialog.confirm({
message: '确定删除吗?',
}).then(() => {
request({
url: 'cart/cartDelete',
data: {
token:wx.getStorageSync('token'),
cart_id:e.currentTarget.dataset.cart_id
}
}).then(res => {
if (res.data.status == 1) {
this.cartList()
} else {
wx.showToast({
title: res.data.msg,
icon: 'none',
duration: 1500
})
}
})
}).catch(() => {
wx.showToast({
title: '取消删除',
icon:"none"
})
});
},
cartList(){
wx.showLoading({
title: '加载中...',
})
request({
url: 'cart/cartList',
data: {
token:wx.getStorageSync('token'),
areaId:wx.getStorageSync('areaId')
}
}).then(res => {
if (res.data.status == 1) {
this.data.dataList = [];
this.data.deldataList = [];
res.data.data.valid_goods_list.forEach(element => {
this.data.dataList.push(String(element.cart_id))
})
res.data.data.invalid_goods_list.forEach(element => {
this.data.deldataList.push(String(element.cart_id))
})
this.setData({
dataList:this.data.dataList,
deldataList:this.data.deldataList,
valid_goods_list:res.data.data.valid_goods_list,
invalid_goods_list:res.data.data.invalid_goods_list,
num:res.data.data.num,
tag:res.data.data.tag,
total_price:res.data.data.total_price,
})
if(res.data.data.tag == 1){
this.setData({
checked:true,
result:this.data.dataList
})
}else{
res.data.data.valid_goods_list.forEach(element => {
if(element.is_select){
this.data.result.push(String(element.cart_id))
}
})
this.setData({
result:this.data.result
})
}
wx.hideLoading({
complete: (res) => {},
})
wx.stopPullDownRefresh({
complete: (res) => {},
})
} else {
wx.showToast({
title: res.data.msg,
icon: 'none',
duration: 1500
})
}
})
},
clear(){
Dialog.confirm({
message: '确定删除吗?',
}).then(() => {
request({
url: 'cart/cartDelete',
data: {
token:wx.getStorageSync('token'),
cart_id:this.data.deldataList
}
}).then(res => {
if (res.data.status == 1) {
this.cartList()
} else {
wx.showToast({
title: res.data.msg,
icon: 'none',
duration: 1500
})
}
})
}).catch(() => {
wx.showToast({
title: '取消删除',
icon:"none"
})
});
},
addToCart(cart_id){
return new Promise((r,j)=>{
request({
url: 'cart/addToCart',
data: {
token:wx.getStorageSync('token'),
cart_id
}
}).then(res => {
if (res.data.status == 1) {
r(res)
} else {
j()
wx.showToast({
title: res.data.msg,
icon: 'none',
duration: 1500
})
}
})
});
},
minusToCart(cart_id){
return new Promise((r,j)=>{ request({
url: 'cart/minusToCart',
data: {
token:wx.getStorageSync('token'),
cart_id
}
}).then(res => {
if (res.data.status == 1) {
r(res)
} else {
j()
wx.showToast({
title: res.data.msg,
icon: 'none',
duration: 1500
})
}
})
});
},
gotoOrder(){
if(this.data.num <= 0){
wx.showToast({
title: '请至少选择一件商品',
icon:"none"
})
}else{
wx.navigateTo({
url: '../../ShopMall/order/order?cart_id=' + JSON.stringify(this.data.result),
})
}
},
onReady: function () {
},
onShow: function () {
this.cartList()
},
onHide: function () {
},
onUnload: function () {
},
onPullDownRefresh: function () {
this.setData({
valid_goods_list:[],
invalid_goods_list:[],
num:0,
tag:0,
total_price:0,
result:[],
dataList:[]
})
this.cartList()
},
onReachBottom: function () {
},
onShareAppMessage: function () {
}
})