You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
suzhouyingjiPC/src/permission.js

111 lines
3.7 KiB

1 year ago
import router from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { getToken } from '@/utils/auth'
import { isRelogin } from '@/utils/request'
1 year ago
import { getUseInfo } from '@/api/login.js'
1 year ago
1 year ago
NProgress.configure({ showSpinner: false })
1 year ago
1 year ago
const whiteList = ['/login', '/register']
1 year ago
router.beforeEach((to, from, next) => {
1 year ago
NProgress.start()
1 year ago
// window.location.href = 'http://localhost:80/?code=4581bffa1e8b4d9eb1f92e3250bea1cf'
// if(getCode()){
// next()
// } else {
// // const params = new URLSearchParams(location.search);
// //最后结果想获取那个参数直接get
// // const code = params.get("code")
// let url = window.location.href
// var splitted = url.split("?");
// var paramPart = splitted[1].split("#")[0];
// var paramArray = paramPart.split("=");
// // var paramName = paramArray[0];
// const code = paramArray[1];
// setCode(code)
// console.log(code,'code')
// getUseInfo({code}).then(user=>{
// console.log(user,'用户信息')
// next()
// })
// }
1 year ago
if (getToken()) {
1 year ago
to.meta.title && store.dispatch('settings/setTitle', to.meta.title)
1 year ago
/* has token*/
1 year ago
if (to.path === '/login') {
next({ path: '/' })
NProgress.done()
1 year ago
} else {
1 year ago
// store.dispatch("Getdistrict");
1 year ago
if (store.getters.roles.length === 0) {
1 year ago
isRelogin.show = true
1 year ago
// 判断当前用户是否已拉取完user_info信息
store.dispatch('GetInfo').then((res) => {
1 year ago
isRelogin.show = false
store.dispatch('GenerateRoutes').then(accessRoutes => {
// 根据roles权限生成可访问的路由表
console.log(res);
1 year ago
router.addRoutes(accessRoutes) // 动态添加可访问路由表
//刷新之后,要去的路由肯定是自己已经保存过的新路由
let refresh = ''
if (store.state.user.new1.indexOf(to.path) != -1) {
refresh = to.path
}
console.log(to.path);
//进入添加的第一个路由
next({ path: refresh || res[0].path })
1 year ago
})
}).catch(err => {
store.dispatch('LogOut').then(() => {
Message.error(err)
next({ path: '/' })
1 year ago
})
})
1 year ago
} else {
if (store.state.user.old1.indexOf(to.path) != -1 && store.state.user.new1.indexOf(to.path) == -1) {
next({ path: '/404' })
} else {
next()
}
1 year ago
}
}
} else {
// 没有token
1 year ago
if(window.location.href.includes('?code')){
// console.log('有code')
const code = window.location.href.split("?")[1].split('=')[1];
getUseInfo({code:code}).then(user=>{
console.log(user,'用户信息')
localStorage.setItem('isGovernmentAffairsNetwork','1')
store.dispatch('LoginNoCaptcha',{username:'admin',password:'admin@123Jichuang'}).then((res) => {
next('/home')
})
})
1 year ago
} else {
1 year ago
// console.log('没code')
if (whiteList.indexOf(to.path) !== -1) {
// 在免登录白名单,直接进入
next()
} else {
let isGovernmentAffairsNetwork = localStorage.getItem('isGovernmentAffairsNetwork')
if(isGovernmentAffairsNetwork !== '1') {
next(`/login`) // 否则全部重定向到登录页
} else {
console.log('路由拦截跳转登录页')
1 year ago
// next()
location.href = 'http://180.108.205.123:8090/sso-server/oauth2/login?aid=17847210&cbu=http%3A%2F%2F180.108.205.123%3A13001%2F'
1 year ago
}
NProgress.done()
}
1 year ago
}
}
1 year ago
})
1 year ago
router.afterEach(() => {
1 year ago
NProgress.done()
})