保利威文档中心

幫助中心

使用者資訊

更新時間:2024-10-11 15:26:25

本文件主要提供 用户模块(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 類型

範例:

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

三、設定微信基礎資訊

透過 wx.login 取得臨時登入憑證 code,並在伺服端取得使用者身份(openId、unionId)後,呼叫該方法儲存,詳細可見小程式官方文件:小程式登入

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

參數說明:

  • info:微信基礎資訊,WxMiniBaseInfo 類型,必傳,詳細類型說明如下
參數名稱 說明 類型 必須 預設值
openId 微信 openId string -
unionId 微信 unionId string -

範例:

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 方法儲存,詳細可見小程式官方文件:頭像暱稱填寫

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

參數說明:

  • info:使用者資訊,WxMiniUserInfo 類型,必傳,詳細類型說明如下
參數名稱 說明 類型 必須 預設值
nickname 微信暱稱 string -
avatar 微信頭像 string -

範例:

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

五、取得小程式使用者資訊

Api 方法: getWxMiniUserInfo(): undefined | WxMiniUserInfo

回傳值說明: 微信使用者資訊,undefined | WxMiniUserInfo 類型

六、判斷是否為特殊的使用者身份

使用者模組提供 Api 用於判斷使用者身份是否為內建的特殊使用者身份,如講師、管理員、助教等。

Api 方法: isSpecialUserType(userType: unknown): boolean

參數說明:

  • userType:使用者身份,unknown 類型,必傳

回傳值說明: 是否特殊身份

範例:

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 類型

範例:

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

八、取得目前使用者暱稱

用於取得目前使用者的暱稱。

未進行觀看條件授權或建立 SDK 沒有傳入使用者資訊時回傳 undefined

Api 方法: getUserNick(): undefined | string

回傳值說明: 使用者暱稱,undefined | string 類型

範例:

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

九、取得目前使用者頭像位址

用於取得目前使用者的頭像位址,觀看頁 SDK 提供 DEFAULT_VIEWER_AVATAR 預設觀眾頭像的常數。

未進行觀看條件授權或建立 SDK 沒有傳入使用者資訊時回傳 undefined

Api 方法: getUserAvatar(): undefined | string

回傳值說明: 使用者頭像位址,undefined | string 類型

範例:

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 類型

範例:

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

十一、取得目前使用者的微信 unionId

用於取得目前使用者的微信 unionId,僅在微信(非)靜默授權後才回傳

Api 方法: getUserUnionId(): undefined | string

回傳值說明: 微信 unionId,undefined | string 類型

範例:

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

十二、取得目前使用者的授權方式

用於取得目前使用者的授權方式,未授權時回傳 AuthType.None

Api 方法: getUserAuthType(): AuthType

回傳值說明: 授權方式,AuthType 類型

範例:

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