<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: {
    _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.$confirm('出现窜号情况，请首先核对好是哪个商户再进行操作！', '提示', {
                  type: 'error'
                }).then(() => {
                  this.$store.commit('changeUniqueId', uId);
                  window.location.reload();
                }).catch(() => {
                  this.$store.commit('changeUniqueId', uId);
                  window.location.reload();
                });
              }
            }
          });
        }
      });
    }
  }
};
</script>
<style></style>
