From 9a86d15344c1671307b5789ba4967e2c3a2a07a7 Mon Sep 17 00:00:00 2001 From: hujing2 Date: Sun, 29 Jan 2023 17:08:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0token=E6=9C=BA=E5=88=B6?= =?UTF-8?q?=EF=BC=8Ctoken=E8=BF=87=E6=9C=9F=E6=97=A0=E6=B3=95=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E8=B5=84=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- UI2.0/src/js/login.js | 1 + UI2.0/src/js/loginlayout.js | 21 +++++++++++++++++++-- UI2.0/src/js/mainlayout.js | 18 ++++++++++++++++++ UI2.0/src/js/userlayout.js | 30 ++++++++++++++++++++++-------- UI2.0/src/js/utils/AxiosConfig.js | 2 +- UI2.0/src/layouts/UserLayout.vue | 7 +++---- UI2.0/src/pages/Personal.vue | 4 ++-- 7 files changed, 66 insertions(+), 17 deletions(-) diff --git a/UI2.0/src/js/login.js b/UI2.0/src/js/login.js index 601660e..a27cbdf 100644 --- a/UI2.0/src/js/login.js +++ b/UI2.0/src/js/login.js @@ -27,6 +27,7 @@ export default defineComponent({ name: res.user_name, userId: res.user_id, }) + localStorage.setItem("token", res.token) this.$router.push({ path: "/user", }); diff --git a/UI2.0/src/js/loginlayout.js b/UI2.0/src/js/loginlayout.js index 5c729bb..d12c95b 100644 --- a/UI2.0/src/js/loginlayout.js +++ b/UI2.0/src/js/loginlayout.js @@ -1,16 +1,33 @@ import { defineComponent, ref } from "vue"; +import axios from "./utils/AxiosConfig"; export default defineComponent({ name: "LoginLayout", components: {}, methods: { - onItemClick() { - }, onMainClick() { this.$router.push({ path: "/", }); }, + verifyToken() { + if(this.$store.state.User.userInfo.userId == 0 || !localStorage.getItem("token")){ + return + } + axios("/v1/UI/user/userVerify", { + userId: this.$store.state.User.userInfo.userId + },"get").then(res => { + res = JSON.parse(res) + if(res.vaild) { + this.$router.push({ + path: "/user" + }) + } + }) + }, + }, + mounted() { + this.verifyToken() }, setup() { }, }); \ No newline at end of file diff --git a/UI2.0/src/js/mainlayout.js b/UI2.0/src/js/mainlayout.js index 1b7860b..b6d7f34 100644 --- a/UI2.0/src/js/mainlayout.js +++ b/UI2.0/src/js/mainlayout.js @@ -2,6 +2,7 @@ import EssentialLink from "components/EssentialLink.vue"; import { defineComponent, ref } from "vue"; import axios from "./utils/AxiosConfig"; + const linksList = [ { title: "Docs", @@ -22,6 +23,7 @@ export default defineComponent({ }, mounted() { this.connectDatabase() + this.verifyToken() }, methods: { onItemClick() { @@ -62,6 +64,22 @@ export default defineComponent({ }).catch(err => { console.log(err) }) + }, + verifyToken() { + if(this.$store.state.User.userInfo.userId == 0 || !localStorage.getItem("token")){ + return + } + axios("/v1/UI/user/userVerify", { + userId: this.$store.state.User.userInfo.userId + },"get").then(res => { + res = JSON.parse(res) + console.log(res) + if(res.vaild) { + this.$router.push({ + path: "/user" + }) + } + }) } }, setup() { diff --git a/UI2.0/src/js/userlayout.js b/UI2.0/src/js/userlayout.js index 6836724..060fa10 100644 --- a/UI2.0/src/js/userlayout.js +++ b/UI2.0/src/js/userlayout.js @@ -1,4 +1,5 @@ import { defineComponent, ref } from "vue"; +import axios from "./utils/AxiosConfig"; export default defineComponent({ name: "MainLayout", @@ -6,10 +7,8 @@ export default defineComponent({ components: {}, methods: { - onItemClick() { - console.log("Clicked."); - }, onMainClick() { + localStorage.clear() this.$router.push({ path: "/", }); @@ -44,11 +43,6 @@ export default defineComponent({ path: "/user", }); }, - onLoginClick() { - this.$router.push({ - path: "/login", - }); - }, onPersonalClick() { this.$router.push({ path: "/personal", @@ -57,9 +51,29 @@ export default defineComponent({ showSearchInput() { document.getElementById("search-input").style.display = "block"; }, + verifyToken() { + if(this.$store.state.User.userInfo.userId == 0 || !localStorage.getItem("token")){ + this.$router.push({ + path: "/" + }) + return + } + axios("/v1/UI/user/userVerify", { + userId: this.$store.state.User.userInfo.userId + },"get").then(res => { + res = JSON.parse(res) + console.log(res) + if(!res.vaild) { + this.$router.push({ + path: "/" + }) + } + }) + }, }, mounted() { this.$store.dispatch("getUserInfoFromBackend") + this.verifyToken() }, setup() { return { diff --git a/UI2.0/src/js/utils/AxiosConfig.js b/UI2.0/src/js/utils/AxiosConfig.js index 55e64c9..b68c2d1 100644 --- a/UI2.0/src/js/utils/AxiosConfig.js +++ b/UI2.0/src/js/utils/AxiosConfig.js @@ -9,7 +9,7 @@ axios.defaults.timeout = 10000; axios.interceptors.request.use( config => { - const token = sessionStorage.getItem("access_token"); + const token = localStorage.getItem("token"); config.headers.authorization = token; return config; }, diff --git a/UI2.0/src/layouts/UserLayout.vue b/UI2.0/src/layouts/UserLayout.vue index 7297988..1b301de 100644 --- a/UI2.0/src/layouts/UserLayout.vue +++ b/UI2.0/src/layouts/UserLayout.vue @@ -59,18 +59,17 @@ dropdown-icon="expand_more" > - + 个人中心 - + - 退出登录 diff --git a/UI2.0/src/pages/Personal.vue b/UI2.0/src/pages/Personal.vue index 7070670..2989ffc 100644 --- a/UI2.0/src/pages/Personal.vue +++ b/UI2.0/src/pages/Personal.vue @@ -197,7 +197,7 @@