permission.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import router from './router'
  2. import store from './store'
  3. import { Message } from 'element-ui'
  4. import NProgress from 'nprogress' // progress bar
  5. import 'nprogress/nprogress.css' // progress bar style
  6. import { getToken,removeToken } from '@/utils/auth' // get token from cookie
  7. import Cookies from 'js-cookie'
  8. import getPageTitle from '@/utils/get-page-title'
  9. // 1为单点登录,其他不是
  10. Cookies.set('sso',1)
  11. NProgress.configure({ showSpinner: false }) // NProgress Configuration
  12. const whiteList = ['/login'] // no redirect whitelist
  13. router.beforeEach(async(to, from, next) => {
  14. // start progress bar
  15. NProgress.start()
  16. // set page title
  17. document.title = getPageTitle(to.meta.title)
  18. // determine whether the user has logged in
  19. const hasToken = getToken()
  20. console.log('hasToken==>',hasToken)
  21. if (hasToken) {
  22. if (to.path === '/login') {
  23. // if is logged in, redirect to the home page
  24. next({ path: '/' })
  25. NProgress.done()
  26. } else {
  27. // determine whether the user has obtained his permission roles through getInfo
  28. const hasRoles = store.getters.roles && store.getters.roles.length > 0
  29. if (hasRoles) {
  30. // console.log('%c 不用获取,直接进入页面', 'color:#4EA35F')
  31. next()
  32. } else {
  33. try {
  34. // get user info
  35. // note: roles must be a object array! such as: ['admin'] or ,['developer','editor']
  36. // console.log('%c 开始请求用户信息', 'color:#4EA35F')
  37. const { roles } = await store.dispatch('user/getInfo')
  38. // generate accessible routes map based on roles
  39. const accessRoutes = await store.dispatch('permission/generateRoutes', roles)
  40. console.log(accessRoutes,'accessRoutes')
  41. // dynamically add accessible routes
  42. router.addRoutes(accessRoutes)
  43. // hack method to ensure that addRoutes is complete
  44. // set the replace: true, so the navigation will not leave a history record
  45. // console.log('%c 马上载入页面', 'color:skyblue')
  46. next({ ...to, replace: true })
  47. } catch (error) {
  48. // remove token and go to login page to re-login
  49. await store.dispatch('user/resetToken')
  50. Message.error(error || 'Has Error')
  51. // next(`/login?redirect=${to.path}`) //跳转到退出前界面
  52. removeToken()
  53. next(`/login`)
  54. NProgress.done()
  55. }
  56. }
  57. }
  58. } else {
  59. /* has no token*/
  60. if (whiteList.indexOf(to.path) !== -1) {
  61. // in the free login whitelist, go directly
  62. next()
  63. } else {
  64. // other pages that do not have permission to access are redirected to the login page.
  65. // next(`/login?redirect=${to.path}`) //跳转到退出前界面
  66. next({ path: '/login' })
  67. NProgress.done()
  68. if(Cookies.get('sso') == 1){
  69. // 单点登录
  70. // 构建要跳转的URL
  71. var url = process.env.VUE_APP_BASE_API
  72. console.log(url,'url')
  73. //获取当前url
  74. if(url.indexOf('/')==0 && url.length==1){
  75. url= window.location.protocol + "//"+window.location.host+url
  76. }
  77. const externalURL = "https://id.xiandaimuye.com/api/v1/oauth2/authorize?response_type=code&client_id=fTBm64I4k3kqHYtoFTUpvirCDxxCfx7I&redirect_uri="+url+'api/v1/oauth2/token';
  78. // 使用 $router.push 进行页面跳转
  79. // 注意: 这里的跳转是在当前窗口进行的,如果需要在新标签页打开,可以使用 window.open(externalURL)
  80. window.open(externalURL, "_self");
  81. }
  82. }
  83. }
  84. })
  85. router.afterEach(() => {
  86. // finish progress bar
  87. NProgress.done()
  88. })