# wx.getRelationFriendList(Object object)

> 基础库 3.16.2 开始支持，低版本需做[兼容处理](/miniprogram/dev/framework/compatibility.md)。

> __以 [Promise 风格](/miniprogram/dev/framework/app-service/api.md#异步-API-返回-Promise) 调用__：不支持
>
> __[用户授权](/minigame/dev/guide/base-ability/authorize.md)__：需要 scope.scope.interactedUserInfo
>
> __微信 鸿蒙 OS 版__：支持

> 相关文档: [开放数据校验与解密](/minigame/dev/guide/open-ability/signature.md)

## 功能描述

获取小游戏同玩互动好友列表。该接口需要用户授权 scope.interactedUserInfo，首次调用时会弹出授权窗口。也可以提前通过 [wx.authorize](/minigame/dev/guide/base-ability/authorize.md) 进行授权。当用户拒绝授权后，再次调用该接口不会再弹出授权窗口，而是直接进入 fail 回调。此时开发者可通过弹出提示或者通过 [wx.openSetting](/miniprogram/dev/platform-capabilities/miniapp/scene/dev/setting.md) 引导用户进入设置页面重新开启授权。

## 参数

### Object object

| 属性 | 类型 | 默认值 | 必填 | 说明 |
| ---- | ---- | ------ | -------- | ---- |
| success | function |  | 否 | 接口调用成功的回调函数 |
| fail | function |  | 否 | 接口调用失败的回调函数 |
| complete | function |  | 否 | 接口调用结束的回调函数（调用成功、失败都会执行） |

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

###### Object res

| 属性 | 类型 | 说明 |
| ---- | ---- | ---- |
| signature  | string | 使用 sha1( rawData + sessionkey ) 得到字符串，用于校验用户信息 |
| encryptedData  | string | 包括 RelationFriendData 在内的加密数据，详见[加密数据解密算法](/minigame/dev/guide/open-ability/signature.md) |
| iv  | string | 加密算法的初始向量 |
| cloudID  | string | 敏感数据对应的云 ID，开通云开发的小程序才会返回，可通过[云调用直接获取开放数据](/minigame/dev/guide/open-ability/signature.md) |

## 示例代码

```javascript

// 提前授权（可选）
wx.authorize({
  scope: 'scope.interactedUserInfo',
  success: () => {
    // 用户同意授权，可以调用接口
    getRelationFriends()
  },
  fail: () => {
    console.log('用户拒绝授权')
  }
})

function getRelationFriends() {
  wx.getRelationFriendList({
    success: (res) => {
      console.log(res.encryptedData)
    },
    fail: (res) => {
      console.error(res)
      // 判断是否为用户拒绝授权导致的失败，此处引导仅为示意，游戏可根据需求自行处理
      // res = { errno: 0, err_code: "-12006", errMsg: "getRelationFriendList:fail auth deny" }
      if (res.errMsg && res.errMsg.indexOf('auth deny') !== -1) {
        // 用户此前已拒绝授权，可通过弹窗提示引导用户前往设置页面重新开启授权
        wx.showModal({
          title: '授权提示',
          content: '需要获取互动好友信息，请在设置中开启授权',
          success: (modalRes) => {
            if (modalRes.confirm) {
              wx.openSetting()
            }
          }
        })
      }
    },
    complete: (res) => console.log(res)
  })
}

```

## encryptedData 解密后得到的 RelationFriendData 的结构

| 属性 | 类型 | 说明                                   |
| ------- | ------- | -------------------------------------- |
|  list   | Array\ | 同玩互动好友列表           |

## RelationFriendInfo 的结构

| 属性 | 类型 | 说明                                   |
| ------- | ------- | -------------------------------------- |
|  openid   | string | 好友的openid          |
