| |
|
| |
|
| |
|
| | window.ADMIN_PATH = window.ADMIN_PATH || 'admin';
|
| |
|
| |
|
| | function getApiPath(path) {
|
| |
|
| | if (path.startsWith(`/${window.ADMIN_PATH}`)) {
|
| | return path;
|
| | }
|
| |
|
| | return `/${window.ADMIN_PATH}${path}`;
|
| | }
|
| |
|
| |
|
| | async function apiRequest(url, options = {}) {
|
| | try {
|
| | const response = await fetch(url, options);
|
| | if (!response.ok) {
|
| | const errorText = await response.text();
|
| | let errorMsg;
|
| | try {
|
| | const errorJson = JSON.parse(errorText);
|
| | errorMsg = errorJson.detail || errorJson.message || errorText;
|
| | } catch {
|
| | errorMsg = errorText;
|
| | }
|
| | throw new Error(`HTTP ${response.status}: ${errorMsg}`);
|
| | }
|
| | return await response.json();
|
| | } catch (error) {
|
| | console.error('API请求失败:', error);
|
| | throw error;
|
| | }
|
| | }
|
| |
|
| |
|
| | if (typeof module !== 'undefined' && module.exports) {
|
| | module.exports = { getApiPath, apiRequest };
|
| | }
|
| |
|