|
@@ -5,7 +5,8 @@ import App from './App'
|
|
import router from './router'
|
|
import router from './router'
|
|
import 'font-awesome/css/font-awesome.css'
|
|
import 'font-awesome/css/font-awesome.css'
|
|
import './style/main.scss'
|
|
import './style/main.scss'
|
|
-import {getAuthUrls} from '@/js/index'
|
|
|
|
|
|
+import {getAuthUrls, getCurrentUser, getRolesPermissions, storageGet, storageSave} from '@/js/index'
|
|
|
|
+import {notify} from '@/constants/index'
|
|
import {
|
|
import {
|
|
Avatar,
|
|
Avatar,
|
|
Button,
|
|
Button,
|
|
@@ -54,26 +55,60 @@ import {
|
|
|
|
|
|
router.beforeEach((to, from, next) => {
|
|
router.beforeEach((to, from, next) => {
|
|
const urls = getAuthUrls()
|
|
const urls = getAuthUrls()
|
|
- //console.log(to.matched[0].path)
|
|
|
|
- //console.log(urls.includes(to.matched[0].path))
|
|
|
|
- if (to.matched.some(record => record.meta.requireAuth)) { // 判断该路由是否需要登录权限
|
|
|
|
- //console.log('需要登录')
|
|
|
|
- if (sessionStorage.userName === '2' && urls.includes(to.matched[0].path)) { // 判断缓存里面是否有 userName //在登录的时候设置它的值
|
|
|
|
- //console.log('未拦截')
|
|
|
|
- next()
|
|
|
|
|
|
+ if (storageGet('user') == null) {
|
|
|
|
+ getCurrentUser().then((res) => {
|
|
|
|
+ storageSave('user', res)
|
|
|
|
+ storageSave('rolesPermissions',getRolesPermissions(res.roleList))
|
|
|
|
+ if (to.matched.some(record => record.meta.requireAuth)) { // 判断该路由是否需要登录权限
|
|
|
|
+ if (sessionStorage.userName === '2' && urls.includes(to.matched[0].path)) { // 判断缓存里面是否有 userName //在登录的时候设置它的值
|
|
|
|
+ next()
|
|
|
|
+ } else {
|
|
|
|
+ next({
|
|
|
|
+ path: '/',
|
|
|
|
+ query: {
|
|
|
|
+ redirect: to.fullPath
|
|
|
|
+ } // 将跳转的路由path作为参数,登录成功后跳转到该路由
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ next()
|
|
|
|
+ }
|
|
|
|
+ }).catch((error) => {
|
|
|
|
+ notify('warning', '请登录')
|
|
|
|
+ if (to.matched.some(record => record.meta.requireAuth)) { // 判断该路由是否需要登录权限
|
|
|
|
+ if (sessionStorage.userName === '2' && urls.includes(to.matched[0].path)) { // 判断缓存里面是否有 userName //在登录的时候设置它的值
|
|
|
|
+ next()
|
|
|
|
+ } else {
|
|
|
|
+ next({
|
|
|
|
+ path: '/',
|
|
|
|
+ query: {
|
|
|
|
+ redirect: to.fullPath
|
|
|
|
+ } // 将跳转的路由path作为参数,登录成功后跳转到该路由
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ next()
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ } else {
|
|
|
|
+ if (to.matched.some(record => record.meta.requireAuth)) { // 判断该路由是否需要登录权限
|
|
|
|
+ if (sessionStorage.userName === '2' && urls.includes(to.matched[0].path)) { // 判断缓存里面是否有 userName //在登录的时候设置它的值
|
|
|
|
+ next()
|
|
|
|
+ } else {
|
|
|
|
+ next({
|
|
|
|
+ path: '/',
|
|
|
|
+ query: {
|
|
|
|
+ redirect: to.fullPath
|
|
|
|
+ } // 将跳转的路由path作为参数,登录成功后跳转到该路由
|
|
|
|
+ })
|
|
|
|
+ }
|
|
} else {
|
|
} else {
|
|
- //console.log('拦截')
|
|
|
|
- next({
|
|
|
|
- path: '/',
|
|
|
|
- query: {
|
|
|
|
- redirect: to.fullPath
|
|
|
|
- } // 将跳转的路由path作为参数,登录成功后跳转到该路由
|
|
|
|
- })
|
|
|
|
|
|
+ next()
|
|
}
|
|
}
|
|
- } else {
|
|
|
|
- //console.log('不需要登录')
|
|
|
|
- next()
|
|
|
|
}
|
|
}
|
|
|
|
+ //console.log(to.matched[0].path)
|
|
|
|
+ //console.log(urls.includes(to.matched[0].path))
|
|
|
|
+
|
|
})
|
|
})
|
|
|
|
|
|
// require('./mock.js')
|
|
// require('./mock.js')
|