| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- import axios from 'axios';
- // Token API 相关类型定义
- interface IMTokenResponse {
- nonce: string;
- token: string;
- appId: string;
- appSign: string;
- timestamp: number;
- }
- interface RTCTokenResponse {
- token: string;
- appId: string;
- timestamp: number;
- }
- interface TokenError {
- message: string;
- code?: number;
- }
- // 课堂相关API
- export const ClassroomAPI = {
- /**
- * 获取IM Token
- * @param userId 用户ID
- * @param role 用户角色 (admin/student)
- * @returns Promise<string> 返回IM Token
- * @throws {TokenError} 获取失败时抛出错误
- */
- getIMToken: async (userId: string, role: string): Promise<IMTokenResponse> => {
- try {
- const response = await axios.post(`/classroom/im_token`, {
- userId,
- role
- });
- if (!response.data.token) {
- throw new Error('Invalid token response');
- }
- return response.data;
- } catch (error) {
- console.error('Failed to get IM token:', error);
- throw new Error('Failed to get IM token');
- }
- },
- /**
- * 获取RTC Token
- * @param channelId 频道ID
- * @param userId 用户ID
- * @returns Promise<string> 返回RTC Token
- * @throws {TokenError} 获取失败时抛出错误
- */
- getRTCToken: async (channelId: string, userId: string): Promise<RTCTokenResponse> => {
- try {
- const response = await axios.post(`/classroom/rtc_token`, {
- channelId,
- userId
- });
- if (!response.data.token) {
- throw new Error('Invalid token response');
- }
- return response.data;
- } catch (error) {
- console.error('Failed to get RTC token:', error);
- throw new Error('Failed to get RTC token');
- }
- }
- };
|