From 65ae6604862df3d4815cb7da1dbf9c840059f85e Mon Sep 17 00:00:00 2001 From: Tim Date: Fri, 8 Aug 2025 13:22:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E6=94=B9=E4=B8=BA=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E7=AB=AF=E6=B8=B2=E6=9F=93=E3=80=81=E8=A7=A3=E5=86=B3?= =?UTF-8?q?=E8=B7=B3=E8=BD=AC=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend_nuxt/components/HeaderComponent.vue | 4 +- frontend_nuxt/components/MenuComponent.vue | 8 +++- frontend_nuxt/pages/index.vue | 40 +++++++++----------- frontend_nuxt/utils/markdown.js | 12 ++++-- 4 files changed, 36 insertions(+), 28 deletions(-) diff --git a/frontend_nuxt/components/HeaderComponent.vue b/frontend_nuxt/components/HeaderComponent.vue index 7abf4ff66..3127929a6 100644 --- a/frontend_nuxt/components/HeaderComponent.vue +++ b/frontend_nuxt/components/HeaderComponent.vue @@ -76,7 +76,9 @@ export default { const router = useRouter() const goToHome = () => { - router.push('/') + router.push('/').then(() => { + window.location.reload() + }) } const search = () => { showSearch.value = true diff --git a/frontend_nuxt/components/MenuComponent.vue b/frontend_nuxt/components/MenuComponent.vue index cda57df0e..40507a531 100644 --- a/frontend_nuxt/components/MenuComponent.vue +++ b/frontend_nuxt/components/MenuComponent.vue @@ -245,13 +245,17 @@ export default { gotoCategory(c) { const value = encodeURIComponent(c.id ?? c.name) this.$router - .push({ path: '/', query: { category: value } }) + .push({ path: '/', query: { category: value } }).then(() => { + window.location.reload() + }) this.handleItemClick() }, gotoTag(t) { const value = encodeURIComponent(t.id ?? t.name) this.$router - .push({ path: '/', query: { tags: value } }) + .push({ path: '/', query: { tags: value } }).then(() => { + window.location.reload() + }) this.handleItemClick() } } diff --git a/frontend_nuxt/pages/index.vue b/frontend_nuxt/pages/index.vue index f1aa824b3..3229345e7 100644 --- a/frontend_nuxt/pages/index.vue +++ b/frontend_nuxt/pages/index.vue @@ -133,10 +133,6 @@ export default { }, async setup() { const route = useRoute() - - /** - * -------- 1. PARAMS & REFS -------- - */ const selectedCategory = ref('') if (route.query.category) { const c = decodeURIComponent(route.query.category) @@ -169,16 +165,6 @@ export default { const pageSize = 10 const allLoaded = ref(false) - /** - * -------- 2. INIT FETCH FOR SSR -------- - * 服务端渲染阶段也需要获取首页内容和选项。 - */ - await loadOptions() - await fetchContent() - - /** - * -------- 3. FETCH OPTION HELPERS -------- - */ const loadOptions = async () => { if (selectedCategory.value && !isNaN(selectedCategory.value)) { try { @@ -242,9 +228,6 @@ export default { return url } - /** - * -------- 4. FETCH CORE -------- - */ const fetchPosts = async (reset = false) => { if (reset) { page.value = 0 @@ -296,7 +279,12 @@ export default { if (isLoadingPosts.value || allLoaded.value) return try { isLoadingPosts.value = true - const res = await fetch(buildRankUrl()) + const token = getToken() + const res = await fetch(buildRankUrl(), { + headers: { + Authorization: token ? `Bearer ${token}` : '' + } + }) isLoadingPosts.value = false if (!res.ok) return const data = await res.json() @@ -333,7 +321,12 @@ export default { if (isLoadingPosts.value || allLoaded.value) return try { isLoadingPosts.value = true - const res = await fetch(buildReplyUrl()) + const token = getToken() + const res = await fetch(buildReplyUrl(), { + headers: { + Authorization: token ? `Bearer ${token}` : '' + } + }) isLoadingPosts.value = false if (!res.ok) return const data = await res.json() @@ -363,11 +356,11 @@ export default { const fetchContent = async (reset = false) => { if (selectedTopic.value === '排行榜') { - fetchRanking(reset) + await fetchRanking(reset) } else if (selectedTopic.value === '最新回复') { - fetchLatestReply(reset) + await fetchLatestReply(reset) } else { - fetchPosts(reset) + await fetchPosts(reset) } } @@ -383,6 +376,9 @@ export default { const sanitizeDescription = text => stripMarkdown(text) + await loadOptions() + await fetchContent() + return { topics, selectedTopic, diff --git a/frontend_nuxt/utils/markdown.js b/frontend_nuxt/utils/markdown.js index dbcd352b7..71de58a16 100644 --- a/frontend_nuxt/utils/markdown.js +++ b/frontend_nuxt/utils/markdown.js @@ -86,9 +86,15 @@ export function handleMarkdownClick(e) { export function stripMarkdown(text) { const html = md.render(text || '') - const el = document.createElement('div') - el.innerHTML = html - return el.textContent || el.innerText || '' + // SSR 环境下没有 document + if (typeof window === 'undefined') { + // 用正则去除 HTML 标签 + return html.replace(/<[^>]+>/g, '').replace(/\s+/g, ' ').trim() + } else { + const el = document.createElement('div') + el.innerHTML = html + return el.textContent || el.innerText || '' + } } export function stripMarkdownLength(text, length) {