You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
275 lines
6.6 KiB
275 lines
6.6 KiB
// pages/cart/cart.js
|
|
const db = wx.cloud.database({});
|
|
const app=getApp()
|
|
const userid=app.globalData.openid
|
|
Page({
|
|
|
|
/**
|
|
* 页面的初始数据
|
|
*/
|
|
data: {
|
|
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').
|
|
where({
|
|
"_openid":userid
|
|
}) //还需修改
|
|
.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')//回调回云数据库
|
|
.where({
|
|
"_openid":userid
|
|
}) //还需修改
|
|
.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')//回调回云数据库
|
|
. where({
|
|
"_openid":userid
|
|
}) //还需修改
|
|
.update(
|
|
{
|
|
data:{
|
|
['cartlist.'+[index]]:db.command.remove(),
|
|
}
|
|
})
|
|
|
|
db.collection('user')//回调回云数据库
|
|
.where({
|
|
"_openid":userid
|
|
}) //还需修改
|
|
.update(
|
|
{
|
|
data:{
|
|
['cartlist']:this.data.cartlistsee
|
|
}
|
|
}
|
|
)
|
|
}}
|
|
})
|
|
|
|
console.log(cartlistsee)
|
|
},
|
|
/**
|
|
* 生命周期函数--监听页面加载
|
|
*/
|
|
onLoad:function(options) {
|
|
|
|
const db = wx.cloud.database({
|
|
//这个是环境ID不是环境名称
|
|
env: 'cloud1-8g5wmepxce8a3b8a'
|
|
})
|
|
//2、开始查询数据了 news对应的是集合的名称
|
|
db.collection("user")
|
|
.where({
|
|
"_openid":userid
|
|
})
|
|
.get()
|
|
.then(res => {
|
|
console.log("列表页", res.data);
|
|
console.log(res.data[0].cartlist)
|
|
//再次显示数据
|
|
this.setData({
|
|
cartlistsee:res.data[0].cartlist,
|
|
total_all_price: 0,
|
|
checked: false
|
|
})
|
|
})
|
|
.catch(err => {
|
|
console.log("加载失败", err);
|
|
})
|
|
|
|
},
|
|
|
|
|
|
/**
|
|
* 生命周期函数--监听页面初次渲染完成
|
|
*/
|
|
onReady() {
|
|
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面显示
|
|
*/
|
|
onShow() {
|
|
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面隐藏
|
|
*/
|
|
onHide() {
|
|
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面卸载
|
|
*/
|
|
onUnload() {
|
|
|
|
},
|
|
|
|
/**
|
|
* 页面相关事件处理函数--监听用户下拉动作
|
|
*/
|
|
onPullDownRefresh() {
|
|
console.log("下拉刷新...");
|
|
},
|
|
|
|
/**
|
|
* 页面上拉触底事件的处理函数
|
|
*/
|
|
onReachBottom() {
|
|
console.log("上拉加载....");
|
|
},
|
|
|
|
/**
|
|
* 用户点击右上角分享
|
|
*/
|
|
onShareAppMessage() {
|
|
|
|
}
|
|
}) |