From cfce4d7d1dfbb7c38de93d6a567a6c82c91a026b Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 21 Aug 2025 10:22:33 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=85=A8=E5=B1=80=E7=A7=BB=E9=99=A4proc?= =?UTF-8?q?ess.client=E3=80=81process.server=20#669?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend_nuxt/components/GlobalPopups.vue | 16 ++++++++-------- frontend_nuxt/components/InfiniteLoadMore.vue | 2 +- frontend_nuxt/components/TagSelect.vue | 2 +- frontend_nuxt/composables/useToast.js | 10 +++++----- frontend_nuxt/pages/index.vue | 2 +- frontend_nuxt/pages/posts/[id]/index.vue | 9 +++++---- frontend_nuxt/plugins/auth-fetch.client.ts | 2 +- frontend_nuxt/plugins/toastification.client.ts | 2 +- frontend_nuxt/utils/auth.js | 12 ++++++------ 9 files changed, 29 insertions(+), 28 deletions(-) diff --git a/frontend_nuxt/components/GlobalPopups.vue b/frontend_nuxt/components/GlobalPopups.vue index 3c31e8d9c..90a0f00c7 100644 --- a/frontend_nuxt/components/GlobalPopups.vue +++ b/frontend_nuxt/components/GlobalPopups.vue @@ -50,7 +50,7 @@ onMounted(async () => { }) const checkMilkTeaActivity = async () => { - if (!process.client) return + if (!import.meta.client) return if (localStorage.getItem('milkTeaActivityPopupShown')) return try { const res = await fetch(`${API_BASE_URL}/api/activities`) @@ -68,7 +68,7 @@ const checkMilkTeaActivity = async () => { } const checkInviteCodeActivity = async () => { - if (!process.client) return + if (!import.meta.client) return if (localStorage.getItem('inviteCodeActivityPopupShown')) return try { const res = await fetch(`${API_BASE_URL}/api/activities`) @@ -86,30 +86,30 @@ const checkInviteCodeActivity = async () => { } const closeInviteCodePopup = () => { - if (!process.client) return + if (!import.meta.client) return localStorage.setItem('inviteCodeActivityPopupShown', 'true') showInviteCodePopup.value = false } const closeMilkTeaPopup = () => { - if (!process.client) return + if (!import.meta.client) return localStorage.setItem('milkTeaActivityPopupShown', 'true') showMilkTeaPopup.value = false } const checkNotificationSetting = async () => { - if (!process.client) return + if (!import.meta.client) return if (!authState.loggedIn) return if (localStorage.getItem('notificationSettingPopupShown')) return showNotificationPopup.value = true } const closeNotificationPopup = () => { - if (!process.client) return + if (!import.meta.client) return localStorage.setItem('notificationSettingPopupShown', 'true') showNotificationPopup.value = false } const checkNewMedals = async () => { - if (!process.client) return + if (!import.meta.client) return if (!authState.loggedIn || !authState.userId) return try { const res = await fetch(`${API_BASE_URL}/api/medals?userId=${authState.userId}`) @@ -127,7 +127,7 @@ const checkNewMedals = async () => { } } const closeMedalPopup = () => { - if (!process.client) return + if (!import.meta.client) return const seen = new Set(JSON.parse(localStorage.getItem('seenMedals') || '[]')) newMedals.value.forEach((m) => seen.add(m.type)) localStorage.setItem('seenMedals', JSON.stringify([...seen])) diff --git a/frontend_nuxt/components/InfiniteLoadMore.vue b/frontend_nuxt/components/InfiniteLoadMore.vue index 441c0ecf4..c4e1a9e0a 100644 --- a/frontend_nuxt/components/InfiniteLoadMore.vue +++ b/frontend_nuxt/components/InfiniteLoadMore.vue @@ -40,7 +40,7 @@ const stopObserver = () => { } const startObserver = () => { - if (!process.client || props.pause || done.value) return + if (!import.meta.client || props.pause || done.value) return stopObserver() io = new IntersectionObserver( async (entries) => { diff --git a/frontend_nuxt/components/TagSelect.vue b/frontend_nuxt/components/TagSelect.vue index b80f506a2..14de024a7 100644 --- a/frontend_nuxt/components/TagSelect.vue +++ b/frontend_nuxt/components/TagSelect.vue @@ -63,7 +63,7 @@ const isImageIcon = (icon) => { } const buildTagsUrl = (kw = '') => { - const base = API_BASE_URL || (process.client ? window.location.origin : '') + const base = API_BASE_URL || (import.meta.client ? window.location.origin : '') const url = new URL('/api/tags', base) if (kw) url.searchParams.set('keyword', kw) diff --git a/frontend_nuxt/composables/useToast.js b/frontend_nuxt/composables/useToast.js index 900aa1f74..e8daff063 100644 --- a/frontend_nuxt/composables/useToast.js +++ b/frontend_nuxt/composables/useToast.js @@ -1,7 +1,7 @@ // 导出一个便捷的 toast 对象 export const toast = { success: async (message) => { - if (process.client) { + if (import.meta.client) { try { const { useToast } = await import('vue-toastification') const toastInstance = useToast() @@ -12,7 +12,7 @@ export const toast = { } }, error: async (message) => { - if (process.client) { + if (import.meta.client) { try { const { useToast } = await import('vue-toastification') const toastInstance = useToast() @@ -23,7 +23,7 @@ export const toast = { } }, warning: async (message) => { - if (process.client) { + if (import.meta.client) { try { const { useToast } = await import('vue-toastification') const toastInstance = useToast() @@ -34,7 +34,7 @@ export const toast = { } }, info: async (message) => { - if (process.client) { + if (import.meta.client) { try { const { useToast } = await import('vue-toastification') const toastInstance = useToast() @@ -48,7 +48,7 @@ export const toast = { // 导出 useToast composable export const useToast = () => { - if (process.client) { + if (import.meta.client) { return new Promise(async (resolve) => { try { const { useToast: useVueToast } = await import('vue-toastification') diff --git a/frontend_nuxt/pages/index.vue b/frontend_nuxt/pages/index.vue index 476ce7757..fd0434dc4 100644 --- a/frontend_nuxt/pages/index.vue +++ b/frontend_nuxt/pages/index.vue @@ -338,7 +338,7 @@ watch([selectedCategory, selectedTags], () => { watch(selectedTopic, (val) => { loadOptions() selectedTopicCookie.value = val - if (process.client) localStorage.setItem('homeTab', val) + if (import.meta.client) localStorage.setItem('homeTab', val) }) /** 选项首屏加载:服务端执行一次;客户端兜底 **/ diff --git a/frontend_nuxt/pages/posts/[id]/index.vue b/frontend_nuxt/pages/posts/[id]/index.vue index 45f4e9bbe..33ecd8f61 100644 --- a/frontend_nuxt/pages/posts/[id]/index.vue +++ b/frontend_nuxt/pages/posts/[id]/index.vue @@ -295,7 +295,7 @@ const commentSort = ref('NEWEST') const isFetchingComments = ref(false) const isMobile = useIsMobile() -const headerHeight = process.client +const headerHeight = import.meta.client ? parseFloat(getComputedStyle(document.documentElement).getPropertyValue('--header-height')) || 0 : 0 @@ -309,7 +309,7 @@ useHead(() => ({ ], })) -if (process.client) { +if (import.meta.client) { onBeforeUnmount(() => { window.removeEventListener('scroll', updateCurrentIndex) if (countdownTimer) clearInterval(countdownTimer) @@ -355,7 +355,7 @@ const updateCountdown = () => { countdown.value = `${h}:${m}:${s}` } const startCountdown = () => { - if (!process.client) return + if (!import.meta.client) return if (countdownTimer) clearInterval(countdownTimer) updateCountdown() countdownTimer = setInterval(updateCountdown, 1000) @@ -515,7 +515,7 @@ watchEffect(() => { }) // 404 客户端跳转 -// if (postError.value?.statusCode === 404 && process.client) { +// if (postError.value?.statusCode === 404 && import.meta.client) { // router.replace('/404') // } @@ -877,6 +877,7 @@ const gotoProfile = () => { } const initPage = async () => { + scrollTo(0, 0) await fetchComments() const hash = location.hash const id = hash.startsWith('#comment-') ? hash.substring('#comment-'.length) : null diff --git a/frontend_nuxt/plugins/auth-fetch.client.ts b/frontend_nuxt/plugins/auth-fetch.client.ts index 0a07440b8..dae8d6e3b 100644 --- a/frontend_nuxt/plugins/auth-fetch.client.ts +++ b/frontend_nuxt/plugins/auth-fetch.client.ts @@ -1,7 +1,7 @@ import { clearToken } from '~/utils/auth' export default defineNuxtPlugin(() => { - if (process.client) { + if (import.meta.client) { const originalFetch = window.fetch window.fetch = async (input, init) => { const response = await originalFetch(input, init) diff --git a/frontend_nuxt/plugins/toastification.client.ts b/frontend_nuxt/plugins/toastification.client.ts index 2a9fd3f31..789a8f5a9 100644 --- a/frontend_nuxt/plugins/toastification.client.ts +++ b/frontend_nuxt/plugins/toastification.client.ts @@ -4,7 +4,7 @@ import '~/assets/toast.css' export default defineNuxtPlugin(async (nuxtApp) => { // 确保只在客户端环境中注册插件 - if (process.client) { + if (import.meta.client) { try { // 使用动态导入来避免 CommonJS 模块问题 const { default: Toast, POSITION } = await import('vue-toastification') diff --git a/frontend_nuxt/utils/auth.js b/frontend_nuxt/utils/auth.js index a4f87e15e..6fb558c13 100644 --- a/frontend_nuxt/utils/auth.js +++ b/frontend_nuxt/utils/auth.js @@ -12,7 +12,7 @@ export const authState = reactive({ role: null, }) -if (process.client) { +if (import.meta.client) { authState.loggedIn = localStorage.getItem(TOKEN_KEY) !== null && localStorage.getItem(TOKEN_KEY) !== '' authState.userId = localStorage.getItem(USER_ID_KEY) @@ -21,18 +21,18 @@ if (process.client) { } export function getToken() { - return process.client ? localStorage.getItem(TOKEN_KEY) : null + return import.meta.client ? localStorage.getItem(TOKEN_KEY) : null } export function setToken(token) { - if (process.client) { + if (import.meta.client) { localStorage.setItem(TOKEN_KEY, token) authState.loggedIn = true } } export function clearToken() { - if (process.client) { + if (import.meta.client) { localStorage.removeItem(TOKEN_KEY) clearUserInfo() authState.loggedIn = false @@ -40,7 +40,7 @@ export function clearToken() { } export function setUserInfo({ id, username }) { - if (process.client) { + if (import.meta.client) { authState.userId = id authState.username = username if (arguments[0] && arguments[0].role) { @@ -53,7 +53,7 @@ export function setUserInfo({ id, username }) { } export function clearUserInfo() { - if (process.client) { + if (import.meta.client) { localStorage.removeItem(USER_ID_KEY) localStorage.removeItem(USERNAME_KEY) localStorage.removeItem(ROLE_KEY)