ssoLogin.js 2.06 KB
import HtProgress from 'nprogress'
import store from '@/store'
import {
  recordRoute,
  routesWhiteList,
  progressBar,
  tokenName,
  casTokenName,
  oauthTokenName,
} from '@/config'

export default function (to, from, next) {
  // 白名单页面
  if (
    routesWhiteList.indexOf(to.path) !== -1 ||
    routesWhiteList.indexOf(to.name) !== -1
  ) {
    next()
  } else if (to.query && to.query[tokenName]) {
    // 先跳转到加载中页面
    next({ path: '/loading' })
    store
      .dispatch('user/authentication', to.query)
      .then(() => {
        // 鉴权完成后再跳转回来
        setTimeout(() => {
          next({ ...to, replace: true })
        }, 100)
      })
      .catch(() => {
        next({ path: '/500', replace: true })
      })
  } else if (window.ssoConfig.mode == 'cas') {
    // cas单点登录
    if (to.query && to.query[casTokenName]) {
      const ticket = to.query[casTokenName]
      store
        .dispatch('user/ssoLogin', {
          ticket,
          service: window.location.href.split('?')[0],
        })
        .then(() => {
          next({ ...to, replace: true })
        })
        .catch(() => {
          next({ path: '/500', replace: true })
        })
    } else {
      window.location.href = `${window.ssoConfig.url}?service=${window.location.href}`
    }
  } else if (window.ssoConfig.mode == 'oauth') {
    // oauth单点登录
    if (to.query && to.query[oauthTokenName]) {
      const code = to.query[oauthTokenName]
      store
        .dispatch('user/ssoLogin', {
          code,
          service: window.location.href.split('?')[0],
        })
        .then(() => {
          next({ ...to, replace: true })
        })
        .catch(() => {
          next({ path: '/500', replace: true })
        })
    } else {
      window.location.href = `${window.ssoConfig.url}?response_type=code&client_id=${window.ssoConfig.clientId}&redirect_uri=${window.location.href}`
    }
  } else {
    if (recordRoute) {
      next(`/login?redirect=${to.fullPath}`)
    } else {
      next('/login')
    }
    if (progressBar) HtProgress.done()
  }
}