<!-- 小程序统一认证 -->
<template>
  <div class="miniProgramAuthentication">

  </div>
</template>
<script>
  import { Toast } from 'vant';
  import { Encrypt,Decrypt} from '@/utils/secret'
  import {
    getConfigByType,
    systemSubjectCheckIsAdminOrSafeUser,
    securityDataStatisticsGetUserIdentity
  } from '@/api/index.js'
  import wx from "weixin-webview-jssdk";
  export default {
    name: 'index',
    data () {
      return {}
    },
    created () {
      let self = this;
      let url = decodeURIComponent(window.location.href);
      if (url.indexOf('token') != -1) {
        let list = url.split("?")[1].split("&");
        let codeData = {};
        list.forEach((item) => {
          codeData[item.split("=")[0]] = item.split("=")[1];
        });
        if(localStorage.getItem('loginH5')){
          localStorage.setItem('touken',codeData.token);
          localStorage.removeItem('loginH5');
          setTimeout(function(){
            //等待配置与字段获取到后跳转
            Promise.all([
              //获取开发配置
              self.getConfigByType(),
            ]).then((result) => {
              //查询是否是管理员/安全员
              self.systemSubjectCheckIsAdminOrSafeUser()
            }).catch((error) => {
              Toast.fail('数据异常,请稍候再试!');
            })
          },500);
        }else{
          wx.miniProgram.navigateTo({
            url: '/pages/views/login/ssoCertification?token='+codeData.token
          })
        }
      } else if (url.indexOf('code') != -1) {
        //code 异常状态处理
        if(url.split('=')[1] == '10100'){
          if(localStorage.getItem('loginH5')){
            localStorage.removeItem('loginH5');
            Toast.fail('当前用户不存在，请联系管理员!');
            setTimeout(function(){
              self.$router.push({ path: '/login' });
            },2000);
          }else{
            //当前用户不存在，请联系管理员!
            wx.miniProgram.navigateTo({
              url: '/pages/views/login/ssoCertification?code=10100'
            })
          }
        }else if(url.split('=')[1] == '500'){
          if(localStorage.getItem('loginH5')){
            localStorage.removeItem('loginH5')
            Toast.fail('服务异常,请稍候再试!');
            setTimeout(function(){
              self.$router.push({ path: '/login' });
            },2000);
          }else{
            //服务异常,请稍候再试!
            wx.miniProgram.navigateTo({
              url: '/pages/views/login/ssoCertification?code=500'
            })
          }
        }
      } else {
        //跳转至认证页面
        window.location.href = process.env.VUE_APP_OUT_URL;
      }
    },
    mounted () {

    },
    methods: {

      //获取开发配置
      getConfigByType() {
        getConfigByType({category: 2, configType: 5}).then(response => {
          let obj = JSON.parse(response.data.configValue)
          //文件预览地址
          localStorage.setItem('filePreviewUrl', 'https://' + obj.fileExtranetUrl)
          //小程序视频地址
          localStorage.setItem('cameraExtranetAgent', 'https://' + obj.cameraExtranetAgent)
          //MQTT地址
          localStorage.setItem('mqttUrl', Decrypt(obj.mqttExtranetUrl))
          //MQTT地址-内网
          localStorage.setItem('mqttIntranetUrl', Decrypt(obj.mqttIntranetUrl))
          //MQTT账号
          localStorage.setItem('mqttUser', Decrypt(obj.mqttExtranetUser))
          //MQTT密码
          localStorage.setItem('mqttPassword', Decrypt(obj.mqttExtranetPassword))
          //文件浏览环境
          localStorage.setItem('fileBrowseEnvironment', 'http://' + Decrypt(obj.fileBrowseEnvironment))
          localStorage.setItem('fileBrowseEnvironmentExtranet', 'http://' + Decrypt(obj.fileBrowseEnvironmentExtranet))
        })
      },
      //查询是否是管理员/安全员
      systemSubjectCheckIsAdminOrSafeUser(){
        systemSubjectCheckIsAdminOrSafeUser({}).then(response => {
          if (response.data.isSafe || response.data.isAdmin) {
            window.location.href = localStorage.getItem('alarmUrl');
          }else{
            this.securityDataStatisticsGetUserIdentity();
          }
        })
      },
      //查询是否是校级管理员/院级管理员
      securityDataStatisticsGetUserIdentity(){
        let self = this;
        securityDataStatisticsGetUserIdentity({}).then(response => {
          if(response.schoolAdmin || response.collegeAdmin){
            window.location.href = localStorage.getItem('alarmUrl');
          }else{
            localStorage.removeItem('touken');
            Toast.fail('您无权查看报警信息,请联系管理员.');
            setTimeout(function(){
              self.$router.push({ path: '/login' });
            },2000);
          }
        })
      },
    },
  }
</script>
<style scoped lang="scss">
  .miniProgramAuthentication{

  }
</style>
