classroom.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import axios from 'axios';
  2. // Token API 相关类型定义
  3. interface IMTokenResponse {
  4. nonce: string;
  5. token: string;
  6. appId: string;
  7. appSign: string;
  8. timestamp: number;
  9. }
  10. interface RTCTokenResponse {
  11. token: string;
  12. appId: string;
  13. timestamp: number;
  14. }
  15. interface TokenError {
  16. message: string;
  17. code?: number;
  18. }
  19. // 课堂相关API
  20. export const ClassroomAPI = {
  21. /**
  22. * 获取IM Token
  23. * @param userId 用户ID
  24. * @param role 用户角色 (admin/student)
  25. * @returns Promise<string> 返回IM Token
  26. * @throws {TokenError} 获取失败时抛出错误
  27. */
  28. getIMToken: async (userId: string, role: string): Promise<IMTokenResponse> => {
  29. try {
  30. const response = await axios.post(`/classroom/im_token`, {
  31. userId,
  32. role
  33. });
  34. if (!response.data.token) {
  35. throw new Error('Invalid token response');
  36. }
  37. return response.data;
  38. } catch (error) {
  39. console.error('Failed to get IM token:', error);
  40. throw new Error('Failed to get IM token');
  41. }
  42. },
  43. /**
  44. * 获取RTC Token
  45. * @param channelId 频道ID
  46. * @param userId 用户ID
  47. * @returns Promise<string> 返回RTC Token
  48. * @throws {TokenError} 获取失败时抛出错误
  49. */
  50. getRTCToken: async (channelId: string, userId: string): Promise<RTCTokenResponse> => {
  51. try {
  52. const response = await axios.post(`/classroom/rtc_token`, {
  53. channelId,
  54. userId
  55. });
  56. if (!response.data.token) {
  57. throw new Error('Invalid token response');
  58. }
  59. return response.data;
  60. } catch (error) {
  61. console.error('Failed to get RTC token:', error);
  62. throw new Error('Failed to get RTC token');
  63. }
  64. }
  65. };