From ad034b417984ca162bf7d2088e2b9bd8021d7fa1 Mon Sep 17 00:00:00 2001 From: GGJ <357021191@qq.com> Date: Tue, 26 Dec 2023 09:51:16 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router/index.ts | 30 ++++----- src/stores/adminInfo.ts | 4 +- src/views/user/login.vue | 141 ++++++++++++++++++++++----------------- 3 files changed, 95 insertions(+), 80 deletions(-) diff --git a/src/router/index.ts b/src/router/index.ts index 26e3974..9fe39df 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -18,22 +18,22 @@ router.beforeEach((to, from, next) => { window.existLoading = true } console.log(to) - // if (to.path == '/login' || to.path == '/404') { - // // 登录或者注册才可以往下进行 - // next() - // } else { - // // 获取 token - // const token = localStorage.getItem('toKen') - // // token 不存在 - // if (token === null || token === '') { - // ElMessage.error('您还没有登录,请先登录') - // next('/login') - // } else { - // next() - // } - // } + if (to.path == '/login' || to.path == '/404') { + // 登录或者注册才可以往下进行 + next() + } else { + // 获取 token + const token = localStorage.getItem('token') + // token 不存在 + if (token === null || token === '') { + ElMessage.error('您还没有登录,请先登录') + next('/login') + } else { + next() + } + } - next() + // next() }) // 路由加载后 diff --git a/src/stores/adminInfo.ts b/src/stores/adminInfo.ts index f42edef..136653f 100644 --- a/src/stores/adminInfo.ts +++ b/src/stores/adminInfo.ts @@ -10,7 +10,7 @@ export const useAdminInfo = defineStore('adminInfo', { nickname: '', avatar: '', last_login_time: '', - token: '', + token: localStorage.getItem('token') || '', refresh_token: '', super: false } @@ -26,7 +26,7 @@ export const useAdminInfo = defineStore('adminInfo', { setToken(token: string, type: 'auth' | 'refresh') { const field = type == 'auth' ? 'token' : 'refresh_token' this[field] = token - window.localStorage.setItem('token', token) + window.localStorage.setItem(field, token) }, getToken(type: 'auth' | 'refresh' = 'auth') { return type === 'auth' ? this.token : this.refresh_token diff --git a/src/views/user/login.vue b/src/views/user/login.vue index 5174b52..0e252da 100644 --- a/src/views/user/login.vue +++ b/src/views/user/login.vue @@ -1,62 +1,63 @@ @@ -64,15 +65,20 @@