particularly complete forum function, add point order export and user delete

This commit is contained in:
2024-03-22 01:47:55 +08:00
parent 87df8d5966
commit 932b83ddcd
52 changed files with 2806 additions and 132 deletions

View File

@@ -19,23 +19,34 @@ export async function QueryPostAPIWithParams<T>(
contentType?: string,
headers?: [string, string][],
): Promise<APIRoot<T>> {
return await QueryPostAPIWithParamsInternal<APIRoot<T>>(urlString, params, body, contentType, headers)
}
async function QueryPostAPIWithParamsInternal<T>(
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<T>(url, {
method: 'post',
headers: headers,
body: typeof body === 'string' ? body : JSON.stringify(body),
})
}
async function QueryAPIInternal<T>(url: URL, init: RequestInit) {
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>
const data = await fetch(url, init)
const result = (await data.json()) as T
return result
} catch (e) {
console.error(`[POST] API调用失败: ${e}`)
console.error(`[${init.method}] API调用失败: ${e}`)
if (!apiFail.value) {
apiFail.value = true
console.log('默认API异常, 切换至故障转移节点')
@@ -48,30 +59,34 @@ export async function QueryGetAPI<T>(
params?: any,
headers?: [string, string][],
): Promise<APIRoot<T>> {
return await QueryGetAPIInternal<APIRoot<T>>(urlString, params, headers)
}
async function QueryGetAPIInternal<T>(urlString: string, params?: any, headers?: [string, string][]) {
const url = new URL(urlString)
url.search = getParams(params)
if (cookie.value) {
headers ??= []
if (cookie.value) headers?.push(['Authorization', `Bearer ${cookie.value}`])
}
try {
const data = await fetch(url.toString(), {
method: 'get',
headers: headers,
})
const result = (await data.json()) as APIRoot<T>
return result
} catch (e) {
console.error(`[GET] API调用失败: ${e}`)
if (!apiFail.value) {
apiFail.value = true
console.log('默认API异常, 切换至故障转移节点')
}
throw e
}
return await QueryAPIInternal<T>(url, {
method: 'get',
headers: headers,
})
}
function getParams(params?: [string, string][]) {
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') || '')
@@ -81,12 +96,12 @@ function getParams(params?: [string, string][]) {
}
return resultParams.toString()
}
export async function QueryPostPaginationAPI<T>(url: string, body?: unknown): Promise<APIRoot<PaginationResponse<T>>> {
return await QueryPostAPI<PaginationResponse<T>>(url, body)
export async function QueryPostPaginationAPI<T>(url: string, body?: unknown): Promise<PaginationResponse<T>> {
return await QueryPostAPIWithParamsInternal<PaginationResponse<T>>(url, undefined, body)
}
export async function QueryGetPaginationAPI<T>(
urlString: string,
params?: unknown,
): Promise<APIRoot<PaginationResponse<T>>> {
return await QueryGetAPI<PaginationResponse<T>>(urlString, params)
export async function QueryGetPaginationAPI<T>(urlString: string, params?: unknown): Promise<PaginationResponse<T>> {
return await QueryGetAPIInternal<PaginationResponse<T>>(urlString, params)
}
export function GetHeaders(): [string, string][] {
return [['Authorization', `Bearer ${cookie.value}`]]
}