<template>
  <div id="app">
    <router-view  v-if="isRouterAlive"></router-view>
  </div>
</template>

<script>
import axios from 'axios'
import { getOrgs, changeOrg } from "@/api/config";
export default {
  name: "App",
  data () {
    return {
      isRouterAlive: true,

      showErr: false,
      showWsErr: false,
    }
  },
  computed: {
    websocket() {
      return this.$store.state.user.websocket;
    }
  },
  created(){
    // getOrgs().then(response => {
    //   if (response.data.state === 1) {
    //     var creator = response.data.data.creator;
    //     console.log("creator", creator);
    //     sessionStorage.setItem("org_id",creator.org_id);
    //     sessionStorage.setItem("admin_user_id",creator.admin_user_id);
    //     this.getToken(creator.org_id,creator.admin_user_id)
    //   }
    // });
    
  },
  methods: {
    reload () {
      this.isRouterAlive = false
      this.$nextTick(function () {
        this.isRouterAlive = true
      })
    },
    getToken(orgId,adminUserId){
      axios.get('/api/index/gettoken/'+ orgId + '/'+ adminUserId
        ).then((res) => {
        console.log('res',res.data)
        
        if(res.data.code == 0){
          let token = res.data.data.token
          localStorage.setItem("token",token)
          let http = 'ws://socket.sgjyun.com?token='
          console.log(this.$store.state.user.websocket)
          this.$store.state.user.websocket = new ReconnectingWebSocket(
            http + token
          );
          this.$store.state.user.websocket.timeoutInterval = 3000;
          setInterval(() => {
            if (navigator.onLine == false) {
              this.showWsErr = true;
              this.wsErrMsg = "网络已断开，请检查网络后重新打开页面";
              alert(this.wsErrMsg)
              console.log('navigator',navigator)
            } else if (
              navigator.onLine == true &&
              this.websocket.readyState == 1
            ) {
              this.showWsErr = false;
            }
            this.websocket.send("1");
          }, 3000);
        }
        
      }).catch(error => {
          window.location.href = error.response.data.redirect;
        });
    }
  },
  
};
</script>

<style>
body .el-table th.gutter {
  display: table-cell !important;
}
</style>
