# 用户信息

本文档主要提供 `用户模块(user)` 提供的用户信息 Api 说明。

## 一、观众详细信息

**Interface 接口：** `UserInfoDetail`

| 属性名          | 说明         | 类型         |
| ------------ | ---------- | ---------- |
| `userId`     | 用户 id      | `string`   |
| `nick`       | 昵称         | `string`   |
| `pic`        | 头像         | `string`   |
| `openId`     | 微信 openid  | `string`   |
| `unionId`    | 微信 unionId | `string`   |
| `wxNickname` | 微信昵称       | `string`   |
| `wxAvatar`   | 微信头像       | `string`   |
| `authType`   | 该用户的授权方式   | `AuthType` |

## 二、获取当前用户信息

用于获取当前用户的详细信息。

> 未进行观看条件授权或创建 SDK 没有传入用户信息时返回 `undefined`。

**Api 方法：** `getUserInfo(): undefined | UserInfoDetail`

**返回值说明：** 用户信息，`undefined | UserInfoDetail` 类型

**示例：**

```js
const userInfo = watchCore.user.getUserInfo();
console.log('用户信息', userInfo);
```

## 三、设置微信基础信息

通过 `wx.login` 获取临时登录凭证 code，并在服务端获取用户身份（openId、unionId）后，调用该方法保存，详细可见小程序官方文档：[小程序登录](https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html)。

**Api 方法：** `setWxMiniBaseInfo(info: WxMiniBaseInfo): Promise<void>`

**参数说明：**

* info：微信基础信息，`WxMiniBaseInfo` 类型，必传，详细类型说明如下

| 参数名       | 说明         | 类型       | 必须 | 默认值 |
| --------- | ---------- | -------- | -- | --- |
| `openId`  | 微信 openId  | `string` | 是  | -   |
| `unionId` | 微信 unionId | `string` | 是  | -   |

**示例：**

```js
function getOpenIdAndUnionIdByCode(code) {
  // TODO: 通过后端获取 openId 和 uninoId
  return {
    openId: 'xxx',
    unionId: 'xxx',
  };
}
wx.login({
  success(result) {
    // 临时登录凭证
    const code = result.code;
    // 获取 openId、unionId
    const { openId, unionId } = getOpenIdAndUnionIdByCode(code);
    // 调用方法保存到 SDK 实例中
    watchCore.user.setWxMiniBaseInfo({
      openId,
      unionId,
    });
  }
});
```

## 四、设置小程序用户信息

在进入观看页前，获取小程序用户信息并调用 `setWxMiniUserInfo` 方法保存，详细可见小程序官方文档：[头像昵称填写](https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/userProfile.html)。

**Api 方法：** `setWxMiniUserInfo(info: WxMiniUserInfo): Promise<void>`

**参数说明：**

* info：用户信息，`WxMiniUserInfo` 类型，必传，详细类型说明如下

| 参数名        | 说明   | 类型       | 必须 | 默认值 |
| ---------- | ---- | -------- | -- | --- |
| `nickname` | 微信昵称 | `string` | 是  | -   |
| `avatar`   | 微信头像 | `string` | 是  | -   |

**示例：**

```js
watchCore.user.setWxMiniUserInfo({
  nickname: '微信昵称',
  avatar: '微信头像'
});
```

## 五、获取小程序用户信息

**Api 方法：** `getWxMiniUserInfo(): undefined | WxMiniUserInfo`

**返回值说明：** 微信用户信息，`undefined | WxMiniUserInfo` 类型

## 六、判断是否为特殊的用户身份

用户模块提供 Api 用于判断用户身份是否为内置的特殊用户身份，如讲师、管理员、助教等。

**Api 方法：** `isSpecialUserType(userType: unknown): boolean`

**参数说明：**

* userType：用户身份，`unknown` 类型，必传

**返回值说明：** 是否特殊身份

**示例：**

```js
import { ChatUserType } from '@polyv/live-watch-miniprogram-sdk';
console.log(watchCore.user.isSpecialUserType(ChatUserType.Teacher)); // true
console.log(watchCore.user.isSpecialUserType(ChatUserType.Manager)); // true
console.log(watchCore.user.isSpecialUserType(ChatUserType.Guest)); // true
console.log(watchCore.user.isSpecialUserType(ChatUserType.Student)); // false
console.log(watchCore.user.isSpecialUserType(ChatUserType.Viewer)); // false
```

## 七、获取当前用户 userId

用于获取当前用户的 userId，默认以时间戳字符串作为用户 userId，关于 userId 说明如下：

* 微信环境下进行微信授权后，以用户的微信 `openid` 作为 userId；
* 白名单授权后，以填写的会员码作为 userId（优先级高于微信授权）；

> 未进行观看条件授权或创建 SDK 没有传入用户信息时返回 `undefined`。

**Api 方法：** `getUserId(): undefined | string`

**返回值说明：** 用户 userId，`undefined | string` 类型

**示例：**

```js
const userId = watchCore.user.getUserId();
console.log('用户 userId', userId);
```

## 八、获取当前用户昵称

用于获取当前用户的昵称。

> 未进行观看条件授权或创建 SDK 没有传入用户信息时返回 `undefined`。

**Api 方法：** `getUserNick(): undefined | string`

**返回值说明：** 用户昵称，`undefined | string` 类型

**示例：**

```js
const nickname = watchCore.user.getUserNick();
console.log('用户昵称', nickname);
```

## 九、获取当前用户头像地址

用于获取当前用户的头衔地址，观看页 SDK 提供 `DEFAULT_VIEWER_AVATAR` 默认观众头像的常量。

> 未进行观看条件授权或创建 SDK 没有传入用户信息时返回 `undefined`。

**Api 方法：** `getUserAvatar(): undefined | string`

**返回值说明：** 用户头像地址，`undefined | string` 类型

**示例：**

```js
import { DEFAULT_VIEWER_AVATAR } from '@polyv/live-watch-miniprogram-sdk';

const userAvatar = watchCore.user.getUserAvatar();
console.log('用户头像', userAvatar);
console.log('默认头像', DEFAULT_VIEWER_AVATAR);
```

## 十、获取当前用户的微信 openId

用于获取当前用户的微信 openId，仅在微信(非)静默授权后才返回

**Api 方法：** `getUserOpenId(): undefined | string`

**返回值说明：** 微信 openId，`undefined | string` 类型

**示例：**

```js
const openId = watchCore.user.getUserOpenId();
console.log('openId', openId);
```

## 十一、获取当前用户的微信 unionId

用于获取当前用户的微信 unionId，仅在微信(非)静默授权后才返回

**Api 方法：** `getUserUnionId(): undefined | string`

**返回值说明：** 微信 unionId，`undefined | string` 类型

**示例：**

```js
const unionId = watchCore.user.getUserUnionId();
console.log('unionId', unionId);
```

## 十二、获取当前用户的授权方式

用户获取当前用户的授权方式，未授权时返回 AuthType.None

**Api 方法：** `getUserAuthType(): AuthType`

**返回值说明：** 授权方式，`AuthType` 类型

**示例：**

```js
const authType = watchCore.user.getUserAuthType();
console.log('当前用户的授权方式', authType);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://polyv.gitbook.io/document/docs/live/miniprogram/live-watch-miniprogram-sdk/articles/modules/user/user-info.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
