支持公私网访问

This commit is contained in:
stardrophere
2026-03-13 13:14:55 +08:00
parent 6fbcf2c81b
commit 9440b7f590
4 changed files with 126 additions and 12 deletions
+8 -8
View File
@@ -3,8 +3,7 @@
*/
import { useAuthStore } from '@/stores/auth'
import { pinia } from '@/stores'
const API_BASE = (import.meta.env.VITE_API_BASE_URL as string | undefined) ?? '/api/v1'
import { fetchApi } from '@/config/apiBase'
// 后端返回的错误消息中英映射
const MESSAGE_MAP: Record<string, string> = {
@@ -57,21 +56,22 @@ async function handleResponse<T>(response: Response): Promise<T> {
/** GET 请求 */
export async function apiGet<T>(path: string, params?: Record<string, string | number>): Promise<T> {
let url = `${API_BASE}${path}`
let requestPath = path
if (params) {
const searchParams = new URLSearchParams()
for (const [key, value] of Object.entries(params)) {
searchParams.set(key, String(value))
}
url += `?${searchParams.toString()}`
const separator = requestPath.includes('?') ? '&' : '?'
requestPath += `${separator}${searchParams.toString()}`
}
const response = await fetch(url, { method: 'GET', headers: getAuthHeaders() })
const response = await fetchApi(requestPath, { method: 'GET', headers: getAuthHeaders() })
return handleResponse<T>(response)
}
/** POST 请求 */
export async function apiPost<T>(path: string, body?: unknown): Promise<T> {
const response = await fetch(`${API_BASE}${path}`, {
const response = await fetchApi(path, {
method: 'POST',
headers: getAuthHeaders(),
body: body !== undefined ? JSON.stringify(body) : undefined,
@@ -81,7 +81,7 @@ export async function apiPost<T>(path: string, body?: unknown): Promise<T> {
/** PATCH 请求 */
export async function apiPatch<T>(path: string, body: unknown): Promise<T> {
const response = await fetch(`${API_BASE}${path}`, {
const response = await fetchApi(path, {
method: 'PATCH',
headers: getAuthHeaders(),
body: JSON.stringify(body),
@@ -91,7 +91,7 @@ export async function apiPatch<T>(path: string, body: unknown): Promise<T> {
/** DELETE 请求 */
export async function apiDelete(path: string): Promise<void> {
const response = await fetch(`${API_BASE}${path}`, {
const response = await fetchApi(path, {
method: 'DELETE',
headers: getAuthHeaders(),
})