复现已有算法
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
/**
|
||||
* @file api.js
|
||||
* @description REST API 封装(原生 fetch + 统一响应信封 {status,msg,data})
|
||||
* 参考: edge_collector/frontend/cloud_app/src/utils/api.js(精简版,无鉴权)
|
||||
*/
|
||||
|
||||
const BASE_URL = '/api';
|
||||
|
||||
/**
|
||||
* 通用请求封装
|
||||
* @param {string} url - 请求路径(相对 /api)
|
||||
* @param {object} options - fetch 选项
|
||||
* @returns {Promise<object>} 响应 data 字段
|
||||
*/
|
||||
async function request(url, options = {}) {
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
...(options.headers || {}),
|
||||
};
|
||||
|
||||
const resp = await fetch(`${BASE_URL}${url}`, {
|
||||
cache: 'no-store',
|
||||
...options,
|
||||
headers,
|
||||
});
|
||||
const text = await resp.text();
|
||||
let json;
|
||||
try {
|
||||
json = JSON.parse(text);
|
||||
} catch {
|
||||
throw new Error(`服务器返回非法响应 (HTTP ${resp.status}),请检查后端服务状态`);
|
||||
}
|
||||
|
||||
// 业务失败
|
||||
if (json.status !== 0) {
|
||||
throw new Error(json.msg || '请求失败');
|
||||
}
|
||||
return json.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取服务端版本信息
|
||||
* @returns {Promise<{name: string, version: string}>}
|
||||
*/
|
||||
export function getVersion() {
|
||||
return request('/system/version');
|
||||
}
|
||||
|
||||
/**
|
||||
* 健康检查
|
||||
* @returns {Promise<{status: string}>}
|
||||
*/
|
||||
export function getHealth() {
|
||||
return request('/system/health');
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建风功率计算任务
|
||||
* @param {{files: Array<{file_name: string, row_count: number}>, mapping: object}} payload
|
||||
* @returns {Promise<{job_id: string}>}
|
||||
*/
|
||||
export function startWindJob(payload) {
|
||||
return request('/wind/jobs/start', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传标准化后的数据分片
|
||||
* @param {{job_id: string, chunk_index: number, rows: Array<object>}} payload
|
||||
* @returns {Promise<{accepted_rows: number}>}
|
||||
*/
|
||||
export function uploadWindChunk(payload) {
|
||||
return request('/wind/jobs/chunk', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 完成任务并计算功率曲线
|
||||
* @param {{job_id: string, options: object}} payload
|
||||
* @returns {Promise<object>}
|
||||
*/
|
||||
export function finishWindJob(payload) {
|
||||
return request('/wind/jobs/finish', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理未完成的计算任务
|
||||
* @param {string} jobId
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export function deleteWindJob(jobId) {
|
||||
return request(`/wind/jobs/${encodeURIComponent(jobId)}`, {
|
||||
method: 'DELETE',
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user