101 lines
4.2 KiB
TypeScript
Raw Normal View History

2025-01-09 20:34:07 +08:00
import NProgress from "@/config/nprogress";
import pinia from "@/store/index";
import { createRouter, createWebHashHistory } from "vue-router";
import { staticRoutes, notFoundAndNoPower } from "@/router/route.ts";
import { currentlyRoute } from "@/router/route-output";
import { storeToRefs } from "pinia";
import { useUserInfoStore } from "@/store/modules/user-info";
import { useRoutesConfigStore } from "@/store/modules/route-config";
import { useRoutingMethod } from "@/hooks/useRoutingMethod";
2025-04-10 16:57:26 +08:00
import { isEmptyObject } from "@/utils/index";
2025-01-09 20:34:07 +08:00
/**
* vue的路由示例
* @method createRouter(options: RouterOptions): Router
* @link https://next.router.vuejs.org/zh/api/#createrouter
*/
const router = createRouter({
history: createWebHashHistory(),
/**
* addRoute动态添加
* 1staticRoutes登录页layout页('/')
* 2notFoundAndNoPower 401500 No match found for location with path 'xxx'
* 2 notFoundAndNoPower 401500
* notFoundAndNoPower 401500
2025-01-09 20:34:07 +08:00
* notFoundAndNoPower layout容器展示
*/
routes: [...staticRoutes, ...notFoundAndNoPower]
});
/**
*
* 1token
* 2token
* 3tokenhome页
2025-04-10 23:05:48 +08:00
* 4token
2025-01-09 20:34:07 +08:00
*
* routeTree不能持久化缓存
* addRoute动态添加的路由失效
*/
router.beforeEach(async (to: any, _: any, next: any) => {
2025-01-09 20:34:07 +08:00
NProgress.start(); // 开启进度条
const store = useUserInfoStore(pinia);
2025-04-10 16:57:26 +08:00
const { token, account } = storeToRefs(store);
// console.log("去", to, "来自", from);
2025-01-09 20:34:07 +08:00
// next()内部加了path等于跳转指定路由会再次触发router.beforeEach内部无参数等于放行不会触发router.beforeEach
if (to.path === "/login" && !token.value) {
// 1、去登录页无token放行
next();
} else if (!token.value) {
// 2、没有token直接重定向到登录页
next("/login");
} else if (to.path === "/login" && token.value) {
// 3、去登录页有token直接重定向到home页
next("/home");
// 项目内的跳转,处理跳转路由高亮
currentlyRoute(to);
2025-01-09 20:34:07 +08:00
} else {
2025-04-10 23:05:48 +08:00
// 4、去非登录页有token用户信息是否存在有则放行否则重新获取路由信息、初始化路由
2025-01-09 20:34:07 +08:00
const routeStore = useRoutesConfigStore(pinia);
2025-04-10 16:57:26 +08:00
// 判断账号信息是否获取,先获取账号信息和路由信息,添加路由后再跳转(页面刷新时触发)
2025-01-09 20:34:07 +08:00
// 解决刷新页面404的问题
2025-04-10 16:57:26 +08:00
if (isEmptyObject(account.value.user)) {
// 获取账号信息
await store.setAccount();
// 获取路由信息
2025-01-09 20:34:07 +08:00
await routeStore.initSetRouter();
// 判断是否是动态路由
const { isDynamicRoute } = useRoutingMethod();
if (isDynamicRoute(to.path)) {
next({ name: to.name, params: to.params });
} else {
next({ path: to.path, query: to.query });
}
2025-01-09 20:34:07 +08:00
} else {
// 获取外链路由的处理函数
// 所有的路由正常放行,只不过额外判断是否是外链,如果是,则打开新窗口跳转外链
// 外链的页面依旧正常打开只不过不会参与缓存与tabs显示符合路由跳转的直觉
const { openExternalLinks } = useRoutingMethod();
2025-01-09 20:34:07 +08:00
// 处理外链跳转
openExternalLinks(to);
// 动态路由添加过走这里,直接放行
next();
// 项目内的跳转,处理跳转路由高亮
currentlyRoute(to);
2025-01-09 20:34:07 +08:00
}
}
});
// 路由跳转错误
router.onError((error: any) => {
NProgress.done();
console.warn("路由错误", error.message);
});
// 路由加载后
router.afterEach(() => {
NProgress.done();
});
export default router;