From 4e5bcaad94d6ff749d9159ff0e8cbe7670a78c04 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Tue, 13 Aug 2024 16:23:33 +0800 Subject: [PATCH] :lipstick: Optimize userinfo loading process --- components/UserMenu.vue | 23 ++++++++++++----------- pages/users/me.vue | 8 ++++---- stores/userinfo.ts | 16 +++++++--------- 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/components/UserMenu.vue b/components/UserMenu.vue index 6fcff17..038807e 100755 --- a/components/UserMenu.vue +++ b/components/UserMenu.vue @@ -2,15 +2,12 @@ - - - - - + @@ -18,6 +15,10 @@ + + + + @@ -30,21 +31,21 @@ const config = useRuntimeConfig() const id = useUserinfo() const username = computed(() => { - if (id.userinfo.isLoggedIn) { - return "@" + id.userinfo.data?.name + if (id.isLoggedIn) { + return "@" + id.userinfo?.name } else { return "@visitor" } }) const nickname = computed(() => { - if (id.userinfo.isLoggedIn) { - return id.userinfo.data?.nick + if (id.isLoggedIn) { + return id.userinfo?.nick } else { return "Anonymous" } }) const avatar = computed(() => { - return id.userinfo.data?.avatar ? `${config.public.solarNetworkApi}/cgi/files/attachments/${id.userinfo.data?.avatar}` : void 0 + return id.userinfo?.avatar ? `${config.public.solarNetworkApi}/cgi/files/attachments/${id.userinfo?.avatar}` : void 0 }) function signOut() { diff --git a/pages/users/me.vue b/pages/users/me.vue index 3fe3e4d..326ee55 100644 --- a/pages/users/me.vue +++ b/pages/users/me.vue @@ -6,8 +6,8 @@
- {{ auth.userinfo.data?.nick }} @{{ auth.userinfo.data?.name }} - {{ auth.userinfo.data?.description }} + {{ auth.userinfo?.nick }} @{{ auth.userinfo?.name }} + {{ auth.userinfo?.description }}
@@ -51,6 +51,6 @@ const config = useRuntimeConfig() const auth = useUserinfo() -const urlOfAvatar = computed(() => auth.userinfo.data?.avatar ? `${config.public.solarNetworkApi}/cgi/files/attachments/${auth.userinfo.data.avatar}` : void 0) -const urlOfBanner = computed(() => auth.userinfo.data?.banner ? `${config.public.solarNetworkApi}/cgi/files/attachments/${auth.userinfo.data.banner}` : void 0) +const urlOfAvatar = computed(() => auth.userinfo?.avatar ? `${config.public.solarNetworkApi}/cgi/files/attachments/${auth.userinfo.avatar}` : void 0) +const urlOfBanner = computed(() => auth.userinfo?.banner ? `${config.public.solarNetworkApi}/cgi/files/attachments/${auth.userinfo.banner}` : void 0) diff --git a/stores/userinfo.ts b/stores/userinfo.ts index b1d5954..2bf8210 100644 --- a/stores/userinfo.ts +++ b/stores/userinfo.ts @@ -27,8 +27,9 @@ export function useLoggedInState() { } export const useUserinfo = defineStore("userinfo", () => { - const userinfo = ref(defaultUserinfo) + const userinfo = ref(null) const isReady = ref(false) + const isLoggedIn = ref(false) const lastRefreshedAt = ref(null) @@ -39,6 +40,7 @@ export const useUserinfo = defineStore("userinfo", () => { } async function getAtk() { + if (!useLoggedInState().value) return useAtk().value if (lastRefreshedAt.value != null && Math.floor(Math.abs(Date.now() - lastRefreshedAt.value.getTime()) / 60000) < 3) { return useAtk().value } @@ -69,23 +71,19 @@ export const useUserinfo = defineStore("userinfo", () => { isReady.value = true } - const config = useRuntimeConfig() - const res = await solarFetch("/cgi/auth/users/me") if (res.status !== 200) { + isReady.value = true return } const data = await res.json() + isLoggedIn.value = true isReady.value = true - userinfo.value = { - isLoggedIn: true, - displayName: data["nick"], - data: data, - } + userinfo.value = data } - return { userinfo, lastRefreshedAt, isReady, setTokenSet, getAtk, readProfiles } + return { userinfo, lastRefreshedAt, isLoggedIn, isReady, setTokenSet, getAtk, readProfiles } })