permission.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import Vue from 'vue'
  2. import router from './router'
  3. import store from './store'
  4. import NProgress from 'nprogress' // progress bar
  5. import 'nprogress/nprogress.css' // progress bar style
  6. import notification from 'ant-design-vue/es/notification'
  7. import { ACCESS_TOKEN,INDEX_MAIN_PAGE_PATH, OAUTH2_LOGIN_PAGE_PATH } from '@/store/mutation-types'
  8. import { generateIndexRouter, isOAuth2AppEnv } from '@/utils/util'
  9. // 修改框架
  10. import cookie from 'js-cookie'
  11. import { getAction, postAction } from '@api/manage'
  12. NProgress.configure({ showSpinner: false }) // NProgress Configuration
  13. const whiteList = ['/user/login', '/user/register', '/user/register-result','/user/alteration'] // no redirect whitelist
  14. whiteList.push(OAUTH2_LOGIN_PAGE_PATH)
  15. whiteList.push('/iot-sso/login')
  16. router.beforeEach((to, from, next) => {
  17. //update-begin---author:scott ---date:2022-10-13 for:[jeecg-boot/issues/4091]多级路由缓存问题 #4091-----------
  18. //解决三级菜单无法缓存问题
  19. //参考: https://blog.csdn.net/qq_37322135/article/details/126013301
  20. //参考: https://blog.csdn.net/cwin8951/article/details/106644118
  21. if (to.matched && to.matched.length>3) {
  22. to.matched.splice(2, to.matched.length - 3)
  23. }
  24. // console.log(to)
  25. // let param = {}
  26. // param.eiToken = ''
  27. // param.username = 'iotedge@iotedge.sense'
  28. // param.token = cookie.get('EIToken')
  29. // param.username = cookie.get('WISEUser')
  30. var loginType = cookie.get('EIName')
  31. console.log(loginType)
  32. if(loginType === 'sso'){
  33. store.dispatch('TzLogin').then(response => {
  34. // 有EIToken
  35. // console.log('pppppppppresult', response)
  36. if (to.path === '/user/login' || to.path === OAUTH2_LOGIN_PAGE_PATH) {
  37. next({ path: INDEX_MAIN_PAGE_PATH })
  38. NProgress.done()
  39. } else {
  40. if (store.getters.permissionList.length === 0) {
  41. store.dispatch('GetPermissionList').then(res => {
  42. const menuData = res.result.menu;
  43. //console.log(res.message)
  44. if (menuData === null || menuData === "" || menuData === undefined) {
  45. return;
  46. }
  47. let constRoutes = [];
  48. constRoutes = generateIndexRouter(menuData);
  49. // 添加主界面路由
  50. store.dispatch('UpdateAppRouter', { constRoutes }).then(() => {
  51. // 根据roles权限生成可访问的路由表
  52. // 动态添加可访问路由表
  53. router.addRoutes(store.getters.addRouters)
  54. const redirect = decodeURIComponent(from.query.redirect || to.path)
  55. if (to.path === redirect) {
  56. // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
  57. next({ ...to, replace: true })
  58. } else {
  59. // 跳转到目的路由
  60. next({ path: redirect })
  61. }
  62. })
  63. })
  64. .catch(() => {
  65. /* notification.error({
  66. message: '系统提示',
  67. description: '请求用户信息失败,请重试!'
  68. })*/
  69. store.dispatch('Logout').then(() => {
  70. next({ path: '/user/login', query: { redirect: to.fullPath } })
  71. })
  72. })
  73. } else {
  74. next()
  75. }
  76. }
  77. })
  78. .catch(() => {
  79. // 没有EIToken-判断有没有token
  80. if (whiteList.indexOf(to.path) !== -1) {
  81. // 在免登录白名单,如果进入的页面是login页面并且当前是OAuth2app环境,就进入OAuth2登录页面
  82. if (to.path === '/user/login' && isOAuth2AppEnv()) {
  83. next({path: OAUTH2_LOGIN_PAGE_PATH})
  84. } else {
  85. // 在免登录白名单,直接进入
  86. next()
  87. }
  88. NProgress.done()
  89. } else {
  90. // 如果当前是在OAuth2APP环境,就跳转到OAuth2登录页面
  91. let path = isOAuth2AppEnv() ? OAUTH2_LOGIN_PAGE_PATH : '/user/login'
  92. next({ path: path, query: { redirect: to.fullPath } })
  93. NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
  94. }
  95. })
  96. }
  97. if(loginType === 'system' || !loginType){
  98. if (Vue.ls.get(ACCESS_TOKEN)) {
  99. /* has token */
  100. if (to.path === '/user/login' || to.path === OAUTH2_LOGIN_PAGE_PATH) {
  101. next({ path: INDEX_MAIN_PAGE_PATH })
  102. NProgress.done()
  103. } else {
  104. if (store.getters.permissionList.length === 0) {
  105. store.dispatch('GetPermissionList').then(res => {
  106. const menuData = res.result.menu;
  107. //console.log(res.message)
  108. if (menuData === null || menuData === "" || menuData === undefined) {
  109. return;
  110. }
  111. let constRoutes = [];
  112. constRoutes = generateIndexRouter(menuData);
  113. // 添加主界面路由
  114. store.dispatch('UpdateAppRouter', { constRoutes }).then(() => {
  115. // 根据roles权限生成可访问的路由表
  116. // 动态添加可访问路由表
  117. router.addRoutes(store.getters.addRouters)
  118. const redirect = decodeURIComponent(from.query.redirect || to.path)
  119. if (to.path === redirect) {
  120. // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
  121. next({ ...to, replace: true })
  122. } else {
  123. // 跳转到目的路由
  124. next({ path: redirect })
  125. }
  126. })
  127. })
  128. .catch(() => {
  129. /* notification.error({
  130. message: '系统提示',
  131. description: '请求用户信息失败,请重试!'
  132. })*/
  133. store.dispatch('Logout').then(() => {
  134. next({ path: '/user/login', query: { redirect: to.fullPath } })
  135. })
  136. })
  137. } else {
  138. // 从添加页面跳转其他页面后需要删除联锁添加页面
  139. if(from.path === '/interLock/summary/add' && !from.meta.replace){
  140. // 为了解决循环设置了replace
  141. from.meta.replace = true
  142. next({ ...to, replace: true })
  143. from.meta.replace = false
  144. } else {
  145. next()
  146. }
  147. // next()
  148. }
  149. }
  150. } else {
  151. if (whiteList.indexOf(to.path) !== -1) {
  152. // 在免登录白名单,如果进入的页面是login页面并且当前是OAuth2app环境,就进入OAuth2登录页面
  153. if (to.path === '/user/login' && isOAuth2AppEnv()) {
  154. next({path: OAUTH2_LOGIN_PAGE_PATH})
  155. } else {
  156. // 在免登录白名单,直接进入
  157. next()
  158. }
  159. NProgress.done()
  160. } else {
  161. // 如果当前是在OAuth2APP环境,就跳转到OAuth2登录页面
  162. let path = isOAuth2AppEnv() ? OAUTH2_LOGIN_PAGE_PATH : '/user/login'
  163. next({ path: path, query: { redirect: to.fullPath } })
  164. NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
  165. }
  166. }
  167. }
  168. })
  169. router.afterEach(() => {
  170. NProgress.done() // finish progress bar
  171. })