u
This commit is contained in:
1 parent
68e0785ec6
commit
57c0d98f11
189 files changed
+34329
No files matched your search
@@ -0,0 +1,63 @@
|
||||
import { useUserStore } from "@/store/user";
|
||||
import { useWebSocketStore } from "@/store/webSocketStore";
|
||||
import { Message } from "@arco-design/web-vue";
|
||||
import type { App } from "vue";
|
||||
import { createRouter, createWebHashHistory } from "vue-router";
|
||||
import routes from "./routes";
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHashHistory(),
|
||||
strict: true,
|
||||
routes: routes,
|
||||
scrollBehavior: () => ({ left: 0, top: 0 })
|
||||
});
|
||||
|
||||
// 提取路径检查的逻辑
|
||||
function shouldCheckLogin(path: string, protectedPaths: string[]): boolean {
|
||||
// 将路径模式转化为正则表达式并检查路径是否匹配
|
||||
const regexPatterns = protectedPaths.map((pattern) => {
|
||||
// 将路径模式转换为正则表达式
|
||||
const regexString = pattern.replace(/\*\*/g, ".*").replace(/\//g, "\\/");
|
||||
return new RegExp(`^${regexString}$`);
|
||||
});
|
||||
|
||||
return regexPatterns.some((pattern) => pattern.test(path));
|
||||
}
|
||||
|
||||
const protectedPaths = ["/im/**", "/settings/**"];
|
||||
|
||||
router.beforeEach((to, from, next) => {
|
||||
window.NProgress?.start?.();
|
||||
|
||||
const userStore = useUserStore();
|
||||
const webSocketStore = useWebSocketStore();
|
||||
|
||||
if (shouldCheckLogin(to.path, protectedPaths)) {
|
||||
if (!userStore.isLogined) {
|
||||
Message.warning("尚未登录,请先登录!");
|
||||
setTimeout(() => {
|
||||
next("/login");
|
||||
}, 500);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (to.path === "/login") {
|
||||
webSocketStore.disconnect();
|
||||
}
|
||||
|
||||
if (userStore.isLogined) {
|
||||
const WebSocketUrl = import.meta.env.VITE_WEBSOCKET_BASEURL;
|
||||
|
||||
webSocketStore.connect(`${WebSocketUrl}/${userStore.userInfo.token}`);
|
||||
}
|
||||
next();
|
||||
});
|
||||
|
||||
router.afterEach(() => {
|
||||
window.NProgress?.done?.();
|
||||
});
|
||||
|
||||
export function setupRouter(app: App) {
|
||||
app.use(router);
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
import { RouteRecordRaw } from "vue-router";
|
||||
|
||||
import BaseLayout from "@/layouts/base-layout/index.vue";
|
||||
|
||||
const routes: RouteRecordRaw[] = [
|
||||
{
|
||||
path: "/",
|
||||
component: BaseLayout,
|
||||
redirect: "/home",
|
||||
children: [
|
||||
{
|
||||
path: "/home",
|
||||
component: () => import("@/pages/home/index.vue")
|
||||
},
|
||||
{
|
||||
path: "/company",
|
||||
component: () => import("@/pages/company/index.vue")
|
||||
},
|
||||
{
|
||||
path: "/company-detail",
|
||||
component: () => import("@/pages/company/detail.vue")
|
||||
},
|
||||
{
|
||||
path: "/messages",
|
||||
component: () => import("@/pages/messages/index.vue"),
|
||||
redirect: "/messages/im",
|
||||
children: [
|
||||
{
|
||||
path: "im",
|
||||
component: () => import("@/pages/messages/im/index.vue")
|
||||
},
|
||||
{
|
||||
path: "like",
|
||||
component: () => import("@/pages/messages/like/index.vue")
|
||||
},
|
||||
{
|
||||
path: "comment",
|
||||
component: () => import("@/pages/messages/comment/index.vue")
|
||||
},
|
||||
{
|
||||
path: "system",
|
||||
component: () => import("@/pages/messages/system/index.vue")
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
path: "/settings",
|
||||
component: () => import("@/pages/settings/index.vue"),
|
||||
redirect: "/settings/company/base",
|
||||
children: [
|
||||
{
|
||||
path: "company",
|
||||
children: [
|
||||
{
|
||||
path: "base",
|
||||
component: () => import("@/pages/settings/sub-pages/company/base/index.vue")
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
path: "creator",
|
||||
children: [
|
||||
{
|
||||
path: "home",
|
||||
component: () => import("@/pages/settings/sub-pages/creator/home/index.vue")
|
||||
},
|
||||
{
|
||||
path: "editor",
|
||||
component: () => import("@/pages/settings/sub-pages/creator/editor/index.vue")
|
||||
},
|
||||
{
|
||||
path: "post-manage",
|
||||
component: () => import("@/pages/settings/sub-pages/creator/post-manage/index.vue")
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
path: "forum",
|
||||
redirect: "/forum/home",
|
||||
children: [
|
||||
{
|
||||
path: "home",
|
||||
component: () => import("@/pages/forum/home/index.vue")
|
||||
},
|
||||
{
|
||||
path: "post-detail",
|
||||
component: () => import("@/pages/forum/post-detail/index.vue")
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
path: "/404",
|
||||
component: () => import("@/pages/error/index.vue")
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
path: "/login",
|
||||
component: () => import("@/pages/auth/login.vue")
|
||||
},
|
||||
{
|
||||
path: "/register",
|
||||
component: () => import("@/pages/auth/register.vue")
|
||||
},
|
||||
{
|
||||
path: "/forget",
|
||||
component: () => import("@/pages/auth/forget.vue")
|
||||
},
|
||||
{
|
||||
path: "/:pathMatch(.*)*",
|
||||
redirect: "/404"
|
||||
}
|
||||
];
|
||||
|
||||
export default routes;
|
||||
Reference in new issue
Block a user