Polyv Help Center

Help Center

User Information

Updated: 2024-10-11 15:26:25

This document primarily provides the API description for user information provided by 用户模块(user).

1. Viewer Detailed Information

Interface: UserInfoDetail

Property Name Description Type
userId User ID string
nick Nickname string
pic Avatar string
openId WeChat openId string
unionId WeChat unionId string
wxNickname WeChat Nickname string
wxAvatar WeChat Avatar string
authType User's Authorization Method AuthType

2. Get Current User Information

Used to obtain detailed information about the current user.

Returns undefined if viewing conditions are not authorized or no user information was passed when creating the SDK.

Api Method: getUserInfo(): undefined | UserInfoDetail

Return Value Description: User information, type undefined | UserInfoDetail

Example:

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

3. Set WeChat Basic Information

Obtain the temporary login credential code via wx.login, retrieve the user identity (openId, unionId) on the server side, then call this method to save it. For details, see the Mini Program official documentation: Mini Program Login.

Api Method: setWxMiniBaseInfo(info: WxMiniBaseInfo): Promise<void>

Parameter Description:

  • info: WeChat basic information, type WxMiniBaseInfo, required. Detailed type description is as follows:
Parameter Name Description Type Required Default Value
openId WeChat openId string Yes -
unionId WeChat unionId string Yes -

Example:

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,
    });
  }
});

4. Set Mini Program User Information

Before entering the viewing page, obtain the Mini Program user information and call the setWxMiniUserInfo method to save it. For details, see the Mini Program official documentation: Nickname and Avatar Filling.

Api Method: setWxMiniUserInfo(info: WxMiniUserInfo): Promise<void>

Parameter Description:

  • info: User information, type WxMiniUserInfo, required. Detailed type description is as follows:
Parameter Name Description Type Required Default Value
nickname WeChat Nickname string Yes -
avatar WeChat Avatar string Yes -

Example:

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

5. Get Mini Program User Information

Api Method: getWxMiniUserInfo(): undefined | WxMiniUserInfo

Return Value Description: WeChat user information, type undefined | WxMiniUserInfo

6. Determine if it is a Special User Role

The user module provides an API to determine if the user role is a built-in special user role, such as lecturer, administrator, teaching assistant, etc.

Api Method: isSpecialUserType(userType: unknown): boolean

Parameter Description:

  • userType: User role, type unknown, required

Return Value Description: Whether it is a special role

Example:

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

7. Get Current User userId

Used to obtain the current user's userId. The default userId is a timestamp string. Notes on userId:

  • In a WeChat environment, after WeChat authorization, the user's WeChat openid is used as the userId;
  • After whitelist authorization, the entered membership code is used as the userId (priority over WeChat authorization);

Returns undefined if viewing conditions are not authorized or no user information was passed when creating the SDK.

Api Method: getUserId(): undefined | string

Return Value Description: User userId, type undefined | string

Example:

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

8. Get Current User Nickname

Used to obtain the current user's nickname.

Returns undefined if viewing conditions are not authorized or no user information was passed when creating the SDK.

Api Method: getUserNick(): undefined | string

Return Value Description: User nickname, type undefined | string

Example:

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

9. Get Current User Avatar URL

Used to obtain the current user's avatar URL. The viewing page SDK provides the constant DEFAULT_VIEWER_AVATAR for the default viewer avatar.

Returns undefined if viewing conditions are not authorized or no user information was passed when creating the SDK.

Api Method: getUserAvatar(): undefined | string

Return Value Description: User avatar URL, type undefined | string

Example:

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

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

10. Get Current User's WeChat openId

Used to obtain the current user's WeChat openId. It is only returned after (non-)silent WeChat authorization.

Api Method: getUserOpenId(): undefined | string

Return Value Description: WeChat openId, type undefined | string

Example:

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

11. Get Current User's WeChat unionId

Used to obtain the current user's WeChat unionId. It is only returned after (non-)silent WeChat authorization.

Api Method: getUserUnionId(): undefined | string

Return Value Description: WeChat unionId, type undefined | string

Example:

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

12. Get Current User's Authorization Method

Used to obtain the current user's authorization method. Returns AuthType.None if not authorized.

Api Method: getUserAuthType(): AuthType

Return Value Description: Authorization method, type AuthType

Example:

const authType = watchCore.user.getUserAuthType();
console.log('当前用户的授权方式', authType);
联系客服,在线咨询
在线咨询