<!-- 认证平台登录 -->
<template>
  <div class="sso-login-page">
  </div>
</template>

<script>
  //                      V3
  import { encrypt, decrypt } from '@/utils/jsencrypt'
  import { Encrypt,Decrypt} from '@/utils/secret'
  import store from '@/store'
  import { login,authInfo,getCodeImg,getConfigByType,configInfo} from "@/api/login";
  import { setToken } from '@/utils/auth'

  export default {
    name: 'Login',
    data() {
      return {
        redirect: undefined,
        rectangleLogo: localStorage.getItem('rectangleLogo')
      }
    },
    watch: {
      $route: {
        handler: function(route) {
          this.redirect = route.query && route.query.redirect
        },
        immediate: true
      }
    },
    created() {
      localStorage.setItem('entranceJumpType','SCHOOL');
      let url = window.location.href
      if (url.indexOf('token') != -1) {
        // console.log('找到了====>token')
        setToken(url.split('=')[1])
        this.getInfo();
      } else if (url.indexOf('code') != -1) {
        // console.log('找到了====>code')
        //异常状态处理
        if(url.split('=')[1] == '10100'){
          //10100 用户信息不存在
          this.$alert('当前用户不存在，请联系管理员!', '提示', {
            confirmButtonText: '确定',
            callback: action => {
              store.dispatch('LogOut').then(() => {
                window.location.href = process.env.VUE_APP_OUT_URL;
              })
            }
          });
        }else if(url.split('=')[1] == '500'){
          //用户服务异常
          this.$alert('服务异常,请稍候再试!', '提示', {
            confirmButtonText: '确定',
            callback: action => {
              store.dispatch('LogOut').then(() => {
                window.location.href = process.env.VUE_APP_OUT_URL;
              })
            }
          });
        }
      } else {
        window.location.href = process.env.VUE_APP_OUT_URL;
      }
      localStorage.setItem('windowHref', window.location.href)
    },
    mounted() {

    },
    methods: {
      getInfo() {
        //等待配置与字段获取到后跳转
        Promise.all([
          this.configInfo(),
          this.getExploitConfig(),
          this.fullScreen()
        ]).then((result)=>{
          this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
        }).catch((error) => {})
      },
      //获取公共配置数据
      configInfo(){
        let self = this;
        configInfo({ type: '1,2,3' }).then(response => {
          let list = JSON.parse(response.data)
          let newData = {};
          list.forEach((item) => {
            let obj = JSON.parse(item.configValue)
            newData = {...newData,...obj}
          })
          setTimeout(function(){
            self.$set(self,'rectangleLogo',newData.rectangleLogo);
            localStorage.setItem('schoolName',newData.schoolName)
            localStorage.setItem('circularLogo',newData.circularLogo)
            localStorage.setItem('rectangleLogo',newData.rectangleLogo)
            localStorage.setItem('videoCover',newData.videoCover)
            localStorage.setItem('setSmartAlarmType',newData.smartLock)
          },200);
        });
      },
      //获取开发配置
      getExploitConfig(){
        getConfigByType({ category: 2, configType: 5 }).then(response => {
          let obj = JSON.parse(response.data.configValue);
          //判定http或者https
          let urlText = window.location.href.split('://')[0]+'://';
          let outerNet = window.location.href.indexOf(obj.ipIdentify) == -1//true外网 false 内网
          if(outerNet){//外网
            //MQTT地址
            localStorage.setItem('mqttUrl','wss://'+Decrypt(obj.mqttExtranetUrl))
            //MQTT账号
            localStorage.setItem('mqttUser',Decrypt(obj.mqttExtranetUser))
            //MQTT密码
            localStorage.setItem('mqttPassword',Decrypt(obj.mqttExtranetPassword))
            //PC端回跳地址
            localStorage.setItem('pcUrl',urlText+obj.pcExtranetUrl)
            localStorage.setItem('fileBrowseEnvironment',urlText+Decrypt(obj.fileBrowseEnvironmentExtranet))
            localStorage.setItem('fileBrowseEnvironmentExtranet',urlText+Decrypt(obj.fileBrowseEnvironmentExtranet))
          }else{
            //MQTT地址
            localStorage.setItem('mqttUrl','ws://'+Decrypt(obj.mqttIntranetUrl))
            //MQTT账号
            localStorage.setItem('mqttUser',Decrypt(obj.mqttIntranetUser))
            //MQTT密码
            localStorage.setItem('mqttPassword',Decrypt(obj.mqttIntranetPassword))
            //PC端回跳地址
            localStorage.setItem('pcUrl',urlText+obj.pcIntranetUrl)
            localStorage.setItem('fileBrowseEnvironment',urlText+Decrypt(obj.fileBrowseEnvironment))
            localStorage.setItem('fileBrowseEnvironmentExtranet',urlText+Decrypt(obj.fileBrowseEnvironmentExtranet))
          }
        });
      },
      //全屏
      fullScreen() {
        var element = document.documentElement;
        if (element.requestFullscreen) {
          element.requestFullscreen();
        } else if (element.msRequestFullscreen) {
          element.msRequestFullscreen();
        } else if (element.mozRequestFullScreen) {
          element.mozRequestFullScreen();
        } else if (element.webkitRequestFullscreen) {
          element.webkitRequestFullscreen();
        }
      },
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss">
  .sso-login-page {
    height: 100%;
    width: 100%;
    background-image: url("../assets/ZDimages/basicsModules/icon_dl_bbg.png");
    background-repeat: no-repeat;
    background-size: cover;
    -webkit-background-size: cover;
    -o-background-size: cover;
    position: relative;
    * {
      margin: 0;
    }
  }
</style>
