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.
git/scr/food/pages/cart/cart.js

311 lines
7.4 KiB

2 years ago
// pages/cart/cart.js
2 years ago
const db = wx.cloud.database({});
2 years ago
const app=getApp()
const userid=app.globalData.openid
2 years ago
Page({
/**
* 页面的初始数据
*/
data: {
2 years ago
cartlistsee:[], //该用户购物车的数组
2 years ago
total_all_price:0, //商品的总价格
2 years ago
all_shop: 0, //所选商品的id
shop_num: 0, //所选商品的数量
good_one: [], //单个商品的数据
checked: false, //是否为全选
is_shop: false, //是否有商品
2 years ago
shopData: null,
Selection:{}
2 years ago
},
/**点击单个多选框 */
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),
});
2 years ago
},
2 years ago
//点击结算订单
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) {
2 years ago
var Selection=[]
var good_one1 = {}
good_one1._id=good[i]._id
good_one1.url=good[i].url
good_one1.food_name=good[i].food_name
good_one1.food_shop=good[i].food_shop
good_one1.price=good[i].price
good_one1.num=good[i].num
2 years ago
var all=this.data.total_all_price
2 years ago
Selection.push(good_one1)
this.setData({
Selection:Selection
})
//将数据添加到数组里边
2 years ago
this.getTotalPrice()
2 years ago
wx.navigateTo({
2 years ago
url:'/pages/paying/paying?info='+JSON.stringify(Selection)+'&totalprice='+this.data.total_all_price
2 years ago
})
2 years ago
}
}}},
2 years ago
topay: function(){
2 years ago
2 years ago
},
2 years ago
2 years ago
// 添加按钮被点击
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)
2 years ago
db.collection('user').
where({
"_openid":userid
}) //还需修改
2 years ago
.update(
{
data:{
['cartlist.'+[index]]:{num}
}
}
)
2 years ago
this.getTotalPrice()
2 years ago
},
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')//回调回云数据库
2 years ago
.where({
"_openid":userid
}) //还需修改
2 years ago
.update(
{
data:{
['cartlist.'+[index]]:{num}
}
}
)
2 years ago
this.getTotalPrice()
2 years ago
}
},
//删除单个商品
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",
2 years ago
2 years ago
success: (res) => {
if (res.confirm) {
cartlistsee.splice(index, 1);
this.setData({
cartlistsee:cartlistsee
})
db.collection('user')//回调回云数据库
2 years ago
. where({
"_openid":userid
2 years ago
})
2 years ago
.update(
{
data:{
['cartlist.'+[index]]:db.command.remove(),
}
})
db.collection('user')//回调回云数据库
2 years ago
.where({
"_openid":userid
2 years ago
})
2 years ago
.update(
{
data:{
['cartlist']:this.data.cartlistsee
}
}
)
}}
})
2 years ago
this.getTotalPrice()
2 years ago
console.log(cartlistsee)
},
2 years ago
/**
* 生命周期函数--监听页面加载
*/
onLoad:function(options) {
const db = wx.cloud.database({
//这个是环境ID不是环境名称
env: 'cloud1-8g5wmepxce8a3b8a'
})
//2、开始查询数据了 news对应的是集合的名称
db.collection("user")
2 years ago
.where({
"_openid":userid
})
.get()
.then(res => {
console.log("列表页", res.data);
2 years ago
console.log(res.data[0].cartlist)
//再次显示数据
this.setData({
2 years ago
cartlistsee:res.data[0].cartlist,
2 years ago
total_all_price: 0,
checked: false
})
})
.catch(err => {
console.log("加载失败", err);
})
},
2 years ago
2 years ago
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
2 years ago
this.onRefresh();
},
onRefresh:function(){
//导航条加载动画
wx.showNavigationBarLoading({
title:'loading'
})
;
setTimeout(function () {
wx.hideNavigationBarLoading();
//停止下拉刷新
wx.stopPullDownRefresh();
}, 2000);
2 years ago
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
2 years ago
console.log("上拉加载....");
2 years ago
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
}
})