Files
webRtc/WebApp/client/public/onebyone/api.js
2026-03-03 17:51:30 +08:00

156 lines
4.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* API客户端
* 封装所有API调用处理HTTP请求
*/
class ApiClient {
constructor(baseUrl = '') {
this.baseUrl = baseUrl || location.origin;
}
/**
* 获取通话信息
* @param {string} callId - 通话ID
* @returns {Promise<Object>} 通话信息
*/
async getCallInfo(callId) {
try {
const response = await fetch(`${this.baseUrl}/api/call/${callId}`);
if (!response.ok) {
throw new Error(`API error: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('Error getting call info:', error);
throw error;
}
}
/**
* 加入通话
* @param {string} callId - 通话ID
* @returns {Promise<Object>} 加入结果
*/
async joinCall(callId) {
try {
const response = await fetch(`${this.baseUrl}/api/call/${callId}/join`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
});
if (!response.ok) {
throw new Error(`API error: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('Error joining call:', error);
throw error;
}
}
/**
* 离开通话
* @param {string} callId - 通话ID
* @returns {Promise<Object>} 离开结果
*/
async leaveCall(callId) {
try {
const response = await fetch(`${this.baseUrl}/api/call/${callId}/leave`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
});
if (!response.ok) {
throw new Error(`API error: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('Error leaving call:', error);
throw error;
}
}
/**
* 更新媒体状态
* @param {string} callId - 通话ID
* @param {Object} mediaState - 媒体状态 {audio?: boolean, video?: boolean}
* @returns {Promise<Object>} 更新结果
*/
async updateMediaState(callId, mediaState) {
try {
const response = await fetch(`${this.baseUrl}/api/call/${callId}/media`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(mediaState)
});
if (!response.ok) {
throw new Error(`API error: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('Error updating media state:', error);
throw error;
}
}
/**
* 获取历史消息
* @param {string} callId - 通话ID
* @param {number} limit - 消息数量限制
* @param {string} before - 时间戳
* @returns {Promise<Array>} 消息列表
*/
async getMessages(callId, limit = 50, before = null) {
try {
let url = `${this.baseUrl}/api/call/${callId}/messages?limit=${limit}`;
if (before) {
url += `&before=${before}`;
}
const response = await fetch(url);
if (!response.ok) {
throw new Error(`API error: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('Error getting messages:', error);
throw error;
}
}
/**
* 发送消息
* @param {string} callId - 通话ID
* @param {string} content - 消息内容
* @param {string} type - 消息类型
* @returns {Promise<Object>} 发送结果
*/
async sendMessage(callId, content, type = 'text') {
try {
const response = await fetch(`${this.baseUrl}/api/call/${callId}/message`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ content, type })
});
if (!response.ok) {
throw new Error(`API error: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('Error sending message:', error);
throw error;
}
}
}
// 创建单例实例
const apiClient = new ApiClient();
export default apiClient;