This commit is contained in:
Megghy
2023-06-07 16:01:12 +08:00
parent 4b456d2ec4
commit 2e14596be6
7 changed files with 149 additions and 60 deletions

View File

@@ -1,11 +1,10 @@
import { BASE_API } from '@/data/constants'
import { ACCOUNT_API_URL, 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>()
const cookies = useCookies()
@@ -24,7 +23,7 @@ export function useAccount() {
}
export async function Register(name: string, email: string, password: string, token: string): Promise<APIRoot<string>> {
return QueryPostAPI<string>(`${ACCOUNT_URL}register`, {
return QueryPostAPI<string>(`${ACCOUNT_API_URL}register`, {
name,
email,
password,
@@ -33,11 +32,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_URL}login`, {
return QueryPostAPI<string>(`${ACCOUNT_API_URL}login`, {
nameOrEmail,
password,
})
}
export async function Self(): Promise<APIRoot<UserInfo>> {
return QueryPostAPI<UserInfo>(`${ACCOUNT_URL}self`)
return QueryPostAPI<UserInfo>(`${ACCOUNT_API_URL}self`)
}

View File

@@ -1,12 +1,18 @@
/* eslint-disable indent */
import { APIRoot, PaginationResponse } from './api-models'
export async function QueryPostAPI<T>(url: string, body?: unknown): Promise<APIRoot<T>> {
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 data = await fetch(url, {
method: 'post',
headers: {
'Content-Type': 'application/json',
},
headers: headers,
body: JSON.stringify(body),
}) // 不处理异常, 在页面处理
return (await data.json()) as APIRoot<T>

View File

@@ -1,9 +1,8 @@
import { QueryGetAPI } from '@/api/query'
import { BASE_API } from '@/data/constants'
import { BASE_API, USER_API_URL } 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) {
@@ -17,7 +16,7 @@ export async function useUser(uId: number) {
}
export async function GetInfo(uId: number): Promise<APIRoot<UserInfo>> {
return QueryGetAPI<UserInfo>(`${ACCOUNT_URL}info`, {
return QueryGetAPI<UserInfo>(`${USER_API_URL}info`, {
uId: uId,
})
}