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. store.dispatch('GenerateRoutes', {roles:store.getters.roles ,menus:store.getters.myMenus} ).then(() => { // 根据roles权限生成可访问的路由表
  74. router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
  75. next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
  76. }).catch(() => {
  77. store.dispatch('FedLogOut').then(() => {
  78. Message.error('路由处理出错,请重新登陆')
  79. next({ path: '/login' })
  80. })
  81. })
  82. }
  83. });
  84. }else if (store.getters.added==false ) { // 判断当前用户是否已拉取完user_info信息并且已经计算完毕动态路由
  85. store.dispatch('GenerateRoutes', {roles:store.getters.roles ,menus:store.getters.myMenus} ).then(() => { // 根据roles权限生成可访问的路由表
  86. router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
  87. next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
  88. }).catch(() => {
  89. store.dispatch('FedLogOut').then(() => {
  90. Message.error('路由处理出错,请重新登陆')
  91. next({ path: '/login' })
  92. })
  93. })
  94. } else {
  95. // 没有动态改变权限的需求可直接next() 删除下方权限判断 ↓
  96. if ( !to.meta || !to.meta.roles || hasPermission(store.getters.roles, to.meta.roles)) {
  97. next()//
  98. } else {
  99. next({ path: '/401', replace: true, query: { noGoBack: true }})
  100. }
  101. // 可删 ↑
  102. }
  103. }
  104. } else {
  105. /* has no token*/
  106. if (whiteList.indexOf(to.path) !== -1 || to.path.startsWith(scanCodeLoginPath)) { // 在免登录白名单,直接进入
  107. next()
  108. } else {
  109. next('/login') // 否则全部重定向到登录页
  110. NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
  111. }
  112. }
  113. })
  114. var queryParams;
  115. function getQueryVariable(variable,url){
  116. if(!queryParams){
  117. queryParams={}
  118. }else{
  119. return queryParams[variable]
  120. }
  121. var query =url;
  122. if(url==null || url==undefined || url==''){
  123. query=window.location.href;
  124. }
  125. //alert(query);
  126. var query2=query.split("?");
  127. if(query2.length>1){
  128. query=query2[1];
  129. }else{
  130. query=""
  131. return null;
  132. }
  133. var vars = query.split("&");
  134. for (var i=0;i<vars.length;i++) {
  135. var pair = vars[i].split("=");
  136. queryParams[pair[0]]=pair[1]
  137. }
  138. return queryParams[variable];
  139. }
  140. function setIndexPath() {
  141. var indexPath=null
  142. var url=window.location.href;
  143. if(url.indexOf("/login")<=0){
  144. indexPath=url
  145. sessionStorage.setItem("index-path",url);
  146. }
  147. return indexPath
  148. }
  149. var indexPath=setIndexPath();
  150. indexPath=indexPath?indexPath:''
  151. var accessToken=getQueryVariable('accessToken');
  152. if(accessToken!=null){
  153. store.dispatch('LogOut').then(res=>{
  154. setToken(accessToken);
  155. getInfo();
  156. })
  157. }
  158. var isOk=getQueryVariable('isOk');
  159. if(indexPath.indexOf('/#/error')<0 && (isOk=="false" || isOk===false) ){
  160. location.replace(getIndexPathUrl(`${baseUrl}#/error`,queryParams))
  161. }
  162. function getInfo(){
  163. store.dispatch('GetUserInfo').then(res=>{
  164. if(!res.data.tips.isOk){
  165. store.dispatch('LogOut').then(() => {
  166. Message.error('请重新登陆')
  167. location.replace(`${baseUrl}#/login`)
  168. })
  169. }else{
  170. store.dispatch('GenerateRoutes', {roles:store.getters.roles ,menus:store.getters.myMenus} ).then(() => { // 根据roles权限生成可访问的路由表
  171. router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
  172. var inviteId=queryParams['inviteId']
  173. if(inviteId && !inviteId.startsWith('login')){
  174. location.replace(getIndexPathUrl(`${baseUrl}#/mdp/tpa/invite/success`,queryParams))
  175. }else{
  176. location.replace(getIndexPathUrl(indexPath,{}))
  177. }
  178. }).catch(() => {
  179. store.dispatch('LogOut').then(() => {
  180. Message.error('路由处理出错,请重新登陆')
  181. location.replace(`${baseUrl}#/login`)
  182. })
  183. })
  184. }
  185. });
  186. }
  187. function getIndexPathUrl(indexPath,queryParams2){
  188. if(indexPath==null){
  189. indexPath=""
  190. }
  191. var queryParams=queryParams2?{...queryParams2}:{}
  192. delete queryParams.accessToken
  193. //router.push({path:'/mdp/tpa/invite/success',query:queryParams})
  194. var indexQua=indexPath.indexOf("?")
  195. var indexUri="";
  196. if(indexQua<0){
  197. indexUri=indexPath
  198. }else{
  199. indexUri=indexPath.substr(0,indexQua)
  200. }
  201. var indexPathUrl="";
  202. if(Object.keys(queryParams).length>0){
  203. indexPathUrl=indexUri+"?"+Object.keys(queryParams).map(k=>k+'='+queryParams[k]).join('&')
  204. }else{
  205. indexPathUrl=indexUri;
  206. }
  207. return indexPathUrl
  208. }
  209. router.afterEach(() => {
  210. NProgress.done() // finish progress bar
  211. })