<template>
</template>
<script>
  import { Notification } from 'element-ui';
  import { getShopInf } from '@/api/module/entry';
  import request from '@/utils/request'
  export default {
    name: "Platformlogin",
    data(){
      return{
        ssourl:''
      }
    },
    created() {
      this.getssourl();
    },
    methods: {
       getssourl(){
          debugger
          var ssa = window.location.host
          // var ssa = 'scm.jxhh.com'
          let data = {
            domain:ssa
          }
          this.$store.dispatch('getssourl', data)
                .then((res) => {
                if(res.code==1){
                  this.ssourl = res.data.sso_url
                  this.gotosso()
                }
                })
                .catch(() => {

                })
    },
      gotosso() {
        var ticket = this.$route.query.ticket;
        this.$store.dispatch('Login', {"ticket": ticket}).then((res) => {
          debugger
          if (res.token != "") {
            // 获取客户是否有审核信息
            getShopInf().then(res => {
              if(res.code === 1) {
                switch(res.data.state) {
                  case 0:
                    switch(res.data.status){
                      case(0):
                        this.$router.push({path: '/process'});
                        break;
                      case(1):
                        this.$router.push({path: '/fillShopInf'});
                        break;
                    }
                    break;
                  case 1: // 审核成功，要跳转到首页
                    this.$router.push({path: '/home/index'});
                    break;
                  case 2: // 审核中
                    this.$router.push({path: '/reviewing'});
                    break;
                  case 3: // 审核失败
                    let msg = res.data.Apply.content;
                    this.$router.push({
                      name: 'reviewFail',
                      params: { msg: msg }
                    });
                    break;
                }
              }else {
                if(res && res.msg) {
                  Notification.error({
                    title: '提示：',
                    message: res.msg
                  })
                }else {
                  Notification.error({
                    title: '提示：',
                    message: '刷新浏览器后，再次登录，若仍不行，请联系我们！'
                  })
                }
              }
            });

          } else {
            this.$store.dispatch('Statistic')
            this.$store.dispatch('FedLogOut')
            var ssa = window.location.host;
            var ht = document.location.protocol
            let that = this
            setTimeout(function () {
              if (ssa == 'localhost:1024') {
                window.location.href = ht+'//'+that.ssourl+'/api/v2/logout?redirect='+ht+'//' + ssa + '/login'
              } else {
                window.location.href = ht+'//'+that.ssourl+'/api/v2/logout?redirect='+ht+'//' + ssa + '/login'
              }
            }, 1000)
          }
          this.loading = false
        })
          .catch(() => {
            this.$store.dispatch('FedLogOut')
            var ssa = window.location.host
            var ht = document.location.protocol
            let that = this
            setTimeout(function () {
              if (ssa == 'localhost:1024') {
                window.location.href = ht+'//'+that.ssourl+'/api/v2/logout?redirect='+ht+'//' + ssa + '/login'
              } else {
                window.location.href = ht+'//'+that.ssourl+'/api/v2/logout?redirect='+ht+'//' + ssa + '/login'
              }
            }, 1000)
            this.loading = false
          })
      },
    }
  };

</script>
