You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
5.6 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
  1. import router from './router'
  2. import store from './store'
  3. import { Message } from 'element-ui'
  4. import NProgress from 'nprogress' // progress bar
  5. import 'nprogress/nprogress.css'// progress bar style
  6. import { getToken,setToken } from '@/utils/auth' // getToken from cookie
  7. NProgress.configure({ showSpinner: false })// NProgress Configuration
  8. // permissiom judge function
  9. function hasPermission(roles, permissionRoles) {
  10. if (!permissionRoles) return true
  11. if (roles.some(role => role.roleid==='superAdmin')) return true // admin permission passed directly
  12. return roles.some(role => permissionRoles.indexOf(role) >= 0)
  13. }
  14. const whiteList = ['/login', '/authredirect','/changeEmailStepOne','/changeEmailStepTwo']// no redirect whitelist
  15. router.beforeEach((to, from, next) => {
  16. NProgress.start() // start progress bar
  17. var outUrl="";
  18. if(to.meta.openTab==true && to.meta.outUrl){
  19. outUrl=to.meta.outUrl;
  20. if(to.query){
  21. var querys='';
  22. Object.keys(to.query).forEach(function(key){
  23. if(outUrl.indexOf(key+"=")<=0){
  24. if(querys==''){
  25. querys=key+"="+to.query[key]
  26. }else{
  27. querys=querys+"&"+key+"="+to.query[key]
  28. }
  29. }
  30. });
  31. if(querys!=''){
  32. if(outUrl.indexOf("?")>0){
  33. outUrl=outUrl+"&"+querys;
  34. }else{
  35. outUrl=outUrl+"?"+querys;
  36. }
  37. }
  38. }
  39. if(outUrl.indexOf("${router.path}")>=0){
  40. outUrl=outUrl.replace("${router.path}",to.path);
  41. }
  42. if(outUrl.indexOf("${curlDomain}")>=0){
  43. var curlDomain=window.location.protocol+"//"+window.location.host; // 返回https://mp.csdn.net
  44. outUrl=outUrl.replace("${curlDomain}",curlDomain);
  45. }
  46. var indexOfHttp=outUrl.indexOf("://");
  47. if(indexOfHttp>0){
  48. outUrl=outUrl.substr(0,indexOfHttp+3)+outUrl.substr(indexOfHttp+3,outUrl.length).replace("//","/");
  49. }else{
  50. outUrl=outUrl.replace("//","/")
  51. }
  52. window.open(outUrl,to.meta.title,null,true)
  53. NProgress.done() // if current page is dashboard will not trigger afterEach hook, so manually handle it
  54. return;
  55. }
  56. if(to!=null && to.fullPath!=null){
  57. var accessToken=getQueryVariable("accessToken",to.fullPath);
  58. if(accessToken!=null){
  59. setToken(accessToken);
  60. }
  61. }
  62. if (getToken()) { // determine if there has token
  63. /* has token*/
  64. if (to.path === '/login') {
  65. next()//
  66. NProgress.done() // if current page is dashboard will not trigger afterEach hook, so manually handle it
  67. } else {
  68. if(store.getters.isLoadOk==false ){
  69. store.dispatch('GetUserInfo').then(res=>{
  70. if(!res.data.tips.isOk){
  71. store.dispatch('FedLogOut').then(() => {
  72. Message.error('请重新登陆')
  73. next({ path: '/login' })
  74. })
  75. }else{
  76. store.dispatch('GenerateRoutes', {roles:store.getters.roles ,menus:store.getters.myMenus} ).then(() => { // 根据roles权限生成可访问的路由表
  77. router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
  78. next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
  79. }).catch(() => {
  80. store.dispatch('FedLogOut').then(() => {
  81. Message.error('路由处理出错,请重新登陆')
  82. next({ path: '/login' })
  83. })
  84. })
  85. }
  86. });
  87. }else if (store.getters.added==false ) { // 判断当前用户是否已拉取完user_info信息并且已经计算完毕动态路由
  88. store.dispatch('GenerateRoutes', {roles:store.getters.roles ,menus:store.getters.myMenus} ).then(() => { // 根据roles权限生成可访问的路由表
  89. router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
  90. next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
  91. }).catch(() => {
  92. store.dispatch('FedLogOut').then(() => {
  93. Message.error('路由处理出错,请重新登陆')
  94. next({ path: '/login' })
  95. })
  96. })
  97. } else {
  98. // 没有动态改变权限的需求可直接next() 删除下方权限判断 ↓
  99. if (hasPermission(store.getters.roles, to.meta.roles)) {
  100. next()//
  101. } else {
  102. next({ path: '/401', replace: true, query: { noGoBack: true }})
  103. }
  104. // 可删 ↑
  105. }
  106. }
  107. } else {
  108. /* has no token*/
  109. if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入
  110. next()
  111. } else {
  112. next('/login') // 否则全部重定向到登录页
  113. NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
  114. }
  115. }
  116. })
  117. function getQueryVariable(variable,url){
  118. var query =url;
  119. if(url==null || url==undefined || url==''){
  120. query=window.location.href;
  121. }
  122. //alert(query);
  123. var query2=query.split("?");
  124. if(query2.length>1){
  125. query=query2[1];
  126. }else{
  127. query=""
  128. return null;
  129. }
  130. var vars = query.split("&");
  131. for (var i=0;i<vars.length;i++) {
  132. var pair = vars[i].split("=");
  133. if(pair[0] == variable){return pair[1];}
  134. }
  135. return null;
  136. }
  137. function setIndexPath() {
  138. var indexPath=null
  139. var url=window.location.href;
  140. if(url.indexOf("/login")<=0){
  141. var indexOf=url.indexOf("/#")
  142. if(indexOf > 0){
  143. indexPath=url.substring(indexOf+2)
  144. localStorage.setItem("index-path",indexPath);
  145. }else{
  146. localStorage.setItem("index-path",null);
  147. }
  148. }
  149. }
  150. setIndexPath();
  151. var accessToken=getQueryVariable('accessToken');
  152. if(accessToken!=null){
  153. //alert(access_token);
  154. setToken(accessToken);
  155. }
  156. router.afterEach(() => {
  157. NProgress.done() // finish progress bar
  158. })