绑定openid

master
wbb 2 years ago
parent 2ebc67070f
commit ebd507a83b

@ -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;

@ -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
})

@ -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',

Loading…
Cancel
Save