permission.js 5.6 KB

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