|
|
|
@ -6,9 +6,182 @@ Page({
|
|
|
|
|
* 页面的初始数据
|
|
|
|
|
*/
|
|
|
|
|
data: {
|
|
|
|
|
cartlistsee:[]
|
|
|
|
|
cartlistsee:[], //该用户购物车的数组
|
|
|
|
|
total_all_price: 0, //商品的总价格
|
|
|
|
|
all_shop: 0, //所选商品的id
|
|
|
|
|
shop_num: 0, //所选商品的数量
|
|
|
|
|
good_one: [], //单个商品的数据
|
|
|
|
|
checked: false, //是否为全选
|
|
|
|
|
is_shop: false, //是否有商品
|
|
|
|
|
shopData: null
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
/**点击单个多选框 */
|
|
|
|
|
allCheckbox: function(tap) {
|
|
|
|
|
var a = 0 //设置初始总价格
|
|
|
|
|
var index = tap.currentTarget.dataset.index
|
|
|
|
|
console.log(index)//获取索引值
|
|
|
|
|
let good = this.data.cartlistsee //获取购物车列表
|
|
|
|
|
const checks = good[index].checks; //获取当前商品的选中状态
|
|
|
|
|
good[index].checks = !checks //改变当前商品的状态
|
|
|
|
|
console.log(checks)
|
|
|
|
|
var all_shop_1 = parseInt(good[index]._id) //获取商品的购物车id
|
|
|
|
|
var shop_num_1 = good[index].num //获取商品的数量
|
|
|
|
|
this.setData({
|
|
|
|
|
cartlistsee: good
|
|
|
|
|
});
|
|
|
|
|
this.getTotalPrice() //重新获取总价格
|
|
|
|
|
/*设置全选 */
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
/**点击全选 */
|
|
|
|
|
setChecked: function() {
|
|
|
|
|
let checked = this.data.checked; //是否为全选状态
|
|
|
|
|
checked = !checked; //改变状态
|
|
|
|
|
let cartlistsee = this.data.cartlistsee;
|
|
|
|
|
for (let i = 0; i < cartlistsee.length; i++) {
|
|
|
|
|
cartlistsee[i].checks = checked; // 改变所有商品状态
|
|
|
|
|
}
|
|
|
|
|
this.setData({
|
|
|
|
|
checked: checked, //更新全选状态
|
|
|
|
|
cartlistsee: cartlistsee //更新所有商品的状态
|
|
|
|
|
});
|
|
|
|
|
this.getTotalPrice(); //重新获取总价格
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
//计算商品总价格
|
|
|
|
|
getTotalPrice() {
|
|
|
|
|
let good = this.data.cartlistsee; // 获取购物车列表
|
|
|
|
|
let total = 0;
|
|
|
|
|
for (let i = 0; i < good.length; i++) { // 循环列表得到每个数据
|
|
|
|
|
if (good[i].checks) { // 判断选中才会计算价格
|
|
|
|
|
total += good[i].num * good[i].price; // 所有价格加起来
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.setData({ // 最后赋值到data中渲染到页面
|
|
|
|
|
good: good,
|
|
|
|
|
total_all_price: total.toFixed(2),
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
//点击结算订单
|
|
|
|
|
toBuy: function(tap) {
|
|
|
|
|
var that = this;
|
|
|
|
|
let good = this.data.cartlistsee; //所有商品数据
|
|
|
|
|
let user_info = wx.getStorageSync('user_data')
|
|
|
|
|
if (that.data.total_all_price == 0) { //判断是否选择了商品,这里我是判断总价格
|
|
|
|
|
wx.showToast({
|
|
|
|
|
title: '请选择商品',
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
this.data.good_one = []; //重置数组
|
|
|
|
|
for (var i = 0; i < this.data.cartlistsee.length; i++) { //循环购物车中的商品
|
|
|
|
|
if (good[i].checks == true) {
|
|
|
|
|
var good_one1 = [good[i].cart_id, good[i].num];
|
|
|
|
|
this.data.good_one.push(good_one1) //将数据添加到数组里边
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}}},
|
|
|
|
|
|
|
|
|
|
// 添加按钮被点击
|
|
|
|
|
addButtonClick: function(event) {
|
|
|
|
|
var that = this;
|
|
|
|
|
const index = event.currentTarget.id;
|
|
|
|
|
console.log(index)//获取缓存里面的用户信息
|
|
|
|
|
let cartlistsee = this.data.cartlistsee;//购物车所有的商品数据
|
|
|
|
|
console.log(cartlistsee)
|
|
|
|
|
console.log(cartlistsee[index])
|
|
|
|
|
let num =cartlistsee[index].num//获取购买数量
|
|
|
|
|
num=num+1
|
|
|
|
|
cartlistsee[index].num= num;
|
|
|
|
|
that.setData({
|
|
|
|
|
cartlistsee:cartlistsee //更新商品数据
|
|
|
|
|
})
|
|
|
|
|
console.log(cartlistsee)
|
|
|
|
|
db.collection('user')
|
|
|
|
|
.doc('eda7dd506371adb300c89bc66afc4601') //还需修改
|
|
|
|
|
.update(
|
|
|
|
|
{
|
|
|
|
|
data:{
|
|
|
|
|
['cartlist.'+[index]]:{num}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
reduceButtonClick: function(event) {
|
|
|
|
|
var that = this;
|
|
|
|
|
const index = event.currentTarget.id;
|
|
|
|
|
console.log(index)//获取缓存里面的用户信息
|
|
|
|
|
let cartlistsee = this.data.cartlistsee;//购物车所有的商品数据
|
|
|
|
|
console.log(cartlistsee)
|
|
|
|
|
console.log(cartlistsee[index])
|
|
|
|
|
let num =cartlistsee[index].num//获取购买数量
|
|
|
|
|
if(num == 1){
|
|
|
|
|
wx.showModal({
|
|
|
|
|
title: '提示',
|
|
|
|
|
content: '已是最后一件商品',
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
else{//如果商品数量不等于 1
|
|
|
|
|
num=num-1
|
|
|
|
|
cartlistsee[index].num= num;
|
|
|
|
|
that.setData({
|
|
|
|
|
cartlistsee:cartlistsee //更新商品数据
|
|
|
|
|
})
|
|
|
|
|
console.log(cartlistsee)
|
|
|
|
|
db.collection('user')//回调回云数据库
|
|
|
|
|
.doc('eda7dd506371adb300c89bc66afc4601') //还需修改
|
|
|
|
|
.update(
|
|
|
|
|
{
|
|
|
|
|
data:{
|
|
|
|
|
['cartlist.'+[index]]:{num}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
//删除单个商品
|
|
|
|
|
deleteGoods(event) {
|
|
|
|
|
var that = this;
|
|
|
|
|
const index = event.currentTarget.id;
|
|
|
|
|
console.log(index)
|
|
|
|
|
|
|
|
|
|
let cartlistsee = this.data.cartlistsee;//购物车所有的商品数据
|
|
|
|
|
let foodid=cartlistsee[index]._id
|
|
|
|
|
console.log(foodid)
|
|
|
|
|
wx.showModal({
|
|
|
|
|
title: '温馨提示',
|
|
|
|
|
content: '确定删除当前商品吗?',
|
|
|
|
|
confirmColor: "#f00",
|
|
|
|
|
success: (res) => {
|
|
|
|
|
if (res.confirm) {
|
|
|
|
|
cartlistsee.splice(index, 1);
|
|
|
|
|
this.setData({
|
|
|
|
|
cartlistsee:cartlistsee
|
|
|
|
|
})
|
|
|
|
|
db.collection('user')//回调回云数据库
|
|
|
|
|
.doc('eda7dd506371adb300c89bc66afc4601') //还需修改
|
|
|
|
|
.update(
|
|
|
|
|
{
|
|
|
|
|
data:{
|
|
|
|
|
['cartlist.'+[index]]:db.command.remove(),
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
db.collection('user')//回调回云数据库
|
|
|
|
|
.doc('eda7dd506371adb300c89bc66afc4601') //还需修改
|
|
|
|
|
.update(
|
|
|
|
|
{
|
|
|
|
|
data:{
|
|
|
|
|
['cartlist']:this.data.cartlistsee
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
console.log(cartlistsee)
|
|
|
|
|
},
|
|
|
|
|
/**
|
|
|
|
|
* 生命周期函数--监听页面加载
|
|
|
|
|
*/
|
|
|
|
@ -27,7 +200,9 @@ Page({
|
|
|
|
|
console.log(res.data.cartlist)
|
|
|
|
|
//再次显示数据
|
|
|
|
|
this.setData({
|
|
|
|
|
cartlistsee:res.data.cartlist
|
|
|
|
|
cartlistsee:res.data.cartlist,
|
|
|
|
|
total_all_price: 0,
|
|
|
|
|
checked: false
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
.catch(err => {
|
|
|
|
@ -35,6 +210,8 @@ Page({
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生命周期函数--监听页面初次渲染完成
|
|
|
|
|
*/
|
|
|
|
@ -67,14 +244,14 @@ Page({
|
|
|
|
|
* 页面相关事件处理函数--监听用户下拉动作
|
|
|
|
|
*/
|
|
|
|
|
onPullDownRefresh() {
|
|
|
|
|
|
|
|
|
|
console.log("下拉刷新...");
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 页面上拉触底事件的处理函数
|
|
|
|
|
*/
|
|
|
|
|
onReachBottom() {
|
|
|
|
|
|
|
|
|
|
console.log("上拉加载....");
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|