diff --git a/js/api.js b/js/api.js index a9abff6..6064248 100644 --- a/js/api.js +++ b/js/api.js @@ -3,15 +3,17 @@ // ============================================================ let token = localStorage.getItem('forumlify-token') || null; +function getAuthHeaders() { + return token ? { 'Authorization': 'Bearer ' + token } : {}; +} + function apiFetch(path, options = {}) { const url = CONFIG.API_BASE_URL + path; const headers = { 'Content-Type': 'application/json', + ...getAuthHeaders(), ...(options.headers || {}) }; - if (token) { - headers['Authorization'] = 'Bearer ' + token; - } return fetch(url, { ...options, headers @@ -85,7 +87,7 @@ const API = { formData.append('file', file); const response = await fetch(CONFIG.API_BASE_URL + '/upload', { method: 'POST', - headers: token ? { 'Authorization': 'Bearer ' + token } : {}, + headers: getAuthHeaders(), body: formData }); const data = await response.json().catch(() => ({ error: '上传响应无效' }));