update url

This commit is contained in:
2023-12-14 12:56:28 +08:00
parent 4ab1f6da4f
commit 82a0e72122
39 changed files with 299 additions and 350 deletions

View File

@@ -1,48 +1,73 @@
/* eslint-disable indent */
import { useLocalStorage } from '@vueuse/core'
import { APIRoot, PaginationResponse } from './api-models'
import { Cookies20Regular } from '@vicons/fluent'
import { apiFail } from '@/data/constants'
const cookie = useLocalStorage('JWT_Token', '')
let failCount = 0
export async function QueryPostAPI<T>(url: string, body?: unknown, headers?: [string, string][]): Promise<APIRoot<T>> {
headers ??= []
if (cookie.value) headers?.push(['Authorization', `Bearer ${cookie.value}`])
headers?.push(['Content-Type', 'application/json'])
const data = await fetch(url, {
method: 'post',
headers: headers,
body: typeof body === 'string' ? body : JSON.stringify(body),
}) // 不处理异常, 在页面处理
return (await data.json()) as APIRoot<T>
export async function QueryPostAPI<T>(urlString: string, body?: unknown, headers?: [string, string][]): Promise<APIRoot<T>> {
return await QueryPostAPIWithParams<T>(urlString, undefined, body, 'application/json', headers)
}
export async function QueryPostAPIWithParams<T>(urlString: string, params?: any, body?: any, contentType?: string, headers?: [string, string][]): Promise<APIRoot<T>> {
const url = new URL(urlString)
url.search = new URLSearchParams(params).toString()
url.search = getParams(params)
headers ??= []
headers?.push(['Authorization', `Bearer ${cookie.value}`])
if (contentType) headers?.push(['Content-Type', contentType])
const data = await fetch(url, {
method: 'post',
headers: headers,
body: body,
}) // 不处理异常, 在页面处理
return (await data.json()) as APIRoot<T>
try {
const data = await fetch(url, {
method: 'post',
headers: headers,
body: typeof body === 'string' ? body : JSON.stringify(body),
})
const result = (await data.json()) as APIRoot<T>
failCount = 0
return result
} catch (e) {
console.error(`[POST] API调用失败: ${e}`)
failCount++
if (failCount > 3 && !apiFail.value) {
apiFail.value = true
console.log('默认API异常, 切换至故障转移节点')
}
throw e
}
}
export async function QueryGetAPI<T>(urlString: string, params?: any, headers?: [string, string][]): Promise<APIRoot<T>> {
const url = new URL(urlString)
url.search = new URLSearchParams(params).toString()
url.search = getParams(params)
if (cookie.value) {
headers ??= []
headers?.push(['Authorization', `Bearer ${cookie.value}`])
}
const data = await fetch(url.toString(), {
method: 'get',
headers: headers,
}) // 不处理异常, 在页面处理
return (await data.json()) as APIRoot<T>
try {
const data = await fetch(url.toString(), {
method: 'get',
headers: headers,
})
const result = (await data.json()) as APIRoot<T>
failCount = 0
return result
} catch (e) {
console.error(`[GET] API调用失败: ${e}`)
failCount++
if (failCount > 3 && !apiFail.value) {
apiFail.value = true
console.log('默认API异常, 切换至故障转移节点')
}
throw e
}
}
function getParams(params?: [string, string][]) {
const urlParams = new URLSearchParams(window.location.search)
const resultParams = new URLSearchParams(params)
if (urlParams.has('as')) {
resultParams.set('as', urlParams.get('as') || '')
}
return resultParams.toString()
}
export async function QueryPostPaginationAPI<T>(url: string, body?: unknown): Promise<APIRoot<PaginationResponse<T>>> {
return await QueryPostAPI<PaginationResponse<T>>(url, body)