微信小程序之getUserProfile

发布时间:2023-05-20

一、getUserProfile是什么

getUserProfile是微信小程序新增的API,可以获取用户的基本信息,包括头像、昵称、性别等,无需用户授权,且不受scope限制。 getUserProfile是通过获取用户个人信息按钮实现的,可以在用户点击按钮后获取用户的个人信息。该API适用于需要获取用户信息,但不需要获取用户敏感信息的场景。

二、getUserProfile怎么使用

1. getUserProfile的基本调用方法

getUserProfile的基本调用方法如下:

wx.getUserProfile({
  desc: '获取用户信息',
  success: res => {
    console.log(res.userInfo)
  }
})

其中,desc是获取用户信息时的提示语,success是成功回调函数,res.userInfo包含用户的基本信息,如头像、昵称、性别等。其他参数如lang、fail等可自行查找使用。

2. getUserProfile的异常处理

在getUserProfile调用过程中,可能会出现以下异常情况:

  1. 调用时未显示弹窗:如果getUserProfile调用时未显示弹窗,可能是因为调用时机不对,可以尝试将调用放在页面onLoad等生命周期函数中,或者尝试重新打开小程序。
  2. 显示弹窗后用户未点击授权:如果用户未点击授权,可以在弹窗弹出后进行提示,引导用户进行授权。
  3. 用户拒绝授权:如果用户拒绝授权,可以提示用户无法获取用户信息,或者引导用户到设置页面进行授权。

3. getUserProfile的使用示例

示例代码如下:

// index.js
Page({
  data: {
    userInfo: {},
    hasUserInfo: false
  },
  //事件处理函数
  getUserProfile() {
    wx.getUserProfile({
      desc: '获取用户信息',
      success: (res) => {
        console.log(res.userInfo)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      },
      fail: () => {
        console.log('getUserProfile fail')
      }
    })
  }
})
<!-- index.wxml -->
<view class="container">
  <button wx:if="{{!hasUserInfo}}" class="userinfo-btn" open-type="getUserProfile" bindgetuserprofile="getUserProfile">点击获取用户信息</button>
  <view wx:if="{{hasUserInfo}}" class="userinfo">
    <image bindtap="getUserProfile" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" background-size="cover"></image>
    <text class="userinfo-nickname">{{userInfo.nickName}}</text>
    <text class="userinfo-gender">{{userInfo.gender == 1 ? '男' : '女'}}</text>
  </view>
</view>

三、getUserProfile常见问题解决方案

1. getUserProfile怎么修改

getUserProfile是调用微信提供的API获取用户信息,无法修改,只能根据调用API的返回值来获取用户信息。

2. getUserProfile怎么避免重复授权

为避免重复授权,可以在获取用户信息前判断用户是否已授权,如果已授权,则无需再次获取。如果未授权,则调用getUserProfile进行授权。 示例代码如下:

// index.js
Page({
  data: {
    userInfo: {},
    hasUserInfo: false
  },
  onShow: function () {
    if (wx.getStorageSync('hasUserInfo')) {
      this.setData({
        userInfo: wx.getStorageSync('userInfo'),
        hasUserInfo: true
      })
    }
  },
  //事件处理函数
  getUserProfile() {
    if (!this.data.hasUserInfo) {
      wx.getUserProfile({
        desc: '获取用户信息',
        success: (res) => {
          console.log(res.userInfo)
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
          wx.setStorageSync('userInfo', res.userInfo)
          wx.setStorageSync('hasUserInfo', true)
        },
        fail: () => {
          console.log('getUserProfile fail')
        }
      })
    }
  }
})
<!-- index.wxml -->
<view class="container">
  <button wx:if="{{!hasUserInfo}}" class="userinfo-btn" open-type="getUserProfile" bindgetuserprofile="getUserProfile">点击获取用户信息</button>
  <view wx:if="{{hasUserInfo}}" class="userinfo">
    <image bindtap="getUserProfile" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" background-size="cover"></image>
    <text class="userinfo-nickname">{{userInfo.nickName}}</text>
    <text class="userinfo-gender">{{userInfo.gender == 1 ? '男' : '女'}}</text>
  </view>
</view>

3. getUserProfile提示升级微信版本

如果使用getUserProfile时提示升级微信版本,可以在开发者工具中将微信版本升级至最新版,或者在手机微信中将微信版本升级至最新版即可。

四、user profile是什么意思

user profile通常指用户资料、用户简介等,包括用户基本信息,如姓名、性别、出生日期、地址等,以及用户偏好、兴趣爱好、历史记录等。 在微信小程序中,user profile指的是通过getUserProfile获取的用户基本信息,如头像、昵称、性别等。 以上就是getUserProfile的详细介绍,希望对大家有所帮助。