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

<script>
import { getUserLogin, logout } from '@/service/api/commonApi.js';
export default {
  name: 'App',
  data() {
    return {
      include: []
    };
  },
  methods: {
    // 保存用户id，串号标志
    saveUserId() {
      getUserLogin().then(res => {
        if (res.errorCode === 0) {
          const uId = res.result && res.result.userId;
          localStorage.setItem('m_uId', uId);
        }
      });
    },
    // 浏览器tab切换检查uid是否变化
    changeTab() {
      document.addEventListener('visibilitychange', () => {
        if (document.visibilityState === 'visible') {
          getUserLogin().then(res => {
            if (res.errorCode === 0) {
              const uId = res.result && res.result.userId;
              if (localStorage.getItem('m_uId') !== uId) {
                // 串号
                this.$confirm('当前登录账号已经发生变化，如果您在其他页面已经登录另一个账号，请退出重新登录！', '登录账号变更提示', {
                  type: 'error',
                  closeOnClickModal: false,
                  confirmButtonText: '退出重新登录',
                  cancelButtonText: '刷新页面',
                  showClose: false
                })
                  .then(() => {
                    logout(); // 登出
                    // if (process.env.NODE_ENV === 'production') { // eslint-disable-line
                    window.location.href = '/gic-web/login';
                    // }
                  })
                  .catch(() => {
                    window.location.reload();
                  });
              } else {
                localStorage.setItem('m_uId', uId);
              }
            }
          });
        }
      });
    }
  },
  created() {
    this.saveUserId();
    this.changeTab();
  }
};
</script>

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