api.js
2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* API 调用封装
*/
const API = {
baseUrl: '/api',
/**
* 通用请求方法
*/
async request(endpoint, options = {}) {
const url = `${this.baseUrl}${endpoint}`;
try {
const response = await fetch(url, {
...options,
headers: {
'Content-Type': 'application/json',
...options.headers,
},
});
if (!response.ok) {
const error = await response.json().catch(() => ({}));
throw new Error(error.detail || `请求失败: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('API Error:', error);
throw error;
}
},
/**
* GET 请求
*/
async get(endpoint, params = {}) {
const queryString = new URLSearchParams(params).toString();
const url = queryString ? `${endpoint}?${queryString}` : endpoint;
return this.request(url);
},
/**
* 获取任务列表
*/
async getTasks(params = {}) {
return this.get('/tasks', params);
},
/**
* 获取任务详情
*/
async getTask(taskNo) {
return this.get(`/tasks/${taskNo}`);
},
/**
* 获取任务配件明细
*/
async getTaskDetails(taskNo, params = {}) {
return this.get(`/tasks/${taskNo}/details`, params);
},
/**
* 获取统计摘要
*/
async getStatsSummary() {
return this.get('/stats/summary');
},
/**
* 获取任务执行日志
*/
async getTaskLogs(taskNo) {
return this.get(`/tasks/${taskNo}/logs`);
},
/**
* 获取任务配件汇总列表
*/
async getPartSummaries(taskNo, params = {}) {
return this.get(`/tasks/${taskNo}/part-summaries`, params);
},
/**
* 获取配件的门店明细
*/
async getPartShopDetails(taskNo, partCode) {
return this.get(`/tasks/${taskNo}/parts/${encodeURIComponent(partCode)}/shops`);
},
};
// 导出到全局
window.API = API;