微信小程序 — wx.getUserInfo引导用户授权问题

版权声明:本博客为博主原创博文,可以任意转载。转载请附上博客链接: http://blog.csdn.net/qiqishuang https://blog.csdn.net/qiqishuang/article/details/82386344
//index.js

//获取应用实例
const app = getApp()

Page({
  data: {
    userInfo: {},
    // hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },

  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        // hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          // hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globaData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo
          })
          // 最后,在page中定义一个 用于检测 当前授权的状态
          // that.checkSettingStatus();
        }
      })
    }
  }
  
})

猜你喜欢

转载自blog.csdn.net/qiqishuang/article/details/82386344