<template>
  <div id="app">
    <transition name="fade" mode="out-in">
      <router-view></router-view>
    </transition>
  </div>
</template>

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

export default {
  name: 'App',
  data() {
    return {};
  },

  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=gic-member-tag-web`);
    },
    _getUserInfo() {
      this.axios.get(`${this.baseUrl}/api-auth/get-login-user-info?requestProject=gic-member-tag-web`).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=gic-member-tag-web`).then(res => {
            if (res.data.errorCode == 0) {
              const uId = res.data.result && res.data.result.userId;
              // 两次的uId不等 表示账号有冲突
              if (this.uniqueId !== uId) {
                this.$store.commit('changeUniqueId', uId);
                this.$confirm('当前登录账号已经发生变化，如果您在其他页面已经登录另一个账号，请重新登录！', '登录账号变更提示', {
                  confirmButtonText: '重新登录',
                  cancelButtonText: '刷新页面',
                  type: 'error'
                })
                  .then(() => {
                    window.location.href = '/gic-web/#/login';
                  })
                  .catch(() => {
                    window.location.href = '/report';
                  });
              }
            }
          });
        }
      });
    }
  }
};
</script>
<style></style>
