- 当前 Bug 的表现(可附上截图)
console.log(that.globalData.openid); console.log(that.globalData.hasUserInfo); console.log(that.globalData); |
- 预期表现
openid应当有值
- 复现路径
app.js直接执行
- 提供一个最简复现 Demo
//app.js App({ onLaunch: function () { }, globalData: { userInfo: null , openid: null , hasUserInfo: false , apiUrl: 'https://www.baidu.com' , }, getUserInfo: function () { var that = this ; if (that.globalData.hasUserInfo == false ) { wx.login({ success: function (res) { if (res.code) { var code = res.code; wx.request({ url: that.globalData.apiUrl, data: { 'controller' : 'Api\\Wuji\\Zhanguan' , 'method' : 'authorization' , 'code' :res.code }, success: function (res) { that.globalData.openid = 'this is openid' ; } }) } } }); wx.getUserInfo({ success: res => { //save infomation that.globalData.userInfo = res.userInfo; that.globalData.hasUserInfo = true ; that.saveUserInfo(); }, fail: res => { wx.openSetting({ success: (res) => { if (res.authSetting[ "scope.userInfo" ] == false ) { wx.showToast({ title: '用户未登录' , icon: 'none' , duration: 2000 }); } else { wx.getUserInfo({ success: res => { //save infomation that.globalData.userInfo = res.userInfo; that.globalData.hasUserInfo = true ; that.saveUserInfo(); } }) } } }) } }); } }, saveUserInfo: function (){ var that = this ; console.log(that.globalData.openid); console.log(that.globalData.hasUserInfo); console.log(that.globalData); if (that.globalData.openid != null && that.globalData.hasUserInfo == true ){ wx.request({ url: that.globalData.apiUrl, data: { 'controller' : 'Api\\Wuji\\Zhanguan' , 'method' : 'saveUserInfo' , 'openid' : that.globalData.openid, 'nickname' : that.globalData.userInfo.nickName, 'avatar' : that.globalData.userInfo.avatarUrl, 'gender' : that.globalData.userInfo.gender, 'province' : that.globalData.userInfo.province, 'city' : that.globalData.userInfo.city }, success: function (res) { console.log(res); if (res.data.status == 200 ) { var data = res.data.data; //console.log(data); } } }); } }, cutString: function (str, len, fill = '...' ) { if (str.length * 2 <= len) { return str; } var strlen = 0 ; var s = "" ; for ( var i = 0 ; i < str.length; i++) { s = s + str.charAt(i); if (str.charCodeAt(i) > 128 ) { strlen = strlen + 2 ; if (strlen >= len) { return s.substring( 0 , s.length - 1 ) + fill; } } else { strlen = strlen + 1 ; if (strlen >= len) { return s.substring( 0 , s.length - 2 ) + fill; } } } return s; } }) |
我也有跟你一样的问题。。。咋办啊😂
怎么解决?解决这个问题了吗
异步的问题