<template>
  <div id="app">
    <keep-alive :include="include">
      <router-view></router-view>
    </keep-alive>
  </div>
</template>

<script>
import { getUserLogin } from '@/service/api/commonApi.js';
export default {
  name: 'App',
  data() {
    return {
      include: [],
      userId: ''
    };
  },
  methods: {
    // 保存用户id，串号标志
    saveUserId() {
      getUserLogin().then(res => {
        if (res.errorCode === 0) {
          this.userId = res.result && res.result.userId;
          const wxaLinkFlag = res.result && res.result.wxaLinkFlag;
          localStorage.setItem('wxaLinkFlag', wxaLinkFlag);
        }
      });
    },
    // 浏览器tab切换检查uid是否变化
    changeTab() {
      document.addEventListener('visibilitychange', () => {
        if (document.visibilityState === 'visible') {
          getUserLogin().then(res => {
            if (res.errorCode === 0) {
              const uId = res.result && res.result.userId;
              const enableAccessControl = res.result && res.result.enableAccessControl;
              localStorage.setItem('enableAccessControl', enableAccessControl);
              if (this.userId !== uId) {
                this.userId = uId;
                // 切换账号后，不缓存权限数据、菜单数据、面包屑数据
                window.notCache = true;
                // 串号
                this.$confirm('当前登录账号已经发生变化，如果您在其他页面已经登录另一个账号，请重新登录！', '登录账号变更提示', {
                  confirmButtonText: '重新登录',
                  cancelButtonText: '刷新页面',
                  type: 'error'
                })
                  .then(() => {
                    window.location.href = '/gic-web/#/login';
                  })
                  .catch(() => {
                    window.location.href = '/report';
                  });
              }
            }
          });
        }
      });
    }
  },
  created() {
    this.saveUserId();
    this.changeTab();
    this.$store.dispatch('getIsShowSelfData'); // 展示是否显示 '仅看本人' btn 和列表展示创建人
  }
};
</script>

<style lang="scss">
@import './assets/theme/index.css';
@import './assets/style/base/index.scss';
#app {
  height: 100%;
}
</style>
