permission.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import router from './router'
  2. import store from './store'
  3. import {Message,MessageBox } from 'element-ui'
  4. import NProgress from 'nprogress'
  5. import 'nprogress/nprogress.css'
  6. import {getToken} from '@/benyun/utils/auth'
  7. import {isRelogin} from '@/benyun/utils/request'
  8. // import {getQueryString} from "@/benyun/utils/benyuntech";
  9. // import {getQueryObject} from '@/benyun/utils'
  10. NProgress.configure({ showSpinner: false })
  11. // const whiteList = ['/login', '/demo', '/auth-redirect', '/bind', '/register']
  12. const infoHandle = (to:any, next:any) => {
  13. isRelogin.show = true
  14. // 判断当前用户是否已拉取完user_info信息
  15. store.dispatch('GetInfo').then(() => {
  16. isRelogin.show = false
  17. store.dispatch('GenerateRoutes').then(accessRoutes => {
  18. // 根据roles权限生成可访问的路由表
  19. for(const item of accessRoutes){
  20. router.addRoute(item)
  21. }
  22. // router.addRoutes(accessRoutes) // 动态添加可访问路由表
  23. next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
  24. })
  25. }).catch(err => {
  26. MessageBox.alert(err, '提示', {
  27. confirmButtonText: '确定',
  28. callback: () => {
  29. store.dispatch('LogOut').then(() => {})
  30. }
  31. });
  32. })
  33. }
  34. router.beforeEach((to:any, from:any, next:any) => {
  35. NProgress.start()
  36. // let params = getQueryObject();
  37. // const ticket = params.ticket;
  38. // const ssoToken = params.ssoToken;
  39. if (getToken()) {
  40. to.meta.title && store.dispatch('settings/setTitle', to.meta.title)
  41. /* has token*/
  42. // if (to.path === '/login') {
  43. // next({ path: '/' })
  44. // NProgress.done()
  45. // } else {
  46. if (store.getters.roles.length === 0) {
  47. infoHandle(to,next)
  48. } else {
  49. next()
  50. }
  51. // }
  52. } else {
  53. const redirect = location.origin
  54. location.href = `${process.env.VUE_APP_LOGIN_URL}?&redirect=${encodeURIComponent(redirect)}`
  55. // if(!ticket && !ssoToken){
  56. // const redirect = location.origin
  57. // location.href = `${process.env.VUE_APP_LOGIN_URL}?&redirect=${encodeURIComponent(redirect)}`
  58. // }else{
  59. // const url = ticket ? '/sso/doLoginByTicket' : '/sso/doLoginByToken'
  60. // let data:any = {}
  61. // if(ticket) {
  62. // data.ticket = ticket
  63. // }
  64. // if(ssoToken) {
  65. // data.token = ssoToken
  66. // }
  67. // store.dispatch('GetToken',{
  68. // url,data
  69. // }).then(() => {
  70. // infoHandle(to,next)
  71. // // location.href = '/index';
  72. // }).catch((err:any) => {
  73. // let msg = '未知错误,请联系管理员!'
  74. // if(err && err.msg) {
  75. // msg = err.msg
  76. // // Message.error(err.msg);
  77. // }
  78. // MessageBox.alert(msg, '提示', {
  79. // confirmButtonText: '确定',
  80. // callback: () => {
  81. // const redirect = location.origin
  82. // location.href = `${process.env.VUE_APP_LOGIN_URL}?&redirect=${encodeURIComponent(redirect)}`
  83. // }
  84. // });
  85. // })
  86. // // next()
  87. // }
  88. // 没有token
  89. // if (whiteList.indexOf(to.path) !== -1) {
  90. // // 在免登录白名单,直接进入
  91. // next()
  92. // } else {
  93. // next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  94. // NProgress.done()
  95. // }
  96. }
  97. })
  98. router.afterEach(() => {
  99. NProgress.done()
  100. })