单点登录+待办跳转

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
  }
}

你可能感兴趣的:(前端,javascript,java)