/* eslint-disable indent */ import { apiFail } from '@/data/constants' import { useLocalStorage } from '@vueuse/core' import { APIRoot, PaginationResponse } from './api-models' const cookie = useLocalStorage('JWT_Token', '') export async function QueryPostAPI( urlString: string, body?: unknown, headers?: [string, string][], ): Promise> { return await QueryPostAPIWithParams(urlString, undefined, body, 'application/json', headers) } export async function QueryPostAPIWithParams( urlString: string, params?: any, body?: any, contentType?: string, headers?: [string, string][], ): Promise> { return await QueryPostAPIWithParamsInternal>(urlString, params, body, contentType, headers) } async function QueryPostAPIWithParamsInternal( urlString: string, params?: any, body?: any, contentType: string = 'application/json', headers: [string, string][] = [], ) { const url = new URL(urlString) url.search = getParams(params) headers ??= [] if (cookie.value) headers?.push(['Authorization', `Bearer ${cookie.value}`]) if (contentType) headers?.push(['Content-Type', contentType]) return await QueryAPIInternal(url, { method: 'post', headers: headers, body: typeof body === 'string' ? body : JSON.stringify(body), }) } async function QueryAPIInternal(url: URL, init: RequestInit) { try { const data = await fetch(url, init) const result = (await data.json()) as T return result } catch (e) { console.error(`[${init.method}] API调用失败: ${e}`) if (!apiFail.value) { apiFail.value = true console.log('默认API异常, 切换至故障转移节点') } throw e } } export async function QueryGetAPI( urlString: string, params?: any, headers?: [string, string][], ): Promise> { return await QueryGetAPIInternal>(urlString, params, headers) } async function QueryGetAPIInternal(urlString: string, params?: any, headers?: [string, string][]) { try { const url = new URL(urlString) url.search = getParams(params) if (cookie.value) { headers ??= [] if (cookie.value) headers?.push(['Authorization', `Bearer ${cookie.value}`]) } return await QueryAPIInternal(url, { method: 'get', headers: headers, }) } catch (err) { console.log(`url:${urlString}, error:${err}`) throw err } } function getParams(params: any) { const urlParams = new URLSearchParams(window.location.search) if (params) { const keys = Object.keys(params) if (keys.length > 0) { keys.forEach((k) => { if (params[k] == undefined) { delete params[k] } }) } } const resultParams = new URLSearchParams(params) if (urlParams.has('as')) { resultParams.set('as', urlParams.get('as') || '') } if (urlParams.has('token')) { resultParams.set('token', urlParams.get('token') || '') } return resultParams.toString() } export async function QueryPostPaginationAPI(url: string, body?: unknown): Promise> { return await QueryPostAPIWithParamsInternal>(url, undefined, body) } export async function QueryGetPaginationAPI(urlString: string, params?: unknown): Promise> { return await QueryGetAPIInternal>(urlString, params) } export function GetHeaders(): [string, string][] { return [['Authorization', `Bearer ${cookie.value}`]] }