gemercheung 6 miesięcy temu
rodzic
commit
64dd8f3d75

+ 18 - 7
packages/backend/src/modules/web/web.service.ts

@@ -161,13 +161,13 @@ export class WebService {
     const lang = this.sharedService.handleValidLang(locale);
 
     // 保留
-    // const category = await this.categoryRepo.findOne({
-    //   where: { id: id },
-    //   relations: { children: true },
-    // });
-    // const ids = await this.findCateAllChildIds(category.id);
+    const category = await this.categoryRepo.findOne({
+      where: { id: id },
+      relations: { children: true },
+    });
+    const ids = await this.findCateAllChildIds(category.id);
     // ids.push(category.id);
-
+    // console.log('ids', ids);
     const articles = await this.articleRepo.find({
       where: {
         categoryId: id,
@@ -175,8 +175,19 @@ export class WebService {
       },
       relations: { translations: true },
     });
-    return articles.map((article) => article.translate(lang));
+    const childArticles = await this.articleRepo.find({
+      where: {
+        categoryId: In(ids),
+        enable: true,
+      },
+      relations: { translations: true },
+    });
+    // console.log('childArticles', childArticles);
+    return articles.length > 0
+      ? articles.map((article) => article.translate(lang))
+      : childArticles.map((article) => article.translate(lang));
   }
+
   async getNearArticle(currentId: number, locale?: string) {
     const lang = this.sharedService.handleValidLang(locale);
     const currentEntity = await this.articleRepo.findOne({

+ 2 - 1
packages/web/src/locales/json/en.json

@@ -36,5 +36,6 @@
   "no_data": " No Data",
   "no_data_article": "others",
   "no_article": "Article not exist",
-  "go_home": "Go Homepage"
+  "go_home": "Go Homepage",
+  "home": "homepage"
 }

+ 2 - 1
packages/web/src/locales/json/zh.json

@@ -37,5 +37,6 @@
   "no_data": "无数据",
   "no_data_article": "看看别的",
   "no_article": "文章不存在",
-  "go_home": "回首页"
+  "go_home": "回首页",
+  "home": "首页"
 }

+ 9 - 1
packages/web/src/pages/showcate/[id].vue

@@ -4,8 +4,9 @@
     <div>
       <div class="breadcrumb my-[30px] pb-[20px] bb-1px_#EBEBEB" role="navigation">
         <n-breadcrumb separator=">" v-if="breadcrumb">
+          <n-breadcrumb-item @click="$router.push('/')">{{ $t('home') }}</n-breadcrumb-item>
           <template v-for="(bread, index) in breadcrumb" :key="index">
-            <n-breadcrumb-item :clickable="false"> {{ bread.title }}</n-breadcrumb-item>
+            <n-breadcrumb-item @click="handleBreadcrumb(bread)"> {{ bread.title }}</n-breadcrumb-item>
           </template>
         </n-breadcrumb>
       </div>
@@ -123,6 +124,13 @@ const handleToArticle = (article: ArticleDetailType) => {
   console.log('article', article)
   router.replace(`/showdoc/${article.id}`)
 }
+
+const handleBreadcrumb = (bread: TreeNode) => {
+  router.replace(`/showcate/${bread.id}`)
+  setTimeout(() => {
+    location.reload()
+  }, 100)
+}
 </script>
 
 <style lang="scss" scoped>

+ 37 - 30
packages/web/src/pages/showdoc/[id].vue

@@ -4,8 +4,9 @@
     <div v-if="detail">
       <div class="breadcrumb my-[30px] pb-[20px] bb-1px_#EBEBEB" role="navigation">
         <n-breadcrumb separator=">" v-if="breadcrumb">
+          <n-breadcrumb-item @click="$router.push('/')">{{ $t('home') }}</n-breadcrumb-item>
           <template v-for="(bread, index) in breadcrumb" :key="index">
-            <n-breadcrumb-item :clickable="false"> {{ bread.title }}</n-breadcrumb-item>
+            <n-breadcrumb-item @click="handleBreadcrumb(bread)"> {{ bread.title }}</n-breadcrumb-item>
           </template>
         </n-breadcrumb>
       </div>
@@ -73,7 +74,7 @@
       <n-empty :description="$t('no_article')">
         <template #extra>
           {{ $t('no_data_article') }},
-          <n-button link size="small" quaternary  type="primary" @click="router.push('/')">
+          <n-button link size="small" quaternary type="primary" @click="router.push('/')">
             {{ $t('go_home') }}
           </n-button>
         </template>
@@ -141,7 +142,7 @@ onMounted(async () => {
     }
   }, 1000)
   if (params.id) {
-    if(!isNaN(+params.id)){
+    if (!isNaN(+params.id)) {
       await getArticleCount(+params.id)
     }
   }
@@ -149,36 +150,36 @@ onMounted(async () => {
 
 watchEffect(() => {
   if (params.id) {
-    if(!isNaN(+params.id)){
+    if (!isNaN(+params.id)) {
       getArticleDetail(+params.id).then(async (data) => {
-      if (data.data) {
-        detail.value = data.data
-        document.title = detail.value.title
-        mainContents.value = htmlToTree(detail.value.content)
-        if (detail.value.categoryId) {
-
-          const res = await getCategoryTree(detail.value.categoryId)
-          if (res.data) {
-            mainCategories.value = res.data as CategoryItem[]
-            if (mainCategories.value) {
-              currentCate.value = findNodeById(
-                [mainCategories.value] as unknown as TreeNode[],
-                detail.value.categoryId,
-              ) as unknown as CategoryItem
-
-              defaultExpandedKeys.value = [currentCate.value.parentId]
-              breadcrumb.value = findBreadcrumbPath([mainCategories.value] as unknown as TreeNode[], detail.value.categoryId)
-              console.log('breadcrumb', [mainCategories.value], breadcrumb.value)
+        if (data.data) {
+          detail.value = data.data
+          document.title = detail.value.title
+          mainContents.value = htmlToTree(detail.value.content)
+          if (detail.value.categoryId) {
+
+            const res = await getCategoryTree(detail.value.categoryId)
+            if (res.data) {
+              mainCategories.value = res.data as CategoryItem[]
+              if (mainCategories.value) {
+                currentCate.value = findNodeById(
+                  [mainCategories.value] as unknown as TreeNode[],
+                  detail.value.categoryId,
+                ) as unknown as CategoryItem
+
+                defaultExpandedKeys.value = [currentCate.value.parentId]
+                breadcrumb.value = findBreadcrumbPath([mainCategories.value] as unknown as TreeNode[], detail.value.categoryId)
+                console.log('breadcrumb', [mainCategories.value], breadcrumb.value)
+              }
             }
           }
         }
-      }
-    })
-    getNearArticles(+params.id).then(res => {
-      if (res.data) {
-        articleNear.value = res.data
-      }
-    })
+      })
+      getNearArticles(+params.id).then(res => {
+        if (res.data) {
+          articleNear.value = res.data
+        }
+      })
     }
   }
 })
@@ -188,7 +189,13 @@ const handleToArticle = (article: ArticleDetailType) => {
   router.replace(`/showdoc/${article.id}`)
   setTimeout(() => {
     location.reload()
-  }, 500)
+  }, 100)
+}
+const handleBreadcrumb = (bread: TreeNode) => {
+  router.replace(`/showcate/${bread.id}`)
+  setTimeout(() => {
+    location.reload()
+  }, 100)
 }
 
 </script>

+ 1 - 1
packages/web/src/utils/tree.ts

@@ -1,5 +1,5 @@
 export interface TreeNode {
-  id: string
+  id: string | number
   title: string
   children?: TreeNode[]
 }