app对象中的globalData存储和获取值不一致
- 当前 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;[代码][代码] [代码][代码]}[代码][代码]})[代码]