<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://www.gicdev.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;
          const isSuperAdmin = res.data.result && res.data.result.superAdmin;
          // 部门权限,1可见本人，2可见部门，3可见所有
          const departAuth = res.data.result && res.data.result.departAuth;
          const enableAccessControl = res.data.result && res.data.result.enableAccessControl;
          this.$store.commit('changeEnableAccess', enableAccessControl);
          this.$store.commit('changeSuperAdmin', isSuperAdmin);
          this.$store.commit('changeDepartAuth', departAuth);
          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);
                // 切换账号后，不缓存权限数据、菜单数据、面包屑数据
                window.notCache = true;
                this.$confirm('当前登录账号已经发生变化，如果您在其他页面已经登录另一个账号，请重新登录！', '登录账号变更提示', {
                  confirmButtonText: '重新登录',
                  cancelButtonText: '刷新页面',
                  type: 'error'
                })
                  .then(() => {
                    window.location.href = '/gic-web/#/login';
                  })
                  .catch(() => {
                    window.location.href = '/report';
                  });
              }
            }
          });
        }
      });
    }
  }
};
</script>
<style></style>
