收藏
回答

app对象中的globalData存储和获取值不一致

框架类型 问题类型 操作系统 工具版本
小程序 Bug macOS v1.02.1803210

- 当前 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;
  }
})


回答关注问题邀请回答
收藏

3 个回答

  • Luna Moonode
    Luna Moonode
    2020-02-09

    我也有跟你一样的问题。。。咋办啊😂

    2020-02-09
    有用
    回复
  • 赵威
    赵威
    2018-09-26

    怎么解决?解决这个问题了吗

    2018-09-26
    有用
    回复
  • 瓦达西瓦
    瓦达西瓦
    2018-04-04

    异步的问题

    2018-04-04
    有用
    回复
登录 后发表内容