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

<script>
  // import { getInfo } from '@/api/login'
  //                      V3
  import Cookies from "js-cookie";
  import { encrypt, decrypt } from '@/utils/jsencrypt'
  import { Encrypt,Decrypt} from '@/utils/secret'
  import store from '@/store'
  import { getConfigByType } from "@/api/commonality/permission";
  import { configInfo } from "@/api/commonality/noPermission";
  import { authInfo} from "@/api/basicsModules/index";
  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() {
      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 => {
              localStorage.removeItem('serviceType');
              store.dispatch('LogOut').then(() => {
                window.location.href = process.env.VUE_APP_OUT_URL;
              })
            }
          });
        }else if(url.split('=')[1] == '500'){
          //用户服务异常
          this.$alert('服务异常,请稍候再试!', '提示', {
            confirmButtonText: '确定',
            callback: action => {
              localStorage.removeItem('serviceType');
              store.dispatch('LogOut').then(() => {
                window.location.href = process.env.VUE_APP_OUT_URL;
              })
            }
          });
        }
      } else {
        this.$alert('参数异常!', '提示', {
          confirmButtonText: '确定',
          callback: action => {
            localStorage.removeItem('serviceType');
            store.dispatch('LogOut').then(() => {
              window.location.href = process.env.VUE_APP_OUT_URL;
            })
          }
        });
      }
      localStorage.setItem('windowHref', window.location.href)
    },
    mounted() {

    },
    methods: {
      getInfo(type) {
        //等待配置与字段获取到后跳转
        Promise.all([
          this.configInfo(),
          this.getExploitConfig(),
          this.getHomeConfig()
        ]).then((result)=>{
          this.$router.push({ path: this.redirect || "/home" }).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)
          },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){//外网
            //文件预览地址
            localStorage.setItem('filePreviewUrl',urlText+obj.fileExtranetUrl)
            //摄像头代理访问地址
            localStorage.setItem('cameraExtranetAgent',urlText+obj.cameraExtranetAgent)
            //摄像头地址ip段
            localStorage.setItem('ipIdentify',obj.ipIdentify)
            //摄像头访问地址
            localStorage.setItem('cameraUrl','wss://'+obj.cameraExtranetUrl)
            //MQTT地址
            localStorage.setItem('mqttUrl','wss://'+Decrypt(obj.mqttExtranetUrl))
            //MQTT账号
            localStorage.setItem('mqttUser',Decrypt(obj.mqttExtranetUser))
            //MQTT密码
            localStorage.setItem('mqttPassword',Decrypt(obj.mqttExtranetPassword))
            //可视化大屏访问地址
            localStorage.setItem('screenUrl',urlText+obj.screenExtranetUrl)
            //小程序二维码规则
            localStorage.setItem('codeOnlineAdd',obj.codeOnlineAdd)
            //文件浏览环境
            localStorage.setItem('fileBrowseEnvironment',Decrypt(obj.fileBrowseEnvironment))
            localStorage.setItem('fileBrowseEnvironmentExtranet','https://'+Decrypt(obj.fileBrowseEnvironmentExtranet))
          }else{
            //文件预览地址
            localStorage.setItem('filePreviewUrl',urlText+obj.fileIntranetUrl)
            //摄像头代理访问地址
            localStorage.setItem('cameraExtranetAgent',urlText+obj.cameraIntranetAgent)
            //摄像头地址ip段
            localStorage.setItem('ipIdentify',obj.ipIdentify)
            //摄像头访问地址
            localStorage.setItem('cameraUrl','wss://'+obj.cameraIntranetUrl)
            //MQTT地址
            localStorage.setItem('mqttUrl','ws://'+Decrypt(obj.mqttIntranetUrl))
            //MQTT账号
            localStorage.setItem('mqttUser',Decrypt(obj.mqttIntranetUser))
            //MQTT密码
            localStorage.setItem('mqttPassword',Decrypt(obj.mqttIntranetPassword))
            //可视化大屏访问地址
            localStorage.setItem('screenUrl',urlText+obj.screenIntranetUrl)
            //小程序二维码规则
            localStorage.setItem('codeOnlineAdd',obj.codeOnlineAdd)
            //文件浏览环境
            localStorage.setItem('fileBrowseEnvironment',Decrypt(obj.fileBrowseEnvironment))
            localStorage.setItem('fileBrowseEnvironmentExtranet','https://'+Decrypt(obj.fileBrowseEnvironmentExtranet))
          }
        });
      },
      //获取首页配置
      getHomeConfig(){
        getConfigByType({ category: 2, configType: 6 }).then(response => {
          localStorage.setItem('homeConfig',response.data.configValue);
          let obj = JSON.parse(response.data.configValue)
          localStorage.setItem('initPage',obj.skipHomePage);
        });
      },
    }
  }
</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>
