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.

228 lines
7.4 KiB

5 years ago
2 years ago
5 years ago
5 years ago
5 years ago
2 years ago
5 years ago
2 years ago
5 years ago
5 years ago
5 years ago
2 years ago
5 years ago
5 years ago
2 years ago
5 years ago
2 years ago
5 years ago
2 years ago
5 years ago
5 years ago
2 years ago
5 years ago
5 years ago
5 years ago
2 years ago
5 years ago
2 years ago
5 years ago
2 years ago
5 years ago
2 years ago
5 years ago
2 years ago
5 years ago
2 years ago
5 years ago
2 years ago
5 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 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,removeToken } 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.roleid) >= 0)
  13. }
  14. const whiteList = ['/login', '/authredirect','/changeEmailStepOne','/changeEmailStepTwo','/error']// no redirect whitelist
  15. const scanCodeLoginPath='/mdp/tpa/invite/code/'
  16. var curlDomain=window.location.protocol+"//"+window.location.host; //
  17. var baseUrl=`${curlDomain}/${process.env.CONTEXT}/${process.env.VERSION}/`
  18. router.beforeEach((to, from, next) => {
  19. NProgress.start() // start progress bar
  20. var outUrl="";
  21. if(to.meta.openTab==true && to.meta.outUrl){
  22. outUrl=to.meta.outUrl;
  23. if(to.query){
  24. var querys='';
  25. Object.keys(to.query).forEach(function(key){
  26. if(outUrl.indexOf(key+"=")<=0){
  27. if(querys==''){
  28. querys=key+"="+to.query[key]
  29. }else{
  30. querys=querys+"&"+key+"="+to.query[key]
  31. }
  32. }
  33. });
  34. if(querys!=''){
  35. if(outUrl.indexOf("?")>0){
  36. outUrl=outUrl+"&"+querys;
  37. }else{
  38. outUrl=outUrl+"?"+querys;
  39. }
  40. }
  41. }
  42. if(outUrl.indexOf("${router.path}")>=0){
  43. outUrl=outUrl.replace("${router.path}",to.path);
  44. }
  45. if(outUrl.indexOf("${curlDomain}")>=0){
  46. outUrl=outUrl.replace("${curlDomain}",curlDomain);
  47. }
  48. var indexOfHttp=outUrl.indexOf("://");
  49. if(indexOfHttp>0){
  50. outUrl=outUrl.substr(0,indexOfHttp+3)+outUrl.substr(indexOfHttp+3,outUrl.length).replace("//","/");
  51. }else{
  52. outUrl=outUrl.replace("//","/")
  53. }
  54. window.open(outUrl,to.meta.title,null,true)
  55. NProgress.done() // if current page is dashboard will not trigger afterEach hook, so manually handle it
  56. return;
  57. }
  58. if (getToken()) { // determine if there has token
  59. /* has token*/
  60. if (to.path === '/login') {
  61. removeToken();
  62. next()//
  63. NProgress.done() // if current page is dashboard will not trigger afterEach hook, so manually handle it
  64. } else {
  65. if(store.getters.isLoadOk==false ){
  66. store.dispatch('GetUserInfo').then(res=>{
  67. if(!res.data.tips.isOk){
  68. store.dispatch('FedLogOut').then(() => {
  69. Message.error('请重新登陆')
  70. next({ path: '/login' })
  71. })
  72. }else{
  73. debugger
  74. store.dispatch('GenerateRoutes', {roles:store.getters.roles ,menus:store.getters.myMenus} ).then(() => { // 根据roles权限生成可访问的路由表
  75. router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
  76. next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
  77. }).catch(() => {
  78. store.dispatch('FedLogOut').then(() => {
  79. Message.error('路由处理出错,请重新登陆')
  80. next({ path: '/login' })
  81. })
  82. })
  83. }
  84. });
  85. }else if (store.getters.added==false ) { // 判断当前用户是否已拉取完user_info信息并且已经计算完毕动态路由
  86. store.dispatch('GenerateRoutes', {roles:store.getters.roles ,menus:store.getters.myMenus} ).then(() => { // 根据roles权限生成可访问的路由表
  87. router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
  88. next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
  89. }).catch(() => {
  90. store.dispatch('FedLogOut').then(() => {
  91. Message.error('路由处理出错,请重新登陆')
  92. next({ path: '/login' })
  93. })
  94. })
  95. } else {
  96. // 没有动态改变权限的需求可直接next() 删除下方权限判断 ↓
  97. if ( !to.meta || !to.meta.roles || hasPermission(store.getters.roles, to.meta.roles)) {
  98. next()//
  99. } else {
  100. next({ path: '/401', replace: true, query: { noGoBack: true }})
  101. }
  102. // 可删 ↑
  103. }
  104. }
  105. } else {
  106. /* has no token*/
  107. if (whiteList.indexOf(to.path) !== -1 || to.path.startsWith(scanCodeLoginPath)) { // 在免登录白名单,直接进入
  108. next()
  109. } else {
  110. next('/login') // 否则全部重定向到登录页
  111. NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
  112. }
  113. }
  114. })
  115. var queryParams;
  116. function getQueryVariable(variable,url){
  117. if(!queryParams){
  118. queryParams={}
  119. }else{
  120. return queryParams[variable]
  121. }
  122. var query =url;
  123. if(url==null || url==undefined || url==''){
  124. query=window.location.href;
  125. }
  126. //alert(query);
  127. var query2=query.split("?");
  128. if(query2.length>1){
  129. query=query2[1];
  130. }else{
  131. query=""
  132. return null;
  133. }
  134. var vars = query.split("&");
  135. for (var i=0;i<vars.length;i++) {
  136. var pair = vars[i].split("=");
  137. queryParams[pair[0]]=pair[1]
  138. }
  139. return queryParams[variable];
  140. }
  141. function setIndexPath() {
  142. var indexPath=null
  143. var url=window.location.href;
  144. if(url.indexOf("/login")<=0){
  145. indexPath=url
  146. sessionStorage.setItem("index-path",url);
  147. }
  148. return indexPath
  149. }
  150. var indexPath=setIndexPath();
  151. indexPath=indexPath?indexPath:''
  152. var accessToken=getQueryVariable('accessToken');
  153. if(accessToken!=null){
  154. store.dispatch('LogOut').then(res=>{
  155. setToken(accessToken);
  156. getInfo();
  157. })
  158. }
  159. var isOk=getQueryVariable('isOk');
  160. if(indexPath.indexOf('/#/error')<0 && (isOk=="false" || isOk===false) ){
  161. location.replace(getIndexPathUrl(`${baseUrl}#/error`,queryParams))
  162. }
  163. function getInfo(){
  164. store.dispatch('GetUserInfo').then(res=>{
  165. if(!res.data.tips.isOk){
  166. store.dispatch('LogOut').then(() => {
  167. Message.error('请重新登陆')
  168. location.replace(`${baseUrl}#/login`)
  169. })
  170. }else{
  171. store.dispatch('GenerateRoutes', {roles:store.getters.roles ,menus:store.getters.myMenus} ).then(() => { // 根据roles权限生成可访问的路由表
  172. router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
  173. var inviteId=queryParams['inviteId']
  174. if(inviteId && !inviteId.startsWith('login')){
  175. location.replace(getIndexPathUrl(`${baseUrl}#/mdp/tpa/invite/success`,queryParams))
  176. }else{
  177. location.replace(getIndexPathUrl(indexPath,{}))
  178. }
  179. }).catch(() => {
  180. store.dispatch('LogOut').then(() => {
  181. Message.error('路由处理出错,请重新登陆')
  182. location.replace(`${baseUrl}#/login`)
  183. })
  184. })
  185. }
  186. });
  187. }
  188. function getIndexPathUrl(indexPath,queryParams2){
  189. if(indexPath==null){
  190. indexPath=""
  191. }
  192. var queryParams=queryParams2?{...queryParams2}:{}
  193. delete queryParams.accessToken
  194. //router.push({path:'/mdp/tpa/invite/success',query:queryParams})
  195. var indexQua=indexPath.indexOf("?")
  196. var indexUri="";
  197. if(indexQua<0){
  198. indexUri=indexPath
  199. }else{
  200. indexUri=indexPath.substr(0,indexQua)
  201. }
  202. var indexPathUrl="";
  203. if(Object.keys(queryParams).length>0){
  204. indexPathUrl=indexUri+"?"+Object.keys(queryParams).map(k=>k+'='+queryParams[k]).join('&')
  205. }else{
  206. indexPathUrl=indexUri;
  207. }
  208. return indexPathUrl
  209. }
  210. router.afterEach(() => {
  211. NProgress.done() // finish progress bar
  212. })