From ff8768967110faeceff708db369346d965619346 Mon Sep 17 00:00:00 2001 From: fanxb Date: Sun, 20 Mar 2022 22:19:37 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=89=8D=E7=AB=AF=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=85=A8=E5=B1=80=E8=B7=AF=E7=94=B1=E9=92=A9=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bookmark_front/src/layout/home/Top.vue | 18 ++ .../src/layout/{main => manage}/Bottom.vue | 0 .../src/layout/{main => manage}/Content.vue | 0 .../src/layout/{main => manage}/Top.vue | 0 bookmark_front/src/router/index.js | 48 ++++- .../src/store/modules/globalConfig.js | 180 +++++++++--------- bookmark_front/src/store/type.js | 5 - bookmark_front/src/views/manage/index.vue | 6 +- 8 files changed, 159 insertions(+), 98 deletions(-) create mode 100644 bookmark_front/src/layout/home/Top.vue rename bookmark_front/src/layout/{main => manage}/Bottom.vue (100%) rename bookmark_front/src/layout/{main => manage}/Content.vue (100%) rename bookmark_front/src/layout/{main => manage}/Top.vue (100%) delete mode 100644 bookmark_front/src/store/type.js diff --git a/bookmark_front/src/layout/home/Top.vue b/bookmark_front/src/layout/home/Top.vue new file mode 100644 index 0000000..905cdb3 --- /dev/null +++ b/bookmark_front/src/layout/home/Top.vue @@ -0,0 +1,18 @@ + + + + + diff --git a/bookmark_front/src/layout/main/Bottom.vue b/bookmark_front/src/layout/manage/Bottom.vue similarity index 100% rename from bookmark_front/src/layout/main/Bottom.vue rename to bookmark_front/src/layout/manage/Bottom.vue diff --git a/bookmark_front/src/layout/main/Content.vue b/bookmark_front/src/layout/manage/Content.vue similarity index 100% rename from bookmark_front/src/layout/main/Content.vue rename to bookmark_front/src/layout/manage/Content.vue diff --git a/bookmark_front/src/layout/main/Top.vue b/bookmark_front/src/layout/manage/Top.vue similarity index 100% rename from bookmark_front/src/layout/main/Top.vue rename to bookmark_front/src/layout/manage/Top.vue diff --git a/bookmark_front/src/router/index.js b/bookmark_front/src/router/index.js index c448b23..62d089f 100644 --- a/bookmark_front/src/router/index.js +++ b/bookmark_front/src/router/index.js @@ -1,5 +1,7 @@ import Vue from "vue"; 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); @@ -17,11 +19,11 @@ const routes = [ path: "/public", component: () => import("@/views/public/index"), children: [ - { path: "/login", component: () => import("@/views/public/login/index") }, - { path: "/register", component: () => import("@/views/public/register/index") }, - { path: "/resetPassword", component: () => import("@/views/public/passwordReset/index") }, - { path: "/oauth/github", component: () => import("@/views/public/oauth/github/index") }, - { path: "/404", component: () => import("@/views/public/notFound/index") } + { path: "login", component: () => import("@/views/public/login/index") }, + { path: "register", component: () => import("@/views/public/register/index") }, + { path: "resetPassword", component: () => import("@/views/public/passwordReset/index") }, + { path: "oauth/github", component: () => import("@/views/public/oauth/github/index") }, + { path: "404", component: () => import("@/views/public/notFound/index") }, ] }, { path: "*", redirect: "/public/404" } @@ -32,4 +34,40 @@ const router = new VueRouter({ 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; diff --git a/bookmark_front/src/store/modules/globalConfig.js b/bookmark_front/src/store/modules/globalConfig.js index da0992a..80f7df9 100644 --- a/bookmark_front/src/store/modules/globalConfig.js +++ b/bookmark_front/src/store/modules/globalConfig.js @@ -1,105 +1,115 @@ import localforage from "localforage"; import HttpUtil from "../../util/HttpUtil"; -const USER_INFO = "userInfo"; -const TOKEN = "token"; -const SERVER_CONFIG = "serverConfig"; +export const GLOBAL_CONFIG = "globalConfig"; +export const USER_INFO = "userInfo"; +export const TOKEN = "token"; +export const SERVER_CONFIG = "serverConfig"; +export const SUPPORT_NO_LOGIN = "supportNoLogin"; /** * 存储全局配置 */ const state = { - /** - * 用户信息 - */ - [USER_INFO]: {}, - /** - * token - */ - [TOKEN]: null, - /** - * 是否已经初始化完成,避免多次重复初始化 - */ - isInit: false, - /** - * 是否移动端 - */ - isPhone: false, - /** - * 服务端全局配置 - */ - [SERVER_CONFIG]: {} + /** + * 用户信息 + */ + [USER_INFO]: {}, + /** + * token,null说明未获取登录凭证 + */ + [TOKEN]: null, + /** + * 是否已经初始化完成,避免多次重复初始化 + */ + isInit: false, + /** + * 是否移动端 + */ + isPhone: false, + /** + * 是否支持未登录进入页面 + */ + [SUPPORT_NO_LOGIN]: false, + /** + * 服务端全局配置 + */ + [SERVER_CONFIG]: {} }; const getters = {}; const actions = { - //登陆后的,初始化数据 - async init(context) { - if (context.state.isInit) { - return; - } - const token = await localforage.getItem(TOKEN); - await context.dispatch("setToken", token); + //登陆后的,初始化数据 + async init (context) { + if (context.state.isInit) { + return; + } + const token = await localforage.getItem(TOKEN); + await context.dispatch("setToken", token); - let userInfo = await localforage.getItem(USER_INFO); - if (userInfo) { - context.commit(USER_INFO, userInfo); - } - try { - await context.dispatch("refreshUserInfo"); - } catch (err) { - console.error(err); - } - context.commit("isInit", true); - context.commit("isPhone", /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)); - }, - async refreshUserInfo({ commit }) { - let userInfo = await HttpUtil.get("/user/currentUserInfo"); - await localforage.setItem(USER_INFO, userInfo); - commit(USER_INFO, userInfo); - }, - async setToken({ commit }, token) { - await localforage.setItem(TOKEN, token); - commit(TOKEN, token); - }, - //登出清除数据 - async clear(context) { - await localforage.removeItem("userInfo"); - await localforage.removeItem("token"); - context.commit(USER_INFO, null); - context.commit(TOKEN, null); - context.commit("isInit", false); - }, - /** - * 从服务器读取全局配置 - */ - async refreshServerConfig({ commit }) { - commit(SERVER_CONFIG, await HttpUtil.get("/common/config/global")); - } + let userInfo = await localforage.getItem(USER_INFO); + if (userInfo) { + context.commit(USER_INFO, userInfo); + } + try { + await context.dispatch("refreshUserInfo"); + } catch (err) { + console.error(err); + } + context.commit("isInit", true); + context.commit("isPhone", /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)); + }, + async refreshUserInfo ({ commit }) { + let userInfo = await HttpUtil.get("/user/currentUserInfo"); + await localforage.setItem(USER_INFO, userInfo); + commit(USER_INFO, userInfo); + }, + async setToken ({ commit }, token) { + await localforage.setItem(TOKEN, token); + commit(TOKEN, token); + }, + //登出清除数据 + async clear (context) { + await localforage.removeItem("userInfo"); + await localforage.removeItem("token"); + context.commit(USER_INFO, null); + context.commit(TOKEN, null); + context.commit("isInit", false); + }, + /** + * 从服务器读取全局配置 + */ + async refreshServerConfig ({ commit }) { + commit(SERVER_CONFIG, await HttpUtil.get("/common/config/global")); + } }; const mutations = { - userInfo(state, userInfo) { - state.userInfo = userInfo; - }, - token(state, token) { - state.token = token; - }, - isInit(state, isInit) { - state.isInit = isInit; - }, - isPhone(state, status) { - state.isPhone = status; - }, - [SERVER_CONFIG](state, serverConfig) { - state[SERVER_CONFIG] = serverConfig; - } + userInfo (state, userInfo) { + state.userInfo = userInfo; + }, + token (state, token) { + state.token = token; + }, + isInit (state, isInit) { + state.isInit = isInit; + }, + isPhone (state, status) { + state.isPhone = status; + }, + [SERVER_CONFIG] (state, serverConfig) { + state[SERVER_CONFIG] = serverConfig; + }, + [SUPPORT_NO_LOGIN] (state, val) { + state[SUPPORT_NO_LOGIN] = val; + } }; + export default { - namespaced: true, - state, - getters, - actions, - mutations + namespaced: true, + state, + getters, + actions, + mutations }; diff --git a/bookmark_front/src/store/type.js b/bookmark_front/src/store/type.js deleted file mode 100644 index 6661bd7..0000000 --- a/bookmark_front/src/store/type.js +++ /dev/null @@ -1,5 +0,0 @@ -/** - * 测试用 - */ -export const TEST = "test"; -export const COUNT = "count"; diff --git a/bookmark_front/src/views/manage/index.vue b/bookmark_front/src/views/manage/index.vue index 75ad923..43037b3 100644 --- a/bookmark_front/src/views/manage/index.vue +++ b/bookmark_front/src/views/manage/index.vue @@ -9,9 +9,9 @@