From bf4a8543ca9f4e9ef6d13f78385f5c0f30255dc0 Mon Sep 17 00:00:00 2001
From: LiuHao <liuhaoai545@gmail.com>
Date: 星期六, 06 五月 2023 09:25:05 +0800
Subject: [PATCH] Merge branch 'ts' into dev

---
 src/components/TopNav/index.vue |   75 ++++++++++++++++++-------------------
 1 files changed, 36 insertions(+), 39 deletions(-)

diff --git a/src/components/TopNav/index.vue b/src/components/TopNav/index.vue
index ac1d2e5..2092415 100644
--- a/src/components/TopNav/index.vue
+++ b/src/components/TopNav/index.vue
@@ -1,14 +1,8 @@
 <template>
-  <el-menu
-    :default-active="activeMenu"
-    mode="horizontal"
-    @select="handleSelect"
-    :ellipsis="false"
-  >
+  <el-menu :default-active="activeMenu" mode="horizontal" @select="handleSelect" :ellipsis="false">
     <template v-for="(item, index) in topMenus">
       <el-menu-item :style="{'--theme': theme}" :index="item.path" :key="index" v-if="index < visibleNumber"
-        ><svg-icon :icon-class="item.meta.icon" />
-        {{ item.meta.title }}</el-menu-item
+        ><svg-icon :icon-class="item.meta ? item.meta.icon : '' " /> {{ item.meta?.title }}</el-menu-item
       >
     </template>
 
@@ -16,29 +10,26 @@
     <el-sub-menu :style="{'--theme': theme}" index="more" v-if="topMenus.length > visibleNumber">
       <template #title>鏇村鑿滃崟</template>
       <template v-for="(item, index) in topMenus">
-        <el-menu-item
-          :index="item.path"
-          :key="index"
-          v-if="index >= visibleNumber"
-          ><svg-icon :icon-class="item.meta.icon" />
-          {{ item.meta.title }}</el-menu-item
+        <el-menu-item :index="item.path" :key="index" v-if="index >= visibleNumber"
+          ><svg-icon :icon-class="item.meta ? item.meta.icon : '' " /> {{ item.meta?.title }}</el-menu-item
         >
       </template>
     </el-sub-menu>
   </el-menu>
 </template>
 
-<script setup>
-import { constantRoutes } from "@/router"
-import { isHttp } from '@/utils/validate'
-import useAppStore from '@/store/modules/app'
-import useSettingsStore from '@/store/modules/settings'
-import usePermissionStore from '@/store/modules/permission'
+<script setup lang="ts">
+import { constantRoutes } from '@/router';
+import { isHttp } from '@/utils/validate';
+import useAppStore from '@/store/modules/app';
+import useSettingsStore from '@/store/modules/settings';
+import usePermissionStore from '@/store/modules/permission';
+import { RouteOption } from 'vue-router';
 
 // 椤堕儴鏍忓垵濮嬫暟
-const visibleNumber = ref(null);
+const visibleNumber = ref<number>(-1);
 // 褰撳墠婵�娲昏彍鍗曠殑 index
-const currentIndex = ref(null);
+const currentIndex = ref<string>();
 // 闅愯棌渚ц竟鏍忚矾鐢�
 const hideList = ['/index', '/user/profile'];
 
@@ -55,12 +46,12 @@
 
 // 椤堕儴鏄剧ず鑿滃崟
 const topMenus = computed(() => {
-  let topMenus = [];
+  let topMenus:RouteOption[] = [];
   routers.value.map((menu) => {
     if (menu.hidden !== true) {
       // 鍏煎椤堕儴鏍忎竴绾ц彍鍗曞唴閮ㄨ烦杞�
       if (menu.path === "/") {
-          topMenus.push(menu.children[0]);
+          topMenus.push(menu.children? menu.children[0] : menu);
       } else {
           topMenus.push(menu);
       }
@@ -71,21 +62,21 @@
 
 // 璁剧疆瀛愯矾鐢�
 const childrenMenus = computed(() => {
-  let childrenMenus = [];
+  let childrenMenus:RouteOption[] = [];
   routers.value.map((router) => {
-    for (let item in router.children) {
-      if (router.children[item].parentPath === undefined) {
+    router.children?.forEach((item) => {
+      if (item.parentPath === undefined) {
         if(router.path === "/") {
-          router.children[item].path = "/" + router.children[item].path;
+          item.path = "/" + item.path;
         } else {
-          if(!isHttp(router.children[item].path)) {
-            router.children[item].path = router.path + "/" + router.children[item].path;
+          if(!isHttp(item.path)) {
+            item.path = router.path + "/" + item.path;
           }
         }
-        router.children[item].parentPath = router.path;
+        item.parentPath = router.path;
       }
-      childrenMenus.push(router.children[item]);
-    }
+      childrenMenus.push(item);
+    })
   })
   return constantRoutes.concat(childrenMenus);
 })
@@ -108,12 +99,12 @@
   return activePath;
 })
 
-function setVisibleNumber() {
+const setVisibleNumber = () => {
   const width = document.body.getBoundingClientRect().width / 3;
-  visibleNumber.value = parseInt(width / 85);
+  visibleNumber.value = parseInt(String(width / 85));
 }
 
-function handleSelect(key, keyPath) {
+const handleSelect = (key: string) => {
   currentIndex.value = key;
   const route = routers.value.find(item => item.path === key);
   if (isHttp(key)) {
@@ -121,7 +112,13 @@
     window.open(key, "_blank");
   } else if (!route || !route.children) {
     // 娌℃湁瀛愯矾鐢辫矾寰勫唴閮ㄦ墦寮�
-    router.push({ path: key });
+    const routeMenu = childrenMenus.value.find(item => item.path === key);
+    if (routeMenu && routeMenu.query) {
+      let query = JSON.parse(routeMenu.query);
+      router.push({ path: key, query: query });
+    } else {
+      router.push({ path: key });
+    }
     appStore.toggleSideBarHide(true);
   } else {
     // 鏄剧ず宸︿晶鑱斿姩鑿滃崟
@@ -130,8 +127,8 @@
   }
 }
 
-function activeRoutes(key) {
-  let routes = [];
+const activeRoutes = (key: string) => {
+  let routes:RouteOption[] = [];
   if (childrenMenus.value && childrenMenus.value.length > 0) {
     childrenMenus.value.map((item) => {
       if (key == item.parentPath || (key == "index" && "" == item.path)) {

--
Gitblit v1.9.3