微信小游戏/微信小程序直接唤起授权面板wx.getSetting()、wx.getUserInfo()、wx.getUserProfile()、wx.createUserInfoButton()

微信小游戏/微信小程序直接唤起授权面板wx.getSetting()、wx.getUserInfo()、wx.getUserProfile()、wx.createUserInfoButton()

微信小游戏/微信小程序的开发,经常会遇到需要获取微信用户信息的需求,简单记录实现方式。
简单介绍:

wx.getSetting():
获取用户的当前设置。返回值中只会出现小程序已经向用户请求过的权限。
示例:

wx.getSetting({
      success (res){
        if (res.authSetting['scope.userInfo']) {
          // 已经授权,可以直接调用 getUserInfo 获取头像昵称
          console.log('【已授权】');
        } else {
          console.log('【未授权】');
         }
      },
      fail (res) {
      console.log('【获取失败】)
      }
    })

wx.getUserInfo():
获取用户信息(需要用户授权)。
示例(已授权情况下):

wx.getUserInfo({
            success: function(res) {
              console.log('【微信返回】', res.userInfo)
            

猜你喜欢

转载自blog.csdn.net/start_sea/article/details/130156518