# wx.getUserProfile(Object object)

用户头像昵称获取规则已调整，参考 [小程序用户头像昵称获取规则调整公告](https://developers.weixin.qq.com/community/develop/doc/00022c683e8a80b29bed2142b56c01)

> 基础库 2.10.4 开始支持，低版本需做[兼容处理](1003706)。

> __以 [Promise 风格](/miniprogram/dev/framework/app-service/api.md#异步-API-返回-Promise) 调用__：支持
> 
> __小程序插件__：不支持
> 
> __微信 Windows 版__：支持
> 
> __微信 鸿蒙 OS 版__：支持
> 
> __限制__：仅在点击行为时调用

> 相关文档: [接口调用频率规范](/miniprogram/dev/framework/performance/api-frequency.md)

## 功能描述

获取用户信息。页面产生点击事件（例如 `button` 上 `bindtap` 的回调中）后才可调用，每次请求都会弹出授权窗口，用户同意后返回 `userInfo`。该接口用于替换 `wx.getUserInfo`，详见 [用户信息接口调整说明](https://developers.weixin.qq.com/community/develop/doc/000cacfa20ce88df04cb468bc52801?highLine=login)。

## 参数

### Object object


```json
[
  {
    "属性": "lang",
    "类型": "string",
    "默认值": "en",
    "必填": "否",
    "说明": "显示用户信息的语言",
    "子项": [
      {
        "合法值": "en",
        "说明": "英文"
      },
      {
        "合法值": "zh_CN",
        "说明": "简体中文"
      },
      {
        "合法值": "zh_TW",
        "说明": "繁体中文"
      }
    ]
  },
  {
    "属性": "desc",
    "类型": "string",
    "默认值": "",
    "必填": "是",
    "说明": "声明获取用户个人信息后的用途，不超过30个字符"
  },
  {
    "属性": "success",
    "类型": "function",
    "默认值": "",
    "必填": "否",
    "说明": "接口调用成功的回调函数"
  },
  {
    "属性": "fail",
    "类型": "function",
    "默认值": "",
    "必填": "否",
    "说明": "接口调用失败的回调函数"
  },
  {
    "属性": "complete",
    "类型": "function",
    "默认值": "",
    "必填": "否",
    "说明": "接口调用结束的回调函数（调用成功、失败都会执行）"
  }
]
```

#### object.success 回调函数
##### 参数

###### Object res

| 属性 | 类型 | 说明 | 最低版本 |
| ---- | ---- | ---- | -------- |
| userInfo  | [UserInfo](/miniprogram/dev/api/open-api/user-info/UserInfo) | 用户信息对象 | [2.10.4](1003706) |
| rawData  | string | 不包括敏感信息的原始数据字符串，用于计算签名 | [2.10.4](1003706) |
| signature  | string | 使用 sha1( rawData + sessionkey ) 得到字符串，用于校验用户信息，详见 [用户数据的签名验证和加解密](/miniprogram/dev/framework/open-ability/signature.md) | [2.10.4](1003706) |
| encryptedData  | string | 包括敏感数据在内的完整用户信息的加密数据，详见 [用户数据的签名验证和加解密](/miniprogram/dev/framework/open-ability/signature.md#加密数据解密算法) | [2.10.4](1003706) |
| iv  | string | 加密算法的初始向量，详见 [用户数据的签名验证和加解密](/miniprogram/dev/framework/open-ability/signature.md#加密数据解密算法) | [2.10.4](1003706) |
| cloudID  | string | 敏感数据对应的云 ID，开通[云开发](/miniprogram/dev/wxcloudservice/wxcloud/basis/getting-started.md)的小程序才会返回，可通过云调用直接获取开放数据，详细见[云调用直接获取开放数据](/miniprogram/dev/framework/open-ability/signature.md#method-cloud) | [2.10.4](1003706) |

## 示例代码

[在开发者工具中预览效果](https://developers.weixin.qq.com/s/tsJaq2mP7Mp4)

## Bug & Tip

1. `tip`：仅小程序中 `wx.getUserInfo` 接口进行调整，小游戏中不受影响；
2. `tip`：开发者工具中仅 2.10.4 及以上版本可访问 `wx.getUserProfile` 接口，在真机上可参考示例代码进行判断，无需根据版本号或者 `canIUse` 进行条件。
3. `tip`：`wx.getUserProfile` 返回的加密数据中不包含 `openId` 和 `unionId` 字段。
4. `bug`：开发者工具中 `2.10.4`~`2.16.1` 基础库版本通过 `<button open-type="getUserInfo">` 会返回真实数据，真机上此区间会按照公告返回匿名数据。

```html
<view class="container">
  <view class="userinfo">
    <block wx:if="{{!hasUserInfo}}">
      <button wx:if="{{canIUseGetUserProfile}}" bindtap="getUserProfile"> 获取头像昵称 </button>
      <button wx:else open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 获取头像昵称 </button>
    </block>
    <block wx:else>
      <image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image>
      <text class="userinfo-nickname">{{userInfo.nickName}}</text>
    </block>
  </view>
</view>
```

```js
Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUseGetUserProfile: false,
  },
  onLoad() {
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
    // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
})
```
