<template>
  <div v-loading="loading" style="height: 100%">
    <el-alert v-if="errMsg" :title="errMsg" type="error" effect="dark" :closable="false" style="margin: 20px 20px 0px 20px" />
  </div>
</template>

<script>
// import { Message } from "element-ui";

export default {
  name: 'SSO',
  data: function() {
    return {
      errMsg: null,
      loading: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  async mounted() {
    this.$loading({
      lock: true,
      text: '请稍等...',
      spinner: 'el-icon-loading',
      background: 'rgba(0, 0, 0, 0.7)'
    })
    var ssoUrl = process.env.VUE_APP_SSO_URL
    if (ssoUrl) {
      window.location.href = ssoUrl
    } else {
      this.$store.dispatch('user/loginOidc')
    }
  },
  methods: {
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
</script>

<style scoped></style>
