This commit is contained in:
Megghy
2023-06-05 15:31:37 +08:00
parent 4dedacf449
commit 981d873225
30 changed files with 16609 additions and 5653 deletions

View File

@@ -1,20 +1,43 @@
import QueryAPI from '@/api/query'
import { BASE_API } from '@/data/constants'
import { APIRoot } from './api-models'
import { QueryPostAPI } from '@/api/query'
import { UserInfo } from '@/api/api-models'
import { ref } from 'vue'
import { useCookies } from '@vueuse/integrations/useCookies'
const ACCOUNT_URL = `${BASE_API}account/`
export const ACCOUNT = ref<UserInfo>()
export async function Register(name: string, email: string, password: string): Promise<APIRoot<string>> {
return QueryAPI<string>(`${ACCOUNT_URL}register`, {
const cookies = useCookies()
export async function GetSelfAccount() {
const cookie = cookies.get('VTSURU_SESSION')
if (cookie) {
const result = await Self()
if (result.code == 200) {
ACCOUNT.value = result.data
}
}
}
export function useAccount() {
return ACCOUNT
}
export async function Register(name: string, email: string, password: string, token: string): Promise<APIRoot<string>> {
return QueryPostAPI<string>(`${ACCOUNT_URL}register`, {
name,
email,
password,
token,
})
}
export async function Login(nameOrEmail: string, password: string): Promise<APIRoot<string>> {
return QueryAPI<string>(`${ACCOUNT_URL}login`, {
return QueryPostAPI<string>(`${ACCOUNT_URL}login`, {
nameOrEmail,
password,
})
}
export async function Self(): Promise<APIRoot<UserInfo>> {
return QueryPostAPI<UserInfo>(`${ACCOUNT_URL}self`)
}

View File

@@ -1,5 +1,32 @@
export interface APIRoot<T> {
code: number;
message: string;
data: T;
code: number
message: string
data: T
}
export interface PaginationResponse<T> {
total: number
index: number
size: number
hasMore: boolean
datas: T
}
export interface UserInfo {
name: string
uId: number
createAt: number
}
export interface AccountInfo extends UserInfo {
isRoomValid: boolean
enableFunctions: string[]
}
export interface SongsInfo {
id: string
name: string
author: string
url: string
cover: string
from: string
language: string
desc: string
tags: string[]
}

View File

@@ -1,13 +1,25 @@
/* eslint-disable indent */
import { APIRoot } from './api-models';
import { APIRoot, PaginationResponse } from './api-models'
export default async function QueryAPI<T>(url: string, body?: unknown): Promise<APIRoot<T>> {
const data = await fetch(url,{
method: 'post',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
}); // 不处理异常, 在页面处理
return await data.json() as APIRoot<T>;
export async function QueryPostAPI<T>(url: string, body?: unknown): Promise<APIRoot<T>> {
const data = await fetch(url, {
method: 'post',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
}) // 不处理异常, 在页面处理
return (await data.json()) as APIRoot<T>
}
export async function QueryGetAPI<T>(urlString: string, params?: any): Promise<APIRoot<T>> {
const url = new URL(urlString)
url.search = new URLSearchParams(params).toString()
const data = await fetch(url.toString()) // 不处理异常, 在页面处理
return (await data.json()) as APIRoot<T>
}
export async function QueryPostPaginationAPI<T>(url: string, body?: unknown): Promise<APIRoot<PaginationResponse<T>>> {
return await QueryPostAPI<PaginationResponse<T>>(url, body)
}
export async function QueryGetPaginationAPI<T>(urlString: string, params?: any): Promise<APIRoot<PaginationResponse<T>>> {
return await QueryGetAPI<PaginationResponse<T>>(urlString, params)
}

23
src/api/user.ts Normal file
View File

@@ -0,0 +1,23 @@
import { QueryGetAPI } from '@/api/query'
import { BASE_API } from '@/data/constants'
import { APIRoot, UserInfo } from './api-models'
import { ref } from 'vue'
const ACCOUNT_URL = `${BASE_API}user/`
export const USERS = ref<{ [uId: number]: UserInfo }>({})
export async function useUser(uId: number) {
if (!USERS.value[uId]) {
const result = await GetInfo(uId)
if (result.code == 200) {
USERS.value[uId] = result.data
}
}
return USERS.value[uId]
}
export async function GetInfo(uId: number): Promise<APIRoot<UserInfo>> {
return QueryGetAPI<UserInfo>(`${ACCOUNT_URL}info`, {
uId: uId,
})
}