feat:前端增加全局路由钩子
This commit is contained in:
parent
022d24ae54
commit
ff87689671
18
bookmark_front/src/layout/home/Top.vue
Normal file
18
bookmark_front/src/layout/home/Top.vue
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<template>
|
||||||
|
<div></div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
import { mapState } from "vuex";
|
||||||
|
export default {
|
||||||
|
name: "homeTop",
|
||||||
|
data() {
|
||||||
|
return {};
|
||||||
|
},
|
||||||
|
computed: {
|
||||||
|
...mapState("globalConfig", ["userInfo"]),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style lang="less" scoped></style>
|
@ -1,5 +1,7 @@
|
|||||||
import Vue from "vue";
|
import Vue from "vue";
|
||||||
import VueRouter from "vue-router";
|
import VueRouter from "vue-router";
|
||||||
|
import vuex from "../store/index.js";
|
||||||
|
import { GLOBAL_CONFIG, SUPPORT_NO_LOGIN, TOKEN } from "@/store/modules/globalConfig";
|
||||||
|
|
||||||
Vue.use(VueRouter);
|
Vue.use(VueRouter);
|
||||||
|
|
||||||
@ -17,11 +19,11 @@ const routes = [
|
|||||||
path: "/public",
|
path: "/public",
|
||||||
component: () => import("@/views/public/index"),
|
component: () => import("@/views/public/index"),
|
||||||
children: [
|
children: [
|
||||||
{ path: "/login", component: () => import("@/views/public/login/index") },
|
{ path: "login", component: () => import("@/views/public/login/index") },
|
||||||
{ path: "/register", component: () => import("@/views/public/register/index") },
|
{ path: "register", component: () => import("@/views/public/register/index") },
|
||||||
{ path: "/resetPassword", component: () => import("@/views/public/passwordReset/index") },
|
{ path: "resetPassword", component: () => import("@/views/public/passwordReset/index") },
|
||||||
{ path: "/oauth/github", component: () => import("@/views/public/oauth/github/index") },
|
{ path: "oauth/github", component: () => import("@/views/public/oauth/github/index") },
|
||||||
{ path: "/404", component: () => import("@/views/public/notFound/index") }
|
{ path: "404", component: () => import("@/views/public/notFound/index") },
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{ path: "*", redirect: "/public/404" }
|
{ path: "*", redirect: "/public/404" }
|
||||||
@ -32,4 +34,40 @@ const router = new VueRouter({
|
|||||||
routes
|
routes
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在此进行登录信息判断,以及重定向到登录页面
|
||||||
|
*/
|
||||||
|
router.beforeEach((to, from, next) => {
|
||||||
|
let supportNoLogin = to.path === '/' || to.path.startsWith("/public");
|
||||||
|
vuex.commit(GLOBAL_CONFIG + "/" + SUPPORT_NO_LOGIN, supportNoLogin);
|
||||||
|
if (!supportNoLogin && !checkJwtValid()) {
|
||||||
|
//如不支持未登录进入,切jwt已过期,直接跳转到登录页面
|
||||||
|
next({
|
||||||
|
path: "/public/login?to=" + btoa(location.href),
|
||||||
|
replace: true
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查jwt是否有效
|
||||||
|
*/
|
||||||
|
function checkJwtValid () {
|
||||||
|
let token = vuex.state[GLOBAL_CONFIG][TOKEN];
|
||||||
|
try {
|
||||||
|
if (token && token.trim().length > 0) {
|
||||||
|
//检查token是否还有效
|
||||||
|
let content = window.atob(token.split(".")[1]);
|
||||||
|
if (content.exp > Date.now() / 1000) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
import localforage from "localforage";
|
import localforage from "localforage";
|
||||||
import HttpUtil from "../../util/HttpUtil";
|
import HttpUtil from "../../util/HttpUtil";
|
||||||
const USER_INFO = "userInfo";
|
export const GLOBAL_CONFIG = "globalConfig";
|
||||||
const TOKEN = "token";
|
export const USER_INFO = "userInfo";
|
||||||
const SERVER_CONFIG = "serverConfig";
|
export const TOKEN = "token";
|
||||||
|
export const SERVER_CONFIG = "serverConfig";
|
||||||
|
export const SUPPORT_NO_LOGIN = "supportNoLogin";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 存储全局配置
|
* 存储全局配置
|
||||||
@ -13,7 +15,7 @@ const state = {
|
|||||||
*/
|
*/
|
||||||
[USER_INFO]: {},
|
[USER_INFO]: {},
|
||||||
/**
|
/**
|
||||||
* token
|
* token,null说明未获取登录凭证
|
||||||
*/
|
*/
|
||||||
[TOKEN]: null,
|
[TOKEN]: null,
|
||||||
/**
|
/**
|
||||||
@ -24,6 +26,10 @@ const state = {
|
|||||||
* 是否移动端
|
* 是否移动端
|
||||||
*/
|
*/
|
||||||
isPhone: false,
|
isPhone: false,
|
||||||
|
/**
|
||||||
|
* 是否支持未登录进入页面
|
||||||
|
*/
|
||||||
|
[SUPPORT_NO_LOGIN]: false,
|
||||||
/**
|
/**
|
||||||
* 服务端全局配置
|
* 服务端全局配置
|
||||||
*/
|
*/
|
||||||
@ -93,9 +99,13 @@ const mutations = {
|
|||||||
},
|
},
|
||||||
[SERVER_CONFIG] (state, serverConfig) {
|
[SERVER_CONFIG] (state, serverConfig) {
|
||||||
state[SERVER_CONFIG] = serverConfig;
|
state[SERVER_CONFIG] = serverConfig;
|
||||||
|
},
|
||||||
|
[SUPPORT_NO_LOGIN] (state, val) {
|
||||||
|
state[SUPPORT_NO_LOGIN] = val;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
namespaced: true,
|
namespaced: true,
|
||||||
state,
|
state,
|
||||||
|
@ -1,5 +0,0 @@
|
|||||||
/**
|
|
||||||
* 测试用
|
|
||||||
*/
|
|
||||||
export const TEST = "test";
|
|
||||||
export const COUNT = "count";
|
|
@ -9,9 +9,9 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import Content from "@/layout/main/Content.vue";
|
import Content from "@/layout/manage/Content.vue";
|
||||||
import Bottom from "@/layout/main/Bottom.vue";
|
import Bottom from "@/layout/manage/Bottom.vue";
|
||||||
import Top from "@//layout/main/Top.vue";
|
import Top from "@/layout/manage/Top.vue";
|
||||||
|
|
||||||
import httpUtil from "../../util/HttpUtil";
|
import httpUtil from "../../util/HttpUtil";
|
||||||
export default {
|
export default {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user