<template>
  <div id="app" class="dm-reset-style">
    <router-view></router-view>
  </div>
</template>

<script>
import { mapState } from 'vuex';

export default {
  name: 'App',

  computed: {
    ...mapState([
      'uniqueId'
    ]),
  },

  created() {
    this.baseUrl = window.location.origin.indexOf('localhost') > -1 ? 'http://gicdev.demogic.com' : window.location.origin;
    this._getUserInfo();
    this.changeTab();
  },

  methods: {
    laout() {
      this.axios.get(`${this.baseUrl}/api-auth/do-logout?requestProject=member`);
    },
    _getUserInfo() {
      this.axios.get(`${this.baseUrl}/api-auth/get-login-user-info?requestProject=member`).then((res) => {
        if (res.data.errorCode == 0) {
          const uId = res.data.result && res.data.result.userId;
          if (this.uniqueId === '') {
            this.$store.commit('changeUniqueId', uId);
          }
        }
      });
    },
    changeTab() {
      document.addEventListener("visibilitychange", () => {
        // 表示切换过来去查uid跟当前uid对比
        if (document.visibilityState === 'visible') {
          this.axios.get(`${this.baseUrl}/api-auth/get-login-user-info?requestProject=member`).then(res => {
            if (res.data.errorCode == 0) {
              const uId = res.data.result && res.data.result.userId;
              // 两次的uId不等 表示账号有冲突
              if (this.uniqueId !== uId) {
                this.$confirm('当前登录账号已经发生变化，如果您在其他页面已经登录另一个账号，请退出重新登录！', '登录账号变更提示', {
                  confirmButtonText: '退出重新登录',
                  cancelButtonText: '刷新页面',
                  type: 'error'
                }).then(() => {
                  this.laout();
                  window.location.reload();
                }).catch(() => {
                  this.laout();
                  window.location.reload();
                });
              }
            }
          });
        }
      });
    }
  }
}
</script>
