| |
| |
| |
| |
|
|
| |
| const API_BASE_URL = '/api'; |
|
|
| |
| |
| |
| |
| function getToken() { |
| return localStorage.getItem('access_token'); |
| } |
|
|
| |
| |
| |
| |
| function saveToken(token) { |
| localStorage.setItem('access_token', token); |
| } |
|
|
| |
| |
| |
| function clearToken() { |
| localStorage.removeItem('access_token'); |
| } |
|
|
| |
| |
| |
| |
| function isLoggedIn() { |
| return !!getToken(); |
| } |
|
|
| |
| |
| |
| |
| |
| function getAuthHeaders(customHeaders = {}) { |
| const token = getToken(); |
| return { |
| 'Content-Type': 'application/json', |
| ...(token && { 'Authorization': `Bearer ${token}` }), |
| ...customHeaders |
| }; |
| } |
|
|
| |
| |
| |
| |
| |
| async function handleResponse(response) { |
| |
| let data; |
| const contentType = response.headers.get('content-type'); |
|
|
| if (contentType && contentType.includes('application/json')) { |
| try { |
| data = await response.json(); |
| } catch (error) { |
| |
| data = {}; |
| } |
| } else { |
| |
| data = await response.text(); |
| } |
|
|
| |
| if (!response.ok) { |
| const error = new Error(data.detail || data.message || `HTTP错误: ${response.status}`); |
| error.status = response.status; |
| error.data = data; |
| throw error; |
| } |
|
|
| return data; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| async function apiRequest(endpoint, options = {}) { |
| const { |
| method = 'GET', |
| body = null, |
| headers = {}, |
| requireAuth = true |
| } = options; |
|
|
| |
| const url = `${API_BASE_URL}${endpoint}`; |
|
|
| |
| const config = { |
| method: method.toUpperCase(), |
| headers: requireAuth ? getAuthHeaders(headers) : { |
| 'Content-Type': 'application/json', |
| ...headers |
| } |
| }; |
|
|
| |
| if (body && (method.toUpperCase() === 'POST' || method.toUpperCase() === 'PUT' || method.toUpperCase() === 'PATCH')) { |
| config.body = JSON.stringify(body); |
| } |
|
|
| try { |
| const response = await fetch(url, config); |
| return await handleResponse(response); |
| } catch (error) { |
| |
| if (error.status === 401) { |
| clearToken(); |
| |
| const currentPath = window.location.pathname; |
| if (currentPath !== '/auth') { |
| const redirectUrl = encodeURIComponent(window.location.href); |
| window.location.href = `/auth?redirect=${redirectUrl}`; |
| } |
| } |
| throw error; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| function apiGet(endpoint, options = {}) { |
| return apiRequest(endpoint, { ...options, method: 'GET' }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| function apiPost(endpoint, body, options = {}) { |
| return apiRequest(endpoint, { ...options, method: 'POST', body }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| function apiPut(endpoint, body, options = {}) { |
| return apiRequest(endpoint, { ...options, method: 'PUT', body }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| function apiPatch(endpoint, body, options = {}) { |
| return apiRequest(endpoint, { ...options, method: 'PATCH', body }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| function apiDelete(endpoint, options = {}) { |
| return apiRequest(endpoint, { ...options, method: 'DELETE' }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| async function apiUploadFile(endpoint, file, options = {}) { |
| const { requireAuth = true } = options; |
|
|
| |
| const url = `${API_BASE_URL}${endpoint}`; |
|
|
| |
| const formData = new FormData(); |
| formData.append('file', file); |
|
|
| |
| const headers = {}; |
| if (requireAuth) { |
| const token = getToken(); |
| if (token) { |
| headers['Authorization'] = `Bearer ${token}`; |
| } |
| } |
|
|
| const config = { |
| method: 'POST', |
| headers: headers, |
| body: formData |
| }; |
|
|
| try { |
| const response = await fetch(url, config); |
| return await handleResponse(response); |
| } catch (error) { |
| |
| if (error.status === 401) { |
| clearToken(); |
| const currentPath = window.location.pathname; |
| if (currentPath !== '/auth') { |
| const redirectUrl = encodeURIComponent(window.location.href); |
| window.location.href = `/auth?redirect=${redirectUrl}`; |
| } |
| } |
| throw error; |
| } |
| } |
|
|
| |
| if (typeof module !== 'undefined' && module.exports) { |
| module.exports = { |
| apiRequest, |
| apiGet, |
| apiPost, |
| apiPut, |
| apiPatch, |
| apiDelete, |
| apiUploadFile, |
| getToken, |
| saveToken, |
| clearToken, |
| isLoggedIn, |
| getAuthHeaders |
| }; |
| } |
|
|