ly-front/src/router/index.js

49 lines
1.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Vue from "vue";
import VueRouter from "vue-router";
import index from "../views/index.vue";
Vue.use(VueRouter);
const routes = [
{
path: "/",
name: "home",
component: index,
beforeEnter: (to, from, next) => {
const expires = localStorage.getItem("expires");
const expiresTimestamp = new Date(expires).getTime();
const now = new Date().getTime();
if (expiresTimestamp && now > expiresTimestamp) {
// 登录已过期,执行自动退出操作
localStorage.removeItem("setToken");
localStorage.removeItem("expires");
localStorage.removeItem("userId");
localStorage.removeItem("isAdmin");
localStorage.removeItem("PositionIds");
next("/login");
}
const token = localStorage.getItem("setToken");
// 在导航到/dashboard路由之前执行的操作
if (!token) {
next("/login"); // 如果非管理员用户访问/dashboard则重定向到登录页面
} else {
next(); // 继续导航到/dashboard
}
}
},
{
path: "/login",
name: "login",
component: () => import("@/views/login/index.vue")
}
];
const router = new VueRouter({
mode: "hash",
base: "/",
routes
});
router.beforeEach((to, from, next) => {
next();
});
export default router;