安虹睿
1 year ago
7 changed files with 317 additions and 136 deletions
@ -0,0 +1,146 @@ |
|||
// 加token之前版本
|
|||
import router from './router' |
|||
import store from './store' |
|||
import { Message } from 'element-ui' |
|||
import NProgress from 'nprogress' // progress bar
|
|||
import 'nprogress/nprogress.css' // progress bar style
|
|||
import { getToken } from '@/utils/auth' // get token from cookie
|
|||
import getPageTitle from '@/utils/get-page-title' |
|||
import { asyncRoutes } from '@/router' |
|||
|
|||
NProgress.configure({ |
|||
showSpinner: false |
|||
}) // NProgress Configuration
|
|||
|
|||
router.beforeEach(async (to, from, next) => { |
|||
// start progress bar
|
|||
NProgress.start() |
|||
// set page title
|
|||
document.title = getPageTitle() |
|||
|
|||
const hasToken = getToken() |
|||
console.log(21,'hasToken',hasToken,!hasToken) |
|||
|
|||
// 获取表头转义
|
|||
if(!store.getters.columZHList || store.getters.columZHList.length <= 0){ |
|||
await store.dispatch('definition/getColumZHList',()=>{ |
|||
NProgress.done() |
|||
}) |
|||
} |
|||
|
|||
// 获取菜单数据
|
|||
if(!store.getters.permission_routes || store.getters.permission_routes.length <= 0){ |
|||
const accessRoutes = await store.dispatch('permission/getApiColumnsNames') |
|||
router.addRoutes(accessRoutes) |
|||
console.log(35,router) |
|||
} |
|||
|
|||
// 获取枚举数据
|
|||
if(!store.getters.enumList || store.getters.enumList.length <= 0){ |
|||
await store.dispatch('definition/getDefinitionConfig') |
|||
} |
|||
|
|||
// 是否已尝试过自动登录
|
|||
const alreadyTryLogin = localStorage.getItem('alreadyTryLogin') |
|||
// 是否有用户信息:
|
|||
const userInfo = store.getters.currentUserInfo |
|||
// 判断是否自动登录
|
|||
let isAutoLogin = JSON.parse(localStorage.getItem('isAutoLogin')) |
|||
// 如果路径中有isAutoLogin参数则优先,否则读取配置文件中的isAutoLogin参数
|
|||
let _url_isAutoLogin = to.query.isAutoLogin |
|||
if( _url_isAutoLogin== 'false' || _url_isAutoLogin == 'true'){ |
|||
isAutoLogin = JSON.parse(_url_isAutoLogin) |
|||
} |
|||
if(to.path == '/login'){ |
|||
console.log('-------当前路由为登录页-------') |
|||
next() |
|||
}else{ |
|||
console.log('-------当前路由不是登录页-------') |
|||
if((userInfo && userInfo.id) || alreadyTryLogin == 'true'){ |
|||
if(userInfo && userInfo.id){ |
|||
console.log('-------当前有用户信息-------',userInfo) |
|||
}else{ |
|||
console.log('-------当前没有用户信息,但是已尝试过登录-------') |
|||
} |
|||
next() |
|||
}else{ |
|||
console.log('-------当前没有用户信息-------') |
|||
if(isAutoLogin || to.query.loginName){ |
|||
let _username = to.query.loginName || localStorage.getItem("isLoginName") || localStorage.getItem("userNameOrEmailAddress") |
|||
console.log('-------要求自动登录-------',isAutoLogin,to.query.loginName,_username) |
|||
// let loginForm= {
|
|||
// userNameOrEmailAddress:localStorage.getItem("userNameOrEmailAddress"),
|
|||
// password:localStorage.getItem("client_secret"),
|
|||
// // rememberMe:true,
|
|||
// // grant_type: 'password',
|
|||
// // client_id: 'Auth_App',
|
|||
// }
|
|||
// // store.dispatch('user/login', loginForm).then(() => {
|
|||
// login(loginForm).then(()=>{
|
|||
await store.dispatch('user/getUserInfo',_username).then(()=>{ |
|||
console.log("-------用户信息获取成功-------") |
|||
localStorage.setItem('alreadyTryLogin',true) |
|||
next(to) |
|||
NProgress.done() |
|||
// next({
|
|||
// ...to,
|
|||
// query:{
|
|||
// loginName:to.query.loginName,
|
|||
// isAutoLogin:to.query.isAutoLogin,
|
|||
// isSinglePage:to.query.isSinglePage
|
|||
// },
|
|||
// replace: true
|
|||
// })
|
|||
}).catch(()=>{ |
|||
localStorage.setItem('alreadyTryLogin',true) |
|||
next() |
|||
// next({
|
|||
// path:'/login',
|
|||
// query:{
|
|||
// isAutoLogin:to.query.isAutoLogin,
|
|||
// isSinglePage:to.query.isSinglePage
|
|||
// },
|
|||
// replace: true
|
|||
// })
|
|||
// next(`/login?loginName=${to.query.loginName}&isAutoLogin=${to.query.isAutoLogin}`)
|
|||
// next(`/login`)
|
|||
localStorage.setItem("isLoginName",_username) |
|||
// Message.error('获取用户信息失败,请重试!')
|
|||
}) |
|||
// })
|
|||
// .catch((error) => {
|
|||
// console.log('登录失败')
|
|||
// })
|
|||
}else{ |
|||
console.log('-------不需要自动登录-------',isAutoLogin,to.query.loginName) |
|||
next({ |
|||
path:'/login', |
|||
query:{ |
|||
loginName:to.query.loginName, |
|||
isAutoLogin:to.query.isAutoLogin, |
|||
isSinglePage:to.query.isSinglePage |
|||
}, |
|||
replace: true |
|||
}) |
|||
NProgress.done() |
|||
} |
|||
} |
|||
} |
|||
|
|||
localStorage.setItem('alreadyTryLogin',false) |
|||
}) |
|||
|
|||
router.afterEach(() => { |
|||
// element-ui 使用 Tooltip 提示时候,更换页面,提示依旧存在偶现bug处理。
|
|||
const mytooltipList = document.querySelectorAll('.el-tooltip__popper') // 获取页面所有tooltip的dom集合
|
|||
setTimeout(() => { |
|||
mytooltipList.forEach((value) => { |
|||
value.style.display = 'none' |
|||
if(value.parentElement){ |
|||
value.parentElement.removeChild(value) |
|||
} |
|||
}) |
|||
}, 1000) |
|||
// finish progress bar
|
|||
NProgress.done() |
|||
}) |
Loading…
Reference in new issue