diff --git a/src/api/account.ts b/src/api/account.ts index b13d89a..1191a9f 100644 --- a/src/api/account.ts +++ b/src/api/account.ts @@ -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() const cookies = useCookies() @@ -24,7 +23,7 @@ export function useAccount() { } export async function Register(name: string, email: string, password: string, token: string): Promise> { - return QueryPostAPI(`${ACCOUNT_URL}register`, { + return QueryPostAPI(`${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> { - return QueryPostAPI(`${ACCOUNT_URL}login`, { + return QueryPostAPI(`${ACCOUNT_API_URL}login`, { nameOrEmail, password, }) } export async function Self(): Promise> { - return QueryPostAPI(`${ACCOUNT_URL}self`) + return QueryPostAPI(`${ACCOUNT_API_URL}self`) } diff --git a/src/api/query.ts b/src/api/query.ts index 1f3c398..6a3781f 100644 --- a/src/api/query.ts +++ b/src/api/query.ts @@ -1,12 +1,18 @@ /* eslint-disable indent */ import { APIRoot, PaginationResponse } from './api-models' -export async function QueryPostAPI(url: string, body?: unknown): Promise> { +export async function QueryPostAPI(url: string, body?: unknown, headers?: any): Promise> { + 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 diff --git a/src/api/user.ts b/src/api/user.ts index 7363ab6..5690a93 100644 --- a/src/api/user.ts +++ b/src/api/user.ts @@ -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> { - return QueryGetAPI(`${ACCOUNT_URL}info`, { + return QueryGetAPI(`${USER_API_URL}info`, { uId: uId, }) } diff --git a/src/components/RegisterAndLogin.vue b/src/components/RegisterAndLogin.vue index b98fe3a..ee4b80c 100644 --- a/src/components/RegisterAndLogin.vue +++ b/src/components/RegisterAndLogin.vue @@ -1,6 +1,9 @@ diff --git a/src/data/constants.ts b/src/data/constants.ts index 02d57ec..59db512 100644 --- a/src/data/constants.ts +++ b/src/data/constants.ts @@ -3,4 +3,5 @@ const releseAPI = `${document.location.protocol}//api.vtsuru.live/` export const BASE_API = process.env.NODE_ENV === 'development' ? debugAPI : releseAPI export const FETCH_API = 'https://fetch.vtsuru.live/' -export const USER_URL = `${BASE_API}user/` +export const USER_API_URL = `${BASE_API}user/` +export const ACCOUNT_API_URL = `${BASE_API}account/` diff --git a/src/main.ts b/src/main.ts index d17552e..da7707e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -6,4 +6,4 @@ import { GetSelfAccount } from './api/account' createApp(App).use(store).use(router).mount('#app') -await GetSelfAccount() +GetSelfAccount() diff --git a/src/views/ViewerLayout.vue b/src/views/ViewerLayout.vue index 8c24ed3..1376abd 100644 --- a/src/views/ViewerLayout.vue +++ b/src/views/ViewerLayout.vue @@ -6,7 +6,7 @@ import { BookOutline as BookIcon, PersonOutline as PersonIcon, WineOutline as Wi import { GetInfo, useUser } from '@/api/user' import { useRoute } from 'vue-router' import { UserInfo } from '@/api/api-models' -import { FETCH_API, USER_URL } from '@/data/constants' +import { FETCH_API } from '@/data/constants' import { useAccount } from '@/api/account' const route = useRoute() @@ -56,7 +56,7 @@ onMounted(async () => {