permission.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import axios from 'axios'
  2. import router, { resetRouter } from './router'
  3. import store from './store'
  4. import storage from 'store'
  5. import NProgress from 'nprogress' // progress bar
  6. import '@/components/NProgress/nprogress.less' // progress bar custom style
  7. import notification from 'ant-design-vue/es/notification'
  8. import { setDocumentTitle, domTitle } from '@/utils/domUtil'
  9. import { userInfo } from '@/api/headerInfo/index.js'
  10. import { fileBimView } from '@/api/document/index.js'
  11. import { ACCESS_TOKEN } from '@/store/mutation-types'
  12. import { i18nRender } from '@/locales'
  13. NProgress.configure({ showSpinner: false }) // NProgress Configuration
  14. const allowList = ['login', 'register', 'registerResult', 'download', 'forget', 'oldRegister'] // no redirect allowList
  15. const loginRoutePath = '/user/login'
  16. const defaultRoutePath = '/'
  17. router.beforeEach(async (to, from, next) => {
  18. console.log('to',to);
  19. console.log('from', from);
  20. NProgress.start() // start progress bar
  21. to.meta && typeof to.meta.title !== 'undefined' && setDocumentTitle(`${i18nRender(to.meta.title)} - ${domTitle}`)
  22. /* has token */
  23. const token = storage.get(ACCESS_TOKEN)
  24. if (token) {
  25. if (to.path === loginRoutePath) {
  26. next({ path: defaultRoutePath })
  27. NProgress.done()
  28. } else {
  29. // check login user.roles is null
  30. if (store.getters.roles.length === 0) {
  31. // request login userInfo
  32. // store
  33. // .dispatch('GetInfo')
  34. // .then(res => {
  35. // // 根据用户权限信息生成可访问的路由表
  36. // store.dispatch('GenerateRoutes', { token, ...res }).then(() => {
  37. // // 动态添加可访问路由表
  38. // // VueRouter@3.5.0+ New API
  39. // resetRouter() // 重置路由 防止退出重新登录或者 token 过期后页面未刷新,导致的路由重复添加
  40. // store.getters.addRouters.forEach(r => {
  41. // router.addRoute(r)
  42. // })
  43. // // 请求带有 redirect 重定向时,登录自动重定向到该地址
  44. // const redirect = decodeURIComponent(from.query.redirect || to.path)
  45. // if (to.path === redirect) {
  46. // // set the replace: true so the navigation will not leave a history record
  47. // next({ ...to, replace: true })
  48. // } else {
  49. // // 跳转到目的路由
  50. // next({ path: redirect })
  51. // }
  52. // })
  53. // })
  54. // .catch(() => {
  55. // notification.error({
  56. // message: '错误',
  57. // description: '请求用户信息失败,请重试'
  58. // })
  59. // // 失败时,获取用户信息失败时,调用登出,来清空历史保留信息
  60. // store.dispatch('Logout').then(() => {
  61. // next({ path: loginRoutePath, query: { redirect: to.fullPath } })
  62. // })
  63. // })
  64. store
  65. .dispatch('getPermissions')
  66. .then((res) => {
  67. // 根据用户权限信息生成可访问的路由表
  68. store.dispatch('GenerateRoutes', { token, ...res }).then(() => {
  69. // 动态添加可访问路由表
  70. // VueRouter@3.5.0+ New API
  71. resetRouter() // 重置路由 防止退出重新登录或者 token 过期后页面未刷新,导致的路由重复添加
  72. store.getters.addRouters.forEach((r) => {
  73. router.addRoute(r)
  74. })
  75. // 请求带有 redirect 重定向时,登录自动重定向到该地址
  76. const redirect = decodeURIComponent(from.query.redirect || to.path)
  77. if (to.path === redirect) {
  78. // set the replace: true so the navigation will not leave a history record
  79. next({ ...to, replace: true })
  80. } else {
  81. // 跳转到目的路由
  82. next({ path: redirect })
  83. }
  84. })
  85. })
  86. .catch(() => {
  87. notification.error({
  88. message: '错误',
  89. description: '请求用户信息失败,请重试',
  90. })
  91. // 失败时,获取用户信息失败时,调用登出,来清空历史保留信息
  92. store.dispatch('Logout').then(() => {
  93. next({ path: loginRoutePath, query: { redirect: to.fullPath } })
  94. })
  95. })
  96. } else {
  97. if (to.path === '/4S') {
  98. const {
  99. data: { name, fs_password },
  100. } = await userInfo()
  101. axios
  102. .post(`http://newlinker.net:18001/login/pc?username=${name}&password=${fs_password}`)
  103. .then(({ data }) => {
  104. if (data.code === 200) {
  105. window.open('https://www.newlinker.net:8090?token=' + data.data.token)
  106. } else {
  107. return
  108. }
  109. })
  110. }
  111. if(to.path === '/IOT') {
  112. let {
  113. data: { bim_view },
  114. } = await fileBimView('1207')
  115. localStorage.setItem(bim_view.lightweightName, JSON.stringify([bim_view]))
  116. window.open(`/modelViewIOT?id=${bim_view.lightweightName}`, '_blank')
  117. }
  118. next()
  119. }
  120. }
  121. } else {
  122. if (allowList.includes(to.name)) {
  123. // 在免登录名单,直接进入
  124. next()
  125. } else {
  126. next({ path: loginRoutePath, query: { redirect: to.fullPath } })
  127. NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
  128. }
  129. }
  130. })
  131. router.afterEach(() => {
  132. NProgress.done() // finish progress bar
  133. })