备份:修复前完整项目快照 2026-04-19
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
// API配置
|
||||
import { useAuthStore } from '../store/authStore'
|
||||
|
||||
export const API_CONFIG = {
|
||||
baseURL: '/api',
|
||||
timeout: 10000,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
}
|
||||
|
||||
// 获取 auth token
|
||||
const getAuthToken = () => {
|
||||
try {
|
||||
// 从 localStorage 获取 token
|
||||
const authData = localStorage.getItem('auth-storage')
|
||||
if (authData) {
|
||||
const parsed = JSON.parse(authData)
|
||||
return parsed.state?.token || null
|
||||
}
|
||||
} catch (e) {
|
||||
// 忽略解析错误
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
// 创建带认证的 fetch 封装
|
||||
export const authFetch = async (url: string, options: RequestInit = {}) => {
|
||||
const token = getAuthToken()
|
||||
|
||||
const headers = new Headers(options.headers)
|
||||
headers.set('Content-Type', 'application/json')
|
||||
|
||||
if (token) {
|
||||
headers.set('Authorization', `Bearer ${token}`)
|
||||
}
|
||||
|
||||
const response = await fetch(url, {
|
||||
...options,
|
||||
headers
|
||||
})
|
||||
|
||||
// 处理 401 未授权
|
||||
if (response.status === 401) {
|
||||
// 清除登录状态
|
||||
localStorage.removeItem('auth-storage')
|
||||
window.location.href = '/login'
|
||||
throw new Error('登录已过期,请重新登录')
|
||||
}
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
// API端点
|
||||
export const API_ENDPOINTS = {
|
||||
auth: {
|
||||
login: '/auth/login',
|
||||
logout: '/auth/logout',
|
||||
verify: '/auth/verify',
|
||||
me: '/auth/me',
|
||||
},
|
||||
products: '/products',
|
||||
customers: '/customers',
|
||||
suppliers: '/suppliers',
|
||||
advances: '/advances',
|
||||
reimbursements: '/reimbursements',
|
||||
projects: '/projects',
|
||||
paymentNodes: '/payment-nodes',
|
||||
paymentRecords: '/payment-records',
|
||||
exchangeRates: '/exchange-rates',
|
||||
financeStats: '/finance-stats',
|
||||
users: '/users',
|
||||
}
|
||||
Reference in New Issue
Block a user