怎样在其他页面调用openid,可以的话,给个例子就好?
我在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: {},
loading: true,
schoolIndex: 0,
school: new Array("请选择", "数据科学与人工智能学院"),
schoolId: new Array("noSelect", "969"),
cateisShow: false,//弹出框
pictureFiles: null,
hidden: false,
avatarUrl: './user-unlogin.png',
userInfo: {},
logged: false,
takeSession: false,
requestResult: '',
userOpenid:"",
//认证信息
userName:null,
phoneNumber:null,
userSchool:null,
userClass:null,
studentCard:null,
changePic:false //是否切换了图片
},
onLoad: function () {
var that = this;
wx.cloud.callFunction({
name: 'login',
data: {},
success: res => {
console.log('[云函数] [login] user openid: ', res.result.openid)
userOpenid = res.result.openid
wx.navigateTo({
url: '../userConsole/userConsole',
})
},
fail: err => {
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({
success: res => {
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.getUserInfo({
success: res => {
console.log(res);
this.setData({
avatarUrl: res.userInfo.avatarUrl,
userInfo: res.userInfo
})
}
})
}
}
})
},
onReady: function () {
this.setData({
loading: false
})
},
bindPickerSchoolChange: function (e) {
//学校切换
console.log(e);
xueyuan=e.detail.value;
this.setData({
schoolIndex: e.detail.value
})
},
togglePtype: function () {
//显示分类
console.log(e);
this.setData({
cateisShow: !this.data.cateisShow
})
},
pictureView: function (e) {
console.log("123")
},
changePicture: function (e) {
},
setName: function (e) {
//真实姓名
var that = this;
name=e.detail.value;
console.log(name);
that.setData({
userName: e.detail.value
})
},
setMajor: function (e) {
//专业班级
var that = this;
zhuanye=e.detail.value;
console.log(zhuanye);
that.setData({
userClass: e.detail.value
})
},
setPhone: function (e) {
//联系方式
var that = this;
lianxi=e.detail.value;
console.log(lianxi);
that.setData({
phoneNumber: e.detail.value
})
},
setCardId: function (e) {
//学号
var that = this;
xuehao=e.detail.value;
console.log(xuehao);
that.setData({
studentCard: e.detail.value
})
},
toAuth: function () {
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',
duration: 3000
})
wx.reLaunch({
url: '/pages/self/self',
})
})
}
})
w我这是直接在onload页面去加载openid了,但是我之前已经在index页面获取openid了,跟我上面说的一样,所以我想换一种方式,从app中去调用已经获取了的openid,但是这怎么做