This commit is contained in:
2023-06-02 23:53:07 +08:00
parent afb2e49345
commit 4dedacf449
31 changed files with 1368 additions and 1236 deletions

20
src/api/account.ts Normal file
View File

@@ -0,0 +1,20 @@
import QueryAPI from '@/api/query'
import { BASE_API } from '@/data/constants'
import { APIRoot } from './api-models'
const ACCOUNT_URL = `${BASE_API}account/`
export async function Register(name: string, email: string, password: string): Promise<APIRoot<string>> {
return QueryAPI<string>(`${ACCOUNT_URL}register`, {
name,
email,
password,
})
}
export async function Login(nameOrEmail: string, password: string): Promise<APIRoot<string>> {
return QueryAPI<string>(`${ACCOUNT_URL}login`, {
nameOrEmail,
password,
})
}

5
src/api/api-models.ts Normal file
View File

@@ -0,0 +1,5 @@
export interface APIRoot<T> {
code: number;
message: string;
data: T;
}

13
src/api/query.ts Normal file
View File

@@ -0,0 +1,13 @@
/* eslint-disable indent */
import { APIRoot } 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>;
}