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.

174 lines
5.8 KiB

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