From 518a52b7c8a59469f16b7b15bc41891f5ee3b154 Mon Sep 17 00:00:00 2001 From: xiesi <305492881@qq.com> Date: Sun, 20 Jun 2021 01:31:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=99=BB=E5=BD=95=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E9=AA=8C=E8=AF=81=E5=92=8C=E9=80=80=E5=87=BA=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.js | 30 +++++++++++++++++++++++++++++- src/router/index.js | 3 +++ src/views/Home.vue | 9 ++++++++- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/main.js b/src/main.js index b843df8..03020bb 100644 --- a/src/main.js +++ b/src/main.js @@ -9,4 +9,32 @@ import 'element-plus/lib/theme-chalk/index.css'; import axios from 'axios' import VueAxios from 'vue-axios' -createApp(App).use(store).use(router).use(ElementPlus).use(VueAxios, axios).mount("#app"); \ No newline at end of file +createApp(App).use(store).use(router).use(ElementPlus).use(VueAxios, axios).mount("#app"); + +router.beforeEach((to, from, next) => { + // to and from are both route objects. must call `next`. + if (to.meta.needLogin) { + console.log("hello"); + axios.get("http://localhost:8070/login", { + withCredentials: true + }) + .then((response) => { + if (response.data.usertype == '用户') { + //显示用户名 + console.log(response.data.data.name); + next(); + } else { + //当前为有课,不允许登录 + next({ + name: "Login", + query: { + redirect: to.meta.redirect + } + }); + } + }); + next(); + } else { + next(); + } +}); \ No newline at end of file diff --git a/src/router/index.js b/src/router/index.js index 136b708..e9a4a2e 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -8,6 +8,9 @@ const routes = [{ path: "/", name: "Home", component: Home, + meta: { + needLogin: true, + } }, { path: "/login", diff --git a/src/views/Home.vue b/src/views/Home.vue index ee18f79..ad1d01f 100644 --- a/src/views/Home.vue +++ b/src/views/Home.vue @@ -39,7 +39,7 @@ hello havelogin - gotologin + logout gotologin gotologin @@ -99,6 +99,13 @@ export default { console.log(response.data); }); }, + logout() { + this.axios + .post("http://localhost:8070/logout", {}, { withCredentials: true }) + .then((response) => { + console.log(response.data); + }); + }, }, data() { return { -- GitLab