mirror of
https://github.com/Megghy/vtsuru.live.git
synced 2025-12-07 02:46:55 +08:00
update url
This commit is contained in:
@@ -39,7 +39,7 @@ export async function GetSelfAccount() {
|
||||
isLoadingAccount.value = false
|
||||
}
|
||||
function refreshCookie() {
|
||||
QueryPostAPI<string>(`${ACCOUNT_API_URL}refresh-token`).then((data) => {
|
||||
QueryPostAPI<string>(`${ACCOUNT_API_URL()}refresh-token`).then((data) => {
|
||||
if (data.code == 200) {
|
||||
cookie.value = data.data
|
||||
cookieRefreshDate.value = Date.now()
|
||||
@@ -48,17 +48,17 @@ function refreshCookie() {
|
||||
})
|
||||
}
|
||||
export async function SaveAccountSettings() {
|
||||
return await QueryPostAPI(ACCOUNT_API_URL + 'update-setting', ACCOUNT.value?.settings)
|
||||
return await QueryPostAPI(ACCOUNT_API_URL() + 'update-setting', ACCOUNT.value?.settings)
|
||||
}
|
||||
export async function SaveEnableFunctions(functions: FunctionTypes[]) {
|
||||
return await QueryPostAPI(ACCOUNT_API_URL + 'update-enable-functions', functions)
|
||||
return await QueryPostAPI(ACCOUNT_API_URL() + 'update-enable-functions', functions)
|
||||
}
|
||||
export function useAccount() {
|
||||
return ACCOUNT
|
||||
}
|
||||
|
||||
export async function Register(name: string, email: string, password: string, token: string): Promise<APIRoot<string>> {
|
||||
return QueryPostAPI<string>(`${ACCOUNT_API_URL}register`, {
|
||||
return QueryPostAPI<string>(`${ACCOUNT_API_URL()}register`, {
|
||||
name,
|
||||
email,
|
||||
password,
|
||||
@@ -67,11 +67,11 @@ export async function Register(name: string, email: string, password: string, to
|
||||
}
|
||||
|
||||
export async function Login(nameOrEmail: string, password: string): Promise<APIRoot<string>> {
|
||||
return QueryPostAPI<string>(`${ACCOUNT_API_URL}login`, {
|
||||
return QueryPostAPI<string>(`${ACCOUNT_API_URL()}login`, {
|
||||
nameOrEmail,
|
||||
password,
|
||||
})
|
||||
}
|
||||
export async function Self(): Promise<APIRoot<AccountInfo>> {
|
||||
return QueryPostAPI<AccountInfo>(`${ACCOUNT_API_URL}self`)
|
||||
return QueryPostAPI<AccountInfo>(`${ACCOUNT_API_URL()}self`)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { QueryGetAPI } from '@/api/query'
|
||||
import { BASE_API, USER_API_URL } from '@/data/constants'
|
||||
import { BASE_API, USER_API_URL, apiFail } from '@/data/constants'
|
||||
import { APIRoot, UserInfo } from './api-models'
|
||||
import { ref } from 'vue'
|
||||
import { useRouteParams } from '@vueuse/router'
|
||||
@@ -12,7 +12,13 @@ export async function useUser(id: string | undefined = undefined) {
|
||||
id ??= route.params.id.toString()
|
||||
if (id) {
|
||||
if (!USERS.value[id]) {
|
||||
const result = await GetInfo(id)
|
||||
let result: APIRoot<UserInfo>
|
||||
try {
|
||||
result = await GetInfo(id)
|
||||
} catch {
|
||||
apiFail.value = true
|
||||
result = await GetInfo(id)
|
||||
}
|
||||
if (result.code == 200) {
|
||||
USERS.value[id] = result.data
|
||||
}
|
||||
@@ -24,7 +30,7 @@ export async function useUser(id: string | undefined = undefined) {
|
||||
}
|
||||
export async function useUserWithUId(id: number) {
|
||||
if (!USERS.value[id.toString()]) {
|
||||
const result = await QueryGetAPI<UserInfo>(`${USER_API_URL}info`, {
|
||||
const result = await QueryGetAPI<UserInfo>(`${USER_API_URL()}info`, {
|
||||
uId: id,
|
||||
})
|
||||
if (result.code == 200) {
|
||||
@@ -35,7 +41,7 @@ export async function useUserWithUId(id: number) {
|
||||
}
|
||||
|
||||
export async function GetInfo(id: string): Promise<APIRoot<UserInfo>> {
|
||||
return QueryGetAPI<UserInfo>(`${USER_API_URL}info`, {
|
||||
return QueryGetAPI<UserInfo>(`${USER_API_URL()}info`, {
|
||||
id: id,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user