feat: 积分商城-供应商-对账-发票3
This commit is contained in:
@@ -1,14 +1,23 @@
|
||||
import { clearAdminSession, readAdminSession } from '@/utils/auth'
|
||||
|
||||
const API_BASE = import.meta.env.VITE_SUPPLIED_ADMIN_API_BASE || 'http://127.0.0.1:8888/api/admin'
|
||||
|
||||
const normalizeBase = (base: string) => base.replace(/\/$/, '')
|
||||
|
||||
export const postAdmin = async <T>(name: string, body: Record<string, unknown>): Promise<T> => {
|
||||
const token = readAdminSession()?.token
|
||||
const response = await fetch(`${normalizeBase(API_BASE)}/${name}`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
headers: { 'Content-Type': 'application/json', ...(token ? { Authorization: `Bearer ${token}` } : {}) },
|
||||
body: JSON.stringify(body)
|
||||
})
|
||||
const payload = await response.json().catch(() => ({}))
|
||||
if (response.status === 401 && name !== 'login') {
|
||||
clearAdminSession()
|
||||
if (name !== 'admin-session' && window.location.pathname !== '/login') {
|
||||
window.location.replace(`/login?redirect=${encodeURIComponent(window.location.pathname + window.location.search)}`)
|
||||
}
|
||||
}
|
||||
if (!response.ok || payload.success === false) {
|
||||
throw new Error(payload.error?.message || payload.msg || '请求失败')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user