变量赋值问题
发布于 6 年前 作者 jiewu 11478 次浏览 来自 问答

onLoad: function (options) {

// 获取openID

var oi=’’;

var that = this;

var user = wx.getStorageSync(‘user’) || {};

var userInfo = wx.getStorageSync(‘userInfo’) || {};

if ((!user.openid || (user.expires_in || Date.now()) < (Date.now() + 600)) && (!userInfo.nickName)) {

wx.login({

success: function(res) {

if (res.code) {

wx.getUserInfo({

success: function(res) {

var objz = {};

objz.avatarUrl = res.userInfo.avatarUrl;

objz.nickName = res.userInfo.nickName;

//console.log(objz);

wx.setStorageSync(‘userInfo’, objz); //存储userInfo

}

});

var d = that.globalData; //这里存储了appid、secret、token串

console.log(“appid” + d.appid);

console.log(“token” + res.code);

var l = https://api.weixin.qq.com/sns/jscode2session?appid= + d.appid + ‘&secret=’ + d.secret + ‘&js_code=’ + res.code + ‘&grant_type=authorization_code’;

wx.request({

url: l,

data: {},

method: ‘GET’, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT  

// header: {}, // 设置请求的 header  

success: function(res) {

var obj = {};

obj.openid = res.data.openid;

obj.expires_in = Date.now() + res.data.expires_in;

oi = obj.openid;

console.log(“OPENID” + oi);

}

});

} else {

console.log(‘获取用户登录态失败!’ + res.errMsg)

}

}

});

}

console.log(“OPENID” + oi);

},

请问为什么最后一个oi变量输出为空,明明在前面oi = obj.openid给oi赋值了啊

4 回复

wx.request又不是同步的,最后一个oi肯定没值啊

js 异步 百度下吧,js基础知识

jscode2session这个接口不能在前端调用的啊,这个接口本身就报错了,肯定是没值啊

回到顶部