diff --git a/src/api/user.ts b/src/api/user.ts index 5690a93..236eeb2 100644 --- a/src/api/user.ts +++ b/src/api/user.ts @@ -2,21 +2,37 @@ import { QueryGetAPI } from '@/api/query' import { BASE_API, USER_API_URL } from '@/data/constants' import { APIRoot, UserInfo } from './api-models' import { ref } from 'vue' +import { useRoute } from 'vue-router' export const USERS = ref<{ [uId: number]: UserInfo }>({}) -export async function useUser(uId: number) { +export async function useUserByUId(uId: number) { if (!USERS.value[uId]) { - const result = await GetInfo(uId) + const result = await QueryGetAPI(`${USER_API_URL}info`, { + uId: uId, + }) if (result.code == 200) { USERS.value[uId] = result.data } } return USERS.value[uId] } - -export async function GetInfo(uId: number): Promise> { - return QueryGetAPI(`${USER_API_URL}info`, { - uId: uId, - }) +export async function useUser(id?: number) { + try { + if (!id) { + id = Number(useRoute().params.id) + } + } catch { } + if (id) { + if (!USERS.value[id]) { + const result = await QueryGetAPI(`${USER_API_URL}info`, { + id: id, + }) + if (result.code == 200) { + USERS.value[id] = result.data + } + } + return USERS.value[id] + } + } diff --git a/src/views/ViewerLayout.vue b/src/views/ViewerLayout.vue index e734822..fa807a3 100644 --- a/src/views/ViewerLayout.vue +++ b/src/views/ViewerLayout.vue @@ -3,14 +3,14 @@ import { NAvatar, NCard, NIcon, NLayout, NLayoutFooter, NLayoutHeader, NLayoutSider, NMenu, NSpace, NText, NButton, NEmpty, NResult, NPageHeader, NSwitch, useOsTheme } from 'naive-ui' import { computed, h, onMounted, ref } from 'vue' import { BookOutline as BookIcon, PersonOutline as PersonIcon, WineOutline as WineIcon } from '@vicons/ionicons5' -import { GetInfo, useUser } from '@/api/user' +import { useUser, useUserByUId } from '@/api/user' import { useRoute } from 'vue-router' import { UserInfo } from '@/api/api-models' import { FETCH_API } from '@/data/constants' import { useAccount } from '@/api/account' const route = useRoute() -const uId = computed(() => { +const id = computed(() => { return Number(route.params.id) }) const theme = useOsTheme() @@ -35,7 +35,7 @@ const menuOptions = [ }, ] async function RequestBiliUserData() { - await fetch(FETCH_API + `https://account.bilibili.com/api/member/getCardByMid?mid=${uId.value}`) + await fetch(FETCH_API + `https://account.bilibili.com/api/member/getCardByMid?mid=${id.value}`) .then(async (respone) => { let data = await respone.json() if (data.code == 0) { @@ -51,12 +51,12 @@ async function RequestBiliUserData() { onMounted(async () => { await RequestBiliUserData() - userInfo.value = await useUser(uId.value) + userInfo.value = await useUser(id.value) })