133 lines
3.7 KiB
JavaScript
Raw Normal View History

2025-04-28 12:25:20 +08:00
/**
* API操作模块
* @type {module}
*/
/**
* 加载模型文件列表
* @returns {Promise<Array>} 模型文件数组
*/
export async function loadModelFiles() {
try {
const response = await fetch('/api/model-files');
if (!response.ok) {
throw new Error(`服务器响应错误: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('加载模型文件失败:', error);
throw error;
}
}
/**
* 加载文件内容
* @param {string} filePath - 文件路径
* @returns {Promise<string>} 文件内容
*/
export async function loadFileContent(filePath) {
try {
const response = await fetch(`/api/model-file-content?path=${encodeURIComponent(filePath)}`);
if (!response.ok) {
throw new Error(`服务器响应错误: ${response.status}`);
}
return await response.text();
} catch (error) {
console.error('加载文件内容失败:', error);
throw error;
}
}
/**
* 保存文件内容
* @param {string} filePath - 文件路径
* @param {string} content - 文件内容
* @returns {Promise<Object>} 保存结果
*/
export async function saveFileContent(filePath, content) {
try {
const response = await fetch('/api/save-model-file', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
path: filePath,
content: content
})
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error || `保存失败: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('保存文件失败:', error);
throw error;
}
}
/**
* 创建新的模型配置文件
* @param {string} fileName - 文件名
* @param {boolean} overwrite - 是否覆盖现有文件
* @returns {Promise<Object>} 创建结果
*/
export async function createNewConfig(fileName, overwrite = false) {
try {
const response = await fetch('/api/create-model-file', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ fileName, overwrite })
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error || `创建失败: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('创建新配置文件失败:', error);
throw error;
}
}
/**
* 另存为新文件
* @param {string} fileName - 文件名
* @param {string} content - 文件内容
* @param {string} currentFile - 当前文件路径
* @param {boolean} overwrite - 是否覆盖现有文件
* @returns {Promise<Object>} 保存结果
*/
export async function saveFileAs(fileName, content, currentFile, overwrite = false) {
try {
const response = await fetch('/api/save-model-as', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
fileName,
content,
currentFile,
overwrite
})
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error || `保存失败: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('另存为失败:', error);
throw error;
}
}