From ebd507a83bf84ad9552c63314d203ff73817e308 Mon Sep 17 00:00:00 2001 From: wbb <1975655432@qq.com> Date: Thu, 17 Nov 2022 10:42:15 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=91=E5=AE=9Aopenid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scr/food/app.js | 16 ++++++++++++++-- scr/food/pages/cart/cart.js | 28 ++++++++++++++++++++-------- scr/food/pages/detail/detail.js | 23 +++++++++++++++++------ 3 files changed, 51 insertions(+), 16 deletions(-) diff --git a/scr/food/app.js b/scr/food/app.js index 88e4722..fa79a63 100644 --- a/scr/food/app.js +++ b/scr/food/app.js @@ -15,10 +15,21 @@ App({ traceUser: true, }); } -this.getOpenid() + this.globalData = {}; // 获取用户openid - + wx.cloud.callFunction({ + name: 'get', + data: {}, + success: res => { + this.globalData.openid = res.result.openid + }, + fail: err => { + console.error('[云函数] [login] 调用失败', err) + } + }) + console.log(this.globalData) + }, getOpenid() { @@ -26,6 +37,7 @@ getOpenid() { wx.cloud.callFunction({ name: 'get', complete: res => { + console.log('openid: ', res.result.openid) console.log('appid: ', res.result.appid) // var openid = res.result.openId; diff --git a/scr/food/pages/cart/cart.js b/scr/food/pages/cart/cart.js index 1eab53d..3ba2f16 100644 --- a/scr/food/pages/cart/cart.js +++ b/scr/food/pages/cart/cart.js @@ -1,5 +1,7 @@ // pages/cart/cart.js const db = wx.cloud.database({}); +const app=getApp() +const userid=app.globalData.openid Page({ /** @@ -98,8 +100,10 @@ setChecked: function() { cartlistsee:cartlistsee //更新商品数据 }) console.log(cartlistsee) -db.collection('user') -.doc('eda7dd506371adb300c89bc66afc4601') //还需修改 +db.collection('user'). +where({ + "_openid":userid + }) //还需修改 .update( { data:{ @@ -130,7 +134,9 @@ db.collection('user') }) console.log(cartlistsee) db.collection('user')//回调回云数据库 -.doc('eda7dd506371adb300c89bc66afc4601') //还需修改 +.where({ + "_openid":userid + }) //还需修改 .update( { data:{ @@ -160,7 +166,9 @@ let foodid=cartlistsee[index]._id cartlistsee:cartlistsee }) db.collection('user')//回调回云数据库 - .doc('eda7dd506371adb300c89bc66afc4601') //还需修改 + . where({ + "_openid":userid + }) //还需修改 .update( { data:{ @@ -169,7 +177,9 @@ let foodid=cartlistsee[index]._id }) db.collection('user')//回调回云数据库 - .doc('eda7dd506371adb300c89bc66afc4601') //还需修改 + .where({ + "_openid":userid + }) //还需修改 .update( { data:{ @@ -193,14 +203,16 @@ let foodid=cartlistsee[index]._id }) //2、开始查询数据了 news对应的是集合的名称 db.collection("user") - .doc("eda7dd506371adb300c89bc66afc4601") + .where({ + "_openid":userid + }) .get() .then(res => { console.log("列表页", res.data); - console.log(res.data.cartlist) + console.log(res.data[0].cartlist) //再次显示数据 this.setData({ - cartlistsee:res.data.cartlist, + cartlistsee:res.data[0].cartlist, total_all_price: 0, checked: false }) diff --git a/scr/food/pages/detail/detail.js b/scr/food/pages/detail/detail.js index e39b79d..711ecea 100644 --- a/scr/food/pages/detail/detail.js +++ b/scr/food/pages/detail/detail.js @@ -1,7 +1,7 @@ // pages/detail/detail.js const db = wx.cloud.database({}); const cont = db.collection('food'); -const get=getApp() +const app=getApp() Page({ /** @@ -24,12 +24,14 @@ Page({ onLoad: function (options) { - const app=getApp() + let foodid =options.id var userid=app.globalData.openid console.log('1111w '); console.log(userid) - + this.setData({ + userid:userid + }) cont.doc(foodid).get({ success: res => { console.log(res.data) @@ -52,13 +54,22 @@ async addCar(event) { detailObj.num=num detailObj.checks=checks console.log(detailObj) - db.collection('user').doc('eda7dd506371adb300c89bc66afc4601').update({ +var userid=app.globalData.openid + db.collection('user').where({ + "_openid":userid + }).update({ data:{ cartlist:db.command.push(detailObj) }, - }) - + }) + db.collection('user').where({ + "_openid":userid + }).get({ + success: res => { + console.log(res.data)} + }) + wx.showToast({ title: '加入购物车成功', icon: 'success',