permission.js 5.4 KB

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