|
@@ -1,5 +1,6 @@
|
|
|
import Vue from 'vue'
|
|
|
import VueRouter from 'vue-router'
|
|
|
+import store from '@/store/index';
|
|
|
|
|
|
// 路由3+版本的异常处理
|
|
|
const originalPush = VueRouter.prototype.push
|
|
@@ -49,9 +50,9 @@ const routes = [
|
|
|
component: error404,
|
|
|
},
|
|
|
{
|
|
|
- path: '/login', // 页面不存在的情况下会跳到404页面
|
|
|
+ path: '/login',
|
|
|
meta: {
|
|
|
- requireAuth: true, // 添加该字段,表示进入这个路由是需要登录的 /进路由勾子函数beforeEach
|
|
|
+ requireAuth: true,
|
|
|
},
|
|
|
name: 'login',
|
|
|
component: login,
|
|
@@ -73,16 +74,16 @@ const router = new VueRouter({
|
|
|
* **/
|
|
|
router.beforeEach((to, from, next) => {
|
|
|
// 路由地址不存在的处理办法
|
|
|
-
|
|
|
- if (to.matched.some(m => m.meta.requireAuth)) { // 需要登录
|
|
|
- // if(window.localStorage.token && window.localStorage.isLogin === '1'){
|
|
|
- // next()
|
|
|
- // } else
|
|
|
- if (to.path !== '/login') {
|
|
|
- let token = window.localStorage.token;
|
|
|
- if (token === 'null' || token === '' || token === undefined){
|
|
|
- next()
|
|
|
- }
|
|
|
+ if(to.path === '/login' && store.getters.authToken){
|
|
|
+ next({path: '/'})
|
|
|
+ }else if (to.matched.some(m => m.meta.requireAuth)) { // 需要登录
|
|
|
+ if(window.localStorage.authToken){
|
|
|
+ next()
|
|
|
+ } else if (to.path !== '/login') {
|
|
|
+ let token = localStorage.getItem('authToken');
|
|
|
+ if (token === null || token === '' || token === undefined){
|
|
|
+ next({path: '/login'})
|
|
|
+ }else{next()}
|
|
|
} else {
|
|
|
next()
|
|
|
}
|