单点登录+待办跳转

router.beforeEach(async (to, from, next) => {
  NProgress.start()
  let code = getParams('code') || null
  let state = getParams('state') || null
  let orgId = getParams('orgId') || null
  // 待办跳转
  let Authorization = getParams('Authorization') || null
  if (Authorization) {
    session.save('studyToken', Authorization)
  }
  // 单点登录  start
  if (orgId && !(code && state)) {
    window.location.href = '重定向地址1' 
    
    return
  } else if ((code && state) && !orgId) {
    window.location.href = '重定向地址2'
    例:http://191.12.121.12/val/trainLogin/authorize?code=${code}&state=${state}&orgId=${orgId}
   
    return
    // 单点登录end
  } else {
    if (process.env.NODE_ENV === "development") session.save('studyToken', 'bearer 59af0a70-6ee0-4c53-80b3-8fcb67b4163e')
    // 单点地址获取token
    if (location.href.indexOf('USER_TOKEN_TYPE') !== -1 && location.href.indexOf('USER_TOKEN') !== -1) {
      let token = getParams('USER_TOKEN_TYPE') + ' ' + getParams('USER_TOKEN')
      console.log('token', token)
      session.save('studyToken', token)
    }
    const studyToken = session.get('studyToken')
    console.log('studyToken', studyToken)
    
    if (studyToken) {
      console.log('存在studyToken')
      if (to.path === '/login') {
        next({ path: '/' })
        NProgress.done()
      } else {
        if (Authorization) {
          let href = location.href.split('?')[0]
          window.location.href = href
          location.reload()
        } else {
          next()
        }
      }
    } else {
      console.log('不存在studyToken')
      next()
      NProgress.done()
    }
  }
})
// 获取url携带的参数
function getParams (name) {
  var search = window.location.search.substring(1)
  if (!search) {
    search = window.location.hash.split('?')[1]
  }
  if (search) {
    var obj = JSON.parse('{"' + decodeURIComponent(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') + '"}')
    return name ? obj[name] : obj
  }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值