解释一下this.globalData.userInfo.openId = res.result.openid;
时间: 2023-05-19 10:04:37 浏览: 57
这段代码是在小程序中获取用户的唯一标识符(openid)并将其存储在全局变量中。其中,this代表当前小程序实例,globalData是小程序全局变量的一个对象,userInfo是其中的一个属性,用于存储用户信息。通过调用微信小程序API中的wx.cloud.callFunction方法,可以获取到用户的openid,然后将其赋值给userInfo对象中的openid属性。
相关问题
const app =getApp() Page({ data: { }, onShow(){ this.setData({ userInfo:app.globalData.userInfo }) }, onLoad: function (options) { }, toMyOrder(){ wx.navigateTo({ url: '/pages/me/myOders/myOrders', }) }, login(){ wx.getUserProfile({ desc: '用于完善用户信息', }) .then(res=>{ console.log(res) this.setData({ userInfo:res.userInfo }) wx.cloud.database().collection('shop_users') .where({ _openid:app.globalData.openid }) .get() .then(result=>{ console.log(result) if(result.data.length == 0){ //添加用户数据到数据库 wx.cloud.database().collection('shop_users') .add({ data:{ avatarUrl:res.userInfo.avatarUrl, nickName: res.userInfo.nickName } }) .then(addResult=>{ console.log(addResult) //获取数据库用户信息 app.getUserInfo() wx.showToast({ title: '登录成功', }) }) }else{ wx.cloud.database().collection('shop_users') .doc(result.data[0]._id) .update({ data:{ avatarUrl:res.userInfo.avatarUrl, nickName: res.userInfo.nickName } }) .then(updateResult=>{ console.log(updateResult) //获取数据库用户信息 app.getUserInfo() wx.showToast({ title: '登录成功', }) }) } }) }) }, loinOut(){ app.globalData.userInfo = null wx.setStorageSync('userInfo', null) this.setData({ userInfo:null })
这段代码是一个小程序中的页面代码,其中主要实现了以下功能:
1. 在页面显示时,将全局变量 app.globalData.userInfo 中的用户信息赋值给页面的 data 中的 userInfo 变量。
2. 点击“我的订单”按钮时,跳转到“我的订单”页面。
3. 点击“登录”按钮时,调用 wx.getUserProfile 接口获取用户的头像和昵称等信息,并将其保存到数据库中。如果该用户已经存在于数据库中,则更新其信息。
4. 点击“退出登录”按钮时,将全局变量 app.globalData.userInfo 和本地存储中的 userInfo 变量都设为 null,并将页面的 userInfo 变量也设为 null。
总体来说,这段代码实现了一个小程序中常见的用户登录、退出登录和跳转页面等功能。
opendLocation(event){ var that = this; wx.openLocation({ latitude: that.data.action.latitude, longitude: that.data.action.longitude, }) }, previewImg(event){ var that = this; console.log(event) wx.previewImage({ current: event.currentTarget.dataset.src,//当前显示图片的路径 urls: that.data.action.images, }) }, onLoad: function (options) { this.setData({ openid : app.globalData.openid }) console.log(options.id) this.data.id = options.id this.getDetail() }, getDetail(){ var that = this; wx.cloud.database().collection('shop_actions').doc(this.data.id).get({ success(res){ console.log(res) var action = res.data action.time = util.formatTime(new Date(action.time)) for(var l in action.prizeList){ if(action.prizeList[l].openid == app.globalData.openid){ action.isPrized = true } } for(var l in action.commentList){ action.commentList[l].time = util.formatTime(new Date(action.commentList[l].time)) } that.setData({ action: res.data }) } }) }, delete(){ console.log(this.data.id) var that = this; wx.cloud.database().collection('shop_actions').doc(this.data.id).remove({ success(res){ console.log(res) wx.navigateBack({ success(res){ wx.showToast({ title: '删除成功!', }) } }) } }) }, pirzeAction(event){ var that = this; if(app.globalData.userInfo == null){ wx.switchTab({ url: '/pages/me/me', success(){ wx.showToast({ icon:'error', title: '请登录', }) } }) }else { console.log(that.data.id) var that = this; wx.cloud.database().collection('shop_actions').doc(that.data.id).get({ success(res){ console.log(res) var action = res.data var tag = false var index for(var l in action.prizeList){ if(action.prizeList[l].openid == app.globalData.openid){ tag = true index = l break } }
这段代码是小程序中的一些功能实现,包括打开地理位置、预览图片、加载页面、删除操作、奖励操作等。其中用到了微信小程序提供的API函数,如wx.openLocation()、wx.previewImage()、wx.cloud.database()等。同时,在获取数据时使用了异步回调函数success()来处理返回的数据。