huac 3 سال پیش
والد
کامیت
4403d1efca
1فایلهای تغییر یافته به همراه86 افزوده شده و 57 حذف شده
  1. 86 57
      src/config/routerBefore.js

+ 86 - 57
src/config/routerBefore.js

@@ -8,6 +8,14 @@ import {
   setCookie,
   dgTree
 } from '@/utils/util.js';
+
+import axios from '@/config/axios.js';
+import {
+  stDev,
+  stProd,
+  proPath
+} from '@/config/config.js';
+
 // 免登录白名单
 const whiteList = ['/', '/login'];
 
@@ -101,43 +109,73 @@ const routerBefore = function (router, constantRouterMap) {
     } else {
       document.title = to.meta.title + ' ' + document.title.substr(document.title.indexOf('-'));
     }
-    //
-    if (getCookie('accessToken')) { // 判断是否有token
-      if (window.top.document.URL === window.document.URL && !window.top.localStorage.getItem('ownPrivilege')) {
-        store.dispatch('getOwnMenuUrl');
-      }
-      if (to.path === '/login') {
-        next({
-          path: '/'
+    if (to.query.ticket) {
+      setCookie('ticket', to.query.ticket, '', '/');
+      let res = axios.get('pass/auth/ticket', {
+        params: {
+          ticket: to.query.ticket
+        }
+      });
+        res.then(res => {
+            if (res.succeed) {
+              setCookie('accessToken', res.data.accessToken, '', '/');
+              DoneCookie(to, from, next, flag)
+            }
+        }).catch(err => {
+          console.log(err)
+          Message('登陆账户异常, 请联系管理员')
         });
-      } else {
-        if (window.top.document.URL === window.document.URL && store.state.userInfo === null) { // 判断当前用户是否已拉取完路由信息
-          // 前期无后台测试用
-          // next();
-          // /*
-          store.dispatch('getUserInfo').then(res => { // 拉取info
-            if (res.code === '0') {
-              const userInfo = res.data;
-              store.dispatch('generateRoutes', {
-                userInfo
-              }).then(() => { // 生成可访问的路由表
-                // router.addRoutes(store.state.routes) // 动态添加可访问路由表
-                if (flag) {
-                  next()
-                } else {
-                  next({
+    } else {
+      DoneCookie(to, from, next, flag)
+    }
+    //
+    function DoneCookie (to, from, next, flag) {
+      if (getCookie('accessToken')) { // 判断是否有token
+        if (window.top.document.URL === window.document.URL && !window.top.localStorage.getItem('ownPrivilege')) {
+          store.dispatch('getOwnMenuUrl');
+        }
+        if (to.path === '/login') {
+          next({
+            path: '/'
+          });
+        } else {
+          if (window.top.document.URL === window.document.URL && store.state.userInfo === null) { // 判断当前用户是否已拉取完路由信息
+            // 前期无后台测试用
+            // next();
+            // /*
+            store.dispatch('getUserInfo').then(res => { // 拉取info
+              if (res.code === '0') {
+                const userInfo = res.data;
+                store.dispatch('generateRoutes', {
+                  userInfo
+                }).then(() => { // 生成可访问的路由表
+                  // router.addRoutes(store.state.routes) // 动态添加可访问路由表
+                  if (flag) {
+                    next()
+                  } else {
+                    next({
+                      ...to,
+                      replace: true
+                    })
+                    flag = true
+                  }
+                  // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
+                  console.log('这是个什么东西', {
                     ...to,
                     replace: true
                   })
-                  flag = true
-                }
-                // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
-                console.log('这是个什么东西', {
-                  ...to,
-                  replace: true
                 })
-              })
-            } else {
+              } else {
+                Message('登录已过期,请重新登录');
+                // next('/login'); // 否则全部重定向到登录页
+                // window.location.href = '/views/index.html';
+                setCookie('accessToken', '', -1, '/');
+                setCookie('refreshToken', '', -1, '/');
+                setCookie('workDate', '', -1);
+                window.top.location.href = store.getters.ctx;
+              }
+            }).catch(err => {
+              console.log(err);
               Message('登录已过期,请重新登录');
               // next('/login'); // 否则全部重定向到登录页
               // window.location.href = '/views/index.html';
@@ -145,33 +183,24 @@ const routerBefore = function (router, constantRouterMap) {
               setCookie('refreshToken', '', -1, '/');
               setCookie('workDate', '', -1);
               window.top.location.href = store.getters.ctx;
-            }
-          }).catch(err => {
-            console.log(err);
-            Message('登录已过期,请重新登录');
-            // next('/login'); // 否则全部重定向到登录页
-            // window.location.href = '/views/index.html';
-            setCookie('accessToken', '', -1, '/');
-            setCookie('refreshToken', '', -1, '/');
-            setCookie('workDate', '', -1);
-            window.top.location.href = store.getters.ctx;
-          });
-          // */
-        } else {
-          next() // 当有用户权限的时候,说明所有可访问路由已生成 如访问没权限的全面会自动进入404页面
+            });
+            // */
+          } else {
+            next() // 当有用户权限的时候,说明所有可访问路由已生成 如访问没权限的全面会自动进入404页面
+          }
         }
-      }
-    } else {
-      if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入
-        next();
       } else {
-        Message('未登录');
-        // next('/login'); // 否则全部重定向到登录页
-        // window.location.href = '/views/index.html';
-        setCookie('accessToken', '', -1, '/');
-        setCookie('refreshToken', '', -1, '/');
-        setCookie('workDate', '', -1);
-        window.top.location.href = store.getters.ctx;
+        if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入
+          next();
+        } else {
+          Message('未登录');
+          // next('/login'); // 否则全部重定向到登录页
+          // window.location.href = '/views/index.html';
+          setCookie('accessToken', '', -1, '/');
+          setCookie('refreshToken', '', -1, '/');
+          setCookie('workDate', '', -1);
+          window.top.location.href = store.getters.ctx;
+        }
       }
     }
   });