更改平台端

This commit is contained in:
李志强 2026-04-09 15:04:31 +08:00
parent 1bfa019634
commit 01afc1f5be
4 changed files with 46 additions and 56 deletions

View File

@ -130,7 +130,7 @@ function addDynamicRoutes(menus) {
path: "/",
name: "Main",
component: () => import("@/views/Main.vue"),
redirect: "/dashboard",
redirect: "/home",
meta: { requiresAuth: true },
children: [...staticMainChildren, ...dynamicRoutes] // 合并静态和动态子路由
});
@ -190,8 +190,17 @@ router.beforeEach(async (to, from, next) => {
}
if (!dynamicRoutesAdded) {
try {
await loadAndAddDynamicRoutes();
// 路由加载后重新导航,确保路由匹配正确
} catch (error) {
console.error('动态路由加载失败:', error);
// token 无效时跳转登录
if (error?.message === 'token无效' || error?.response?.status === 401) {
next({ path: '/login' });
return;
}
}
// 路由加载后重新导航,确保路由匹配正确
next({ path: to.path, replace: true });
return;
}

View File

@ -2,6 +2,7 @@
* 通用的别名路径解析工具
* 用于在动态导入时解析 @ 别名路径
*/
import { h } from 'vue';
// 使用 import.meta.glob 预加载所有组件
const viewsModules = import.meta.glob('../views/**/*.vue');

View File

@ -119,31 +119,11 @@ const handleAsideMenuClick = async (menuItem) => {
return;
}
//
let routeExists = router.resolve(targetPath).matched.length > 0;
if (!routeExists) {
// 500ms
for (let i = 0; i < 5; i++) {
await new Promise(resolve => setTimeout(resolve, 100));
routeExists = router.resolve(targetPath).matched.length > 0;
if (routeExists) break;
}
}
//
if (routeExists) {
router.push(targetPath).catch(err => {
if (err.name !== 'NavigationDuplicated') {
console.error('路由跳转失败:', err);
}
});
} else {
//
console.warn('路由不存在,尝试刷新页面:', targetPath);
//
ElMessage.warning(`路由 ${targetPath} 不存在,请刷新缓存后重试`);
}
};
const tabsCloseTab = (targetKey) => {
tabsStore.removeTab(targetKey);
@ -166,19 +146,10 @@ const closeAll = () => {
watch(
() => tabsStore.activeTab,
(newVal, oldVal) => {
// oldVal undefined
// watchtabhandleAsideMenuClick
if (newVal && oldVal !== undefined && router.currentRoute.value.fullPath !== newVal) {
// tab
if (newVal === defaultDashboardPath) {
tabsStore.closeAll();
}
//
const routeExists = router.resolve(newVal).matched.length > 0;
if (routeExists) {
//
nextTick(() => {
router.push(newVal).catch(err => {
if (err.name !== 'NavigationDuplicated') {
@ -186,19 +157,6 @@ watch(
}
});
});
} else {
//
setTimeout(() => {
const retryRouteExists = router.resolve(newVal).matched.length > 0;
if (retryRouteExists && router.currentRoute.value.fullPath !== newVal) {
router.push(newVal).catch(err => {
if (err.name !== 'NavigationDuplicated') {
console.error('路由跳转失败:', err);
}
});
}
}, 100);
}
}
},
{ flush: 'post' }

View File

@ -58,6 +58,8 @@
</label>
<div class="action-links">
<a class="forget-link" @click.prevent="goForget">忘记密码?</a>
<span>&nbsp;&nbsp;|&nbsp;&nbsp;</span>
<a class="forget-link" @click.prevent="clearCache">清除缓存</a>
</div>
</div>
<transition name="fade">
@ -66,6 +68,9 @@
<button class="login-btn" @click="handleLogin" :disabled="loading">
{{ loading ? "登录中..." : "登 录" }}
</button>
<div>
</div>
</div>
</div>
<!-- 背景光效 -->
@ -74,7 +79,7 @@
</div>
</template>
<script setup>
<script setup lang="ts">
import { ref, onMounted, nextTick } from "vue";
import { useRouter } from "vue-router";
import { useAuthStore } from "@/stores/auth";
@ -420,6 +425,23 @@ onMounted(() => {
const goRegister = () => router.push("/register");
const goForget = () => router.push("/forget");
const clearCache = async () => {
try {
await ElMessageBox.confirm("确定要清除本地缓存吗?", "提示", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning",
});
localStorage.clear();
sessionStorage.clear();
ElMessage.success("缓存已清除");
//
window.location.reload();
} catch {
//
}
};
</script>
<style scoped>