Explorar o código

跳转携带token修改路由验证

ChengYuXuan %!s(int64=5) %!d(string=hai) anos
pai
achega
de768d8276
Modificáronse 2 ficheiros con 24 adicións e 20 borrados
  1. 11 4
      src/router/index.ts
  2. 13 16
      src/views/Home.vue

+ 11 - 4
src/router/index.ts

@@ -5,6 +5,7 @@ import exam from '@/router/exam'
 import teacher from '@/router/teacher'
 import store from '@/store/index'
 import { jumpToLogin } from '@/api/axios.config'
+import { getUserInfo } from '@/api/user'
 Vue.use(VueRouter)
 
 const routes: Array<RouteConfig> = [
@@ -28,11 +29,17 @@ router.beforeEach((to, from, next) => {
   const { id_token } = to.query
   if (id_token) {
     store.commit('setToken', id_token)
+    getUserInfo().then(({ data }) => {
+      store.commit('setUser', data.result)
+      next()
+    })
+  } else {
+    if (!store.getters.isLogin) {
+      jumpToLogin()
+    } else {
+      next()
+    }
   }
-  if (!store.getters.isLogin) {
-    jumpToLogin()
-  }
-  next()
 })
 
 export default router

+ 13 - 16
src/views/Home.vue

@@ -13,7 +13,7 @@ export default Vue.extend({
     return {}
   },
   computed: {
-    ...mapGetters(['isStudent', 'isTeacher'])
+    ...mapGetters(['isStudent', 'isTeacher', 'isLogin'])
   },
   methods: {
     ...mapMutations(['setToken', 'setUser'])
@@ -27,23 +27,20 @@ export default Vue.extend({
   //   next()
   // },
   mounted() {
-    getUserInfo()
-      .then(({ data }) => {
-        this.setUser(data.result)
-        if (this.isStudent) {
-          this.$router.push({
-            name: 'student'
-          })
-        }
-        if (this.isTeacher) {
-          this.$router.push({
-            name: 'teacher'
-          })
-        }
+    if (!this.isLogin) {
+      jumpToLogin()
+    }
+
+    if (this.isStudent) {
+      this.$router.push({
+        name: 'student'
       })
-      .catch(() => {
-        jumpToLogin()
+    }
+    if (this.isTeacher) {
+      this.$router.push({
+        name: 'teacher'
       })
+    }
   }
 })
 </script>