permission.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import router from './router'
  2. import store from './store'
  3. import { MessageBox, Message } from 'element-ui'
  4. import NProgress from 'nprogress'
  5. import 'nprogress/nprogress.css'
  6. import methods from '@/utils/methodsTool';
  7. import { getToken, setToken } from '@/utils/auth'
  8. import { checkSession, spotCheckFunc } from '@/utils/spotCheck'
  9. import $api from "@/api/api"
  10. NProgress.configure({ showSpinner: false })
  11. let spotCheckData = {}
  12. const whiteList = ['/login', '/auth-redirect', '/bind', '/register']
  13. router.beforeEach(async (to, from, next) => {
  14. if (to.path !== from.path) {
  15. NProgress.start();
  16. }
  17. if (to.path === '/login' && to.query.account) {
  18. await store.dispatch('oldLogin', to.query)
  19. } else {
  20. if (to.path === '/login' && !methods.getQueryVariable('TenantId')) {
  21. sessionStorage.removeItem('TenantId')
  22. }
  23. if (!sessionStorage.TenantId) {
  24. await store.dispatch('findTenantId')
  25. } else {
  26. if (methods.getQueryVariable('TenantId')) {
  27. sessionStorage.TenantId = methods.getQueryVariable('TenantId')
  28. }
  29. }
  30. }
  31. //监管人员抽查处理
  32. if (to.path.includes('/spotCheck') || to.path.includes('/spotCheck2')) {
  33. let array = to.path.split('/').filter(i => !!i)
  34. sessionStorage.TenantId = array[1]
  35. if (to.query.data) {
  36. to.query.data = to.query.data.replace(/\s/g, '+')
  37. store.dispatch('FedLogOut')
  38. await new Promise((resolve) => {
  39. let url = ''
  40. switch (array[0]) {
  41. case "spotCheck":
  42. url = 'commonfreedecryptOfficialInfo'
  43. break;
  44. case "spotCheck2":
  45. url = 'commonfreedecryptSevenOfficialInfo'
  46. break;
  47. default:
  48. break;
  49. }
  50. $api[url]({ data: to.query.data }).then(res => {
  51. let data = {
  52. userId: res.data.userId,
  53. realName: res.data.realName,
  54. id: res.data.gradeId,
  55. className: res.data.className,
  56. goodsId: res.data.goodsId,
  57. goodsName: res.data.goodsName,
  58. orderGoodsId:res.data.orderGoodsId,
  59. fullName: res.data.fullName,
  60. token: res.data.token,
  61. keyId: `${res.data.userId}-${res.data.goodsId}-${res.data.gradeId}-${res.data.orderGoodsId}`,
  62. };
  63. setToken(data.token)
  64. store.commit('SET_TOKEN', data.token)
  65. spotCheckData = data
  66. resolve()
  67. }).catch(() => {
  68. Message.error("数据错误,解析失败")
  69. store.dispatch('FedLogOut')
  70. next(`/login?TenantId=${sessionStorage.TenantId}`)
  71. })
  72. })
  73. }
  74. }
  75. if (sessionStorage.TenantId && !store.state.user.companyName) {
  76. store.dispatch('footerData')
  77. }
  78. if (getToken()) {
  79. to.meta.title && store.dispatch('settings/setTitle', to.meta.title)
  80. /* has token*/
  81. if (to.path === '/login') {
  82. next({ path: '/' })
  83. NProgress.done()
  84. } else {
  85. if (store.getters.roles.length === 0) {
  86. // 判断当前用户是否已拉取完user_info信息
  87. store.dispatch('GetInfo').then(() => {
  88. store.dispatch('GenerateRoutes').then(accessRoutes => {
  89. // 根据roles权限生成可访问的路由表
  90. router.addRoutes(accessRoutes) // 动态添加可访问路由表
  91. if (to.path.includes('/spotCheck')) {
  92. checkSession(spotCheckData)
  93. .then(() => {
  94. //学时审核
  95. next({ path: '/classHoursReviews', replace: true })
  96. })
  97. .catch(() => {
  98. Message.error("存在异常,请联系开发人员")
  99. });
  100. } else {
  101. next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
  102. }
  103. })
  104. }).catch(err => {
  105. store.dispatch('LogOut').then(() => {
  106. Message.error(err)
  107. next({ path: '/' })
  108. })
  109. })
  110. } else {
  111. if (!to.path.includes('/user/profile')) {
  112. checkFunc()
  113. }
  114. next()
  115. }
  116. }
  117. } else {
  118. // 没有token
  119. if (whiteList.indexOf(to.path) !== -1) {
  120. // 在免登录白名单,直接进入
  121. next()
  122. } else {
  123. if (sessionStorage.TenantId == undefined) {
  124. next(`/login`) // 否则全部重定向到登录页
  125. } else {
  126. next(`/login?TenantId=${sessionStorage.TenantId}`) // 否则全部重定向到登录页
  127. }
  128. NProgress.done()
  129. }
  130. }
  131. })
  132. function checkFunc() {
  133. $api.inquireusercheckPwdTime().then(res => {
  134. if (res.data) {
  135. MessageBox.confirm('您已90天为修改密码,请前往修改密码', '系统提示', {
  136. confirmButtonText: '前往修改',
  137. showCancelButton: false,
  138. closeOnClickModal: false,
  139. closeOnPressEscape: false,
  140. showClose: false,
  141. type: 'warning'
  142. }
  143. ).then(() => {
  144. location.href = '/user/profile';
  145. }).catch(() => { });
  146. }
  147. })
  148. }
  149. router.afterEach(() => {
  150. NProgress.done()
  151. })