From 12173f49fc0dc479573db10c94f74c23b6e1b406 Mon Sep 17 00:00:00 2001 From: wbb <1975655432@qq.com> Date: Wed, 16 Nov 2022 19:22:09 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B4=AD=E7=89=A9=E8=BD=A6=E5=B7=B2=E9=80=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scr/food/app.js | 10 +- scr/food/app.json | 3 +- scr/food/pages/cart/cart.js | 185 +++++++++++++++++++++- scr/food/pages/cart/cart.wxml | 4 +- scr/food/pages/detail/detail.js | 18 ++- scr/food/pages/usercenter/usercenter.wxml | 4 +- scr/food/project.config.json | 1 + 7 files changed, 209 insertions(+), 16 deletions(-) diff --git a/scr/food/app.js b/scr/food/app.js index 7374423..95643d4 100644 --- a/scr/food/app.js +++ b/scr/food/app.js @@ -17,5 +17,13 @@ App({ } this.globalData = {}; - } + },//触底函数 + onReachBottom(){ + console.log("上拉加载...."); + }, + //上拉函数 + onPullDownRefresh(){ + console.log("下拉刷新..."); + }, + }); diff --git a/scr/food/app.json b/scr/food/app.json index 845309c..b0bf96e 100644 --- a/scr/food/app.json +++ b/scr/food/app.json @@ -51,7 +51,8 @@ "backgroundTextStyle": "light", "navigationBarBackgroundColor": "#F6F6F6", "navigationBarTitleText": "节时不节食", - "navigationBarTextStyle": "black" + "navigationBarTextStyle": "black", + "enablePullDownRefresh": true }, "networkTimeout": { "request": 10000, diff --git a/scr/food/pages/cart/cart.js b/scr/food/pages/cart/cart.js index 4ba3488..1eab53d 100644 --- a/scr/food/pages/cart/cart.js +++ b/scr/food/pages/cart/cart.js @@ -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("上拉加载...."); }, /** diff --git a/scr/food/pages/cart/cart.wxml b/scr/food/pages/cart/cart.wxml index 265f26c..58bc0b4 100644 --- a/scr/food/pages/cart/cart.wxml +++ b/scr/food/pages/cart/cart.wxml @@ -4,7 +4,7 @@ - + @@ -20,7 +20,7 @@ - 删除 + 删除 diff --git a/scr/food/pages/detail/detail.js b/scr/food/pages/detail/detail.js index 961cdc0..312e437 100644 --- a/scr/food/pages/detail/detail.js +++ b/scr/food/pages/detail/detail.js @@ -11,7 +11,8 @@ Page({ detailObj: {}, list:[], clockresult:[], - userid:null + userid:null, + num:null }, @@ -38,15 +39,20 @@ Page({ }, // 渲染数据 async addCar(event) { - var detailObj= event.currentTarget.dataset.item; - var userid=event.currentTarget.dataset.userid; + let detailObj= event.currentTarget.dataset.item; + let num=this.data.num + num=num+1 + const checks=false + console.log(num) + detailObj.num=num + detailObj.checks=checks + console.log(detailObj) db.collection('user').doc('eda7dd506371adb300c89bc66afc4601').update({ data:{ cartlist:db.command.push(detailObj) - } + }, }) - - + wx.showToast({ title: '加入购物车成功', diff --git a/scr/food/pages/usercenter/usercenter.wxml b/scr/food/pages/usercenter/usercenter.wxml index d9c7606..809acf7 100644 --- a/scr/food/pages/usercenter/usercenter.wxml +++ b/scr/food/pages/usercenter/usercenter.wxml @@ -44,7 +44,7 @@ - 售后记录 + 收货地址 @@ -53,6 +53,6 @@ 分享邀请 - + diff --git a/scr/food/project.config.json b/scr/food/project.config.json index d4a3633..bb4f26e 100644 --- a/scr/food/project.config.json +++ b/scr/food/project.config.json @@ -2,6 +2,7 @@ "miniprogramRoot": "", "cloudfunctionRoot": "cloudfunctions/", "setting": { + "enablePullDownRefresh": true, "urlCheck": true, "es6": true, "enhance": true,