怎样在其他页面调用openid,可以的话,给个例子就好?
发布于 5 年前 作者 guiying12 5133 次浏览 来自 官方Issues

我在index界面获取了openid并app.globalData.openid=res.userInfo.openid,但是我在其他页面的时候用

 getApp().getUserOpenid(function (openid)
getApp().getUserOpenid(function (openid) {
          that.setData({
              // userName: getApp().globalData.userInfo.nickName,
              userOpenid: openid
          })
调用openid(从网上学的)
然后在数据库.add语句中
usermsg.add({  //添加数据
      data: {
        userOpenid:{openid},
        name:name,
        xueyuan:xueyuan,
        zhuanye:zhuanye,
        lianxi:lianxi,
        xuehao:xuehao
      }
d但却显示这个错误

2 回复
 //toAuth.js 认证页面
//获取应用实例
var app = getApp()
const db = wx.cloud.database();
const usermsg = db.collection('usermsg');
var name = null;
var password = null;
var name=null;
var xueyuan=null;
var zhuanye=null;
var lianxi=null;
var xuehao=null;
var userOpenid=null;

wx.cloud.init({
  env'zhongchou-46ua1',
})
Page({
    data: {
        userInfo: {},
        loadingtrue,
        schoolIndex0,
        schoolnew Array("请选择""数据科学与人工智能学院"),
        schoolIdnew Array("noSelect""969"),
        cateisShowfalse,//弹出框
        pictureFilesnull,
        hiddenfalse,
        avatarUrl'./user-unlogin.png',
        userInfo: {},
        loggedfalse,
        takeSessionfalse,
        requestResult'',
        userOpenid:"",
        
        //认证信息
        userName:null,
        phoneNumber:null,
        userSchool:null,
        userClass:null,
        studentCard:null,
        changePic:false //是否切换了图片
    },
    onLoadfunction () {
        var that = this;
        wx.cloud.callFunction({
          name'login',
          data: {},
          successres => {
            console.log('[云函数] [login] user openid: ', res.result.openid)
            userOpenid = res.result.openid
            wx.navigateTo({
              url'../userConsole/userConsole',
            })
          },
          failerr => {
            console.error('[云函数] [login] 调用失败', err)
            wx.navigateTo({
              url'../deployFunctions/deployFunctions',
            })
          }
        })
        getApp().getUserOpenid(function (openid{
          that.setData({
              // userName: getApp().globalData.userInfo.nickName,
              userOpenid: openid
          })
          console.log('用户openid', that.data.userOpenid)
      })

        if (!wx.cloud) {
            wx.redirectTo({
              url'../chooseLib/chooseLib',
            })
            return
          }
      
          // 获取用户信息
          wx.getSetting({
            successres => {
              if (res.authSetting['scope.userInfo']) {
                // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
                wx.getUserInfo({
                  successres => {
                    console.log(res);
                    this.setData({
                      avatarUrl: res.userInfo.avatarUrl,
                      userInfo: res.userInfo
                    })
                  }
                })
              }
            }
          })
        
    },
    onReadyfunction () {
        this.setData({
            loadingfalse
        })
    },
    

    bindPickerSchoolChangefunction (e{
        //学校切换
        console.log(e);
        xueyuan=e.detail.value;
        
        this.setData({
            schoolIndex: e.detail.value
        })
    },

    togglePtypefunction () {
        //显示分类
        console.log(e);
        this.setData({
            cateisShow: !this.data.cateisShow
        })
    },

   

    pictureViewfunction (e{
       
        console.log("123")
    },

    changePicturefunction (e{
      

    },

    setNamefunction (e{
        //真实姓名
       
        var that = this;
        name=e.detail.value;
        console.log(name);
        that.setData({
            userName: e.detail.value
        })
    },

    setMajorfunction (e{
        //专业班级
        
        var that = this;
        zhuanye=e.detail.value;
        console.log(zhuanye);
        that.setData({
            userClass: e.detail.value
        })
    },

    setPhonefunction (e{
        //联系方式
        
        var that = this;
        lianxi=e.detail.value;
        console.log(lianxi);
        that.setData({
            phoneNumber: e.detail.value
        })
    },

    setCardIdfunction (e{
        //学号
        
        var that = this;
        xuehao=e.detail.value;
        console.log(xuehao);
        that.setData({
            studentCard: e.detail.value
        })
    },

    toAuthfunction () {
        let that = this;
    usermsg.add({  //添加数据
      data: {
        userOpenid:userOpenid,
        name:name,
        xueyuan:xueyuan,
        zhuanye:zhuanye,
        lianxi:lianxi,
        xuehao:xuehao
      }
    }).then(res => {
      console.log('提交成功!')
      wx.showToast({
        title'提交成功!',
        icon'success',
        duration3000
      })
      wx.reLaunch({
        url'/pages/self/self',
      })
    })
       
    }

})

w我这是直接在onload页面去加载openid了,但是我之前已经在index页面获取openid了,跟我上面说的一样,所以我想换一种方式,从app中去调用已经获取了的openid,但是这怎么做
回到顶部