Browse Source

open router interceptor and fix unauth msgbox

zhangjie 5 năm trước cách đây
mục cha
commit
82839cc414
2 tập tin đã thay đổi với 35 bổ sung26 xóa
  1. 26 24
      src/main.js
  2. 9 2
      src/plugins/axios.js

+ 26 - 24
src/main.js

@@ -22,30 +22,32 @@ Vue.prototype.GLOBAL = GLOBAL;
 Vue.config.productionTip = false;
 
 // route interceptor
-// router.beforeEach((to, from, next) => {
-//   const token = Vue.ls.get("token");
-//   if (to.meta.noRequire) {
-//     // 不需要登录的页面
-//     if (token) {
-//       // 当前状态是已登录
-//       ElementUI.Notice.info({ title: "您已经登录,已自动跳转到主页", duration: 2 });
-//       next({ name: "Home" });
-//     } else {
-//       next();
-//     }
-//   } else {
-//     // 需要登录的路由
-//     if (token) {
-//       next();
-//     } else {
-//       // 登录失效的处理
-//       Vue.ls.clear();
-//       // Vue.ls.remove("token");
-//       // Vue.ls.remove("user");
-//       next({ name: "Login" });
-//     }
-//   }
-// });
+router.beforeEach((to, from, next) => {
+  const token = Vue.ls.get("token");
+  if (to.meta.noRequire) {
+    next();
+    // 不需要登录的页面
+    // if (token) {
+    //   // 当前状态是已登录
+    //   ElementUI.Notice.info({
+    //     title: "您已经登录,已自动跳转到主页",
+    //     duration: 5
+    //   });
+    //   next({ name: "Home" });
+    // } else {
+
+    // }
+  } else {
+    // 需要登录的路由
+    if (token) {
+      next();
+    } else {
+      // 登录失效的处理
+      Vue.ls.clear();
+      next({ name: "Login" });
+    }
+  }
+});
 
 // axios interceptors
 var load = "";

+ 9 - 2
src/plugins/axios.js

@@ -4,6 +4,9 @@ import { MessageBox, Notification } from "element-ui";
 import router from "../router";
 import Vue from "vue";
 
+// 防止鉴权失效之后多次弹窗。
+let unauthMsgBoxIsShow = false;
+
 const mdData = datas => {
   return {
     ...datas,
@@ -44,12 +47,16 @@ const errorDataCallback = error => {
   message = message.indexOf("###") !== -1 ? "参数错误" : message;
 
   if (error.code === "106") {
+    if (unauthMsgBoxIsShow) return error;
+    unauthMsgBoxIsShow = true;
     message = "身份验证失效,请重新登录";
     MessageBox.confirm(message, "重新登陆?", {
-      cancelButtonClass: "el-button--primary",
-      confirmButtonClass: "el-button--default-act",
       type: "warning",
+      closeOnClickModal: false,
+      closeOnPressEscape: false,
+      showClose: false,
       callback: action => {
+        unauthMsgBoxIsShow = false;
         if (action !== "confirm") return;
         Vue.ls.clear();
         router.push({ name: "Login" });