This commit is contained in:
2023-06-11 08:47:10 +08:00
parent 2e14596be6
commit 1b6bb7c248
15 changed files with 395 additions and 134 deletions

View File

@@ -1,20 +1,30 @@
import { ACCOUNT_API_URL, BASE_API } from '@/data/constants'
import { APIRoot } from './api-models'
import { APIRoot, AccountInfo } from './api-models'
import { QueryPostAPI } from '@/api/query'
import { UserInfo } from '@/api/api-models'
import { ref } from 'vue'
import { useCookies } from '@vueuse/integrations/useCookies'
import { useLocalStorage } from '@vueuse/core'
import { createDiscreteApi } from 'naive-ui'
export const ACCOUNT = ref<UserInfo>()
export const ACCOUNT = ref<AccountInfo>()
const cookies = useCookies()
const { message } = createDiscreteApi(['message'])
const cookie = useLocalStorage('JWT_Token', '')
export async function GetSelfAccount() {
const cookie = cookies.get('VTSURU_SESSION')
if (cookie) {
if (cookie.value) {
const result = await Self()
if (result.code == 200) {
ACCOUNT.value = result.data
console.log('[vtsuru] 已获取账户信息')
return result.data
} else if (result.code == 403) {
cookie.value = ''
console.warn('[vtsuru] Cookie 已失效, 需要重新登陆')
message.error('Cookie 已失效, 需要重新登陆')
}
else {
console.warn('[vtsuru] '+ result.message)
message.error(result.message)
}
}
}
@@ -37,6 +47,6 @@ export async function Login(nameOrEmail: string, password: string): Promise<APIR
password,
})
}
export async function Self(): Promise<APIRoot<UserInfo>> {
return QueryPostAPI<UserInfo>(`${ACCOUNT_API_URL}self`)
export async function Self(): Promise<APIRoot<AccountInfo>> {
return QueryPostAPI<AccountInfo>(`${ACCOUNT_API_URL}self`)
}

View File

@@ -12,12 +12,17 @@ export interface PaginationResponse<T> {
}
export interface UserInfo {
name: string
uId: number
id: number
createAt: number
biliId?: number
biliRoomId?: number
}
export interface AccountInfo extends UserInfo {
isRoomValid: boolean
isEmailVerified: boolean
isBiliVerified: boolean
enableFunctions: string[]
biliVerifyCode?: string
emailVerifyUrl?: string
}
export interface SongsInfo {
id: string

View File

@@ -1,15 +1,14 @@
/* eslint-disable indent */
import { useLocalStorage } from '@vueuse/core'
import { APIRoot, PaginationResponse } from './api-models'
export async function QueryPostAPI<T>(url: string, body?: unknown, headers?: any): Promise<APIRoot<T>> {
if (headers) {
headers['Content-Type'] = 'application/json'
}
else {
headers = {
'Content-Type': 'application/json',
}
}
const cookie = useLocalStorage('JWT_Token', '')
export async function QueryPostAPI<T>(url: string, body?: unknown, headers?: [string, string][]): Promise<APIRoot<T>> {
headers ??= []
headers?.push(['Authorization', `Bearer ${cookie.value}`])
headers?.push(['Content-Type', 'application/json'])
const data = await fetch(url, {
method: 'post',
headers: headers,
@@ -17,10 +16,17 @@ export async function QueryPostAPI<T>(url: string, body?: unknown, headers?: any
}) // 不处理异常, 在页面处理
return (await data.json()) as APIRoot<T>
}
export async function QueryGetAPI<T>(urlString: string, params?: any): Promise<APIRoot<T>> {
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()
const data = await fetch(url.toString()) // 不处理异常, 在页面处理
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>
}
export async function QueryPostPaginationAPI<T>(url: string, body?: unknown): Promise<APIRoot<PaginationResponse<T>>> {