【m】优化拆分,拆分为多个脚本

This commit is contained in:
zhangzheng
2026-03-03 17:51:30 +08:00
parent c717ee4d8d
commit a0ab9b44f1
11 changed files with 1827 additions and 1983 deletions

View File

@@ -0,0 +1,155 @@
/**
* 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;