Browse Source

进入页面前校验用户token

yujinwen 2 tháng trước cách đây
mục cha
commit
4975968997
2 tập tin đã thay đổi với 23 bổ sung0 xóa
  1. 4 0
      src/api/system/user.js
  2. 19 0
      src/router/index.js

+ 4 - 0
src/api/system/user.js

@@ -41,6 +41,10 @@ export default {
   // 系统用户修改密码
   modifyPwd:params=>{
     return post('/sys_user/reset_my_pass',params)
+  },
+  // 用户token检测
+  checkUserToken:()=>{
+    return post('/user/optional',{})
   }
 
   

+ 19 - 0
src/router/index.js

@@ -1,4 +1,5 @@
 import { createRouter, createWebHistory } from "vue-router";
+import {apiSystemUser} from '@/api/system'
 
 //all routes
 const appAllRoutes = [];
@@ -45,5 +46,23 @@ const router = createRouter({
   routes,
 });
 
+const checkTokenPageWhiteArr=['/404','/login','/temppage']
+router.beforeEach(async (to, from, next) => {
+  // to and from are Route Object,next() must be called to resolve the hook
+  // 进入每个页面前校验用户token
+  try {
+    if(checkTokenPageWhiteArr.includes(to.path)) return next()
+    const res=await apiSystemUser.checkUserToken()
+    if(res.Ret!==200) {
+      next('/login')
+      return
+    }
+    next()
+  } catch (error) {
+    next('/login')
+  }
+  
+})
+
 
 export default router;