app.js中操作wx.login在首页中获取值为空处理.
发布于 4 年前 作者 yanwang 2088 次浏览 来自 分享

app.js页面操作wx.login 

wx.login({

success: function(res) {

        if (res.code) {

          wx.request({

            method: “POST”,

            url: getApp().globalData.url + ‘/v1/wechat/userId’,

            data: {

              js_code: res.code

            },

            header: {

              “Content-Type”: “application/x-www-form-urlencoded”,

            },

            success: function(res) {

              wx.setStorageSync(“openid”, res.data.data.openid)

              wx.setStorageSync(“access_token”, res.data.data.access_token)

              wx.setStorageSync(“status”, res.data.data.status)

              if (that.callBack){

                that.callBack(res);

              }

            }

          })

        } else {

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

        }

      }

    })

首页获取值

const app = getApp();

onLoad(query) {

    let that = this;

    //判断onLaunch是否执行完毕

    if (wx.getStorageSync(‘access_token’)) {

    } else {

      app.callBack = res => {       

      };

    }

},

回到顶部