<template>
  <div id="app">
    <router-view> </router-view>
  </div>
</template>

<script>
import request from './service/request.js';
export default {
  name: 'App',
  data() {
    return {};
  },
  methods: {
    // 保存用户id，串号标志
    getUserId() {
      request.get('/api-auth/get-login-user-info').then(res => {
        if (res.data.errorCode === 0) {
          const useId = res.data.result.userId;
          localStorage.setItem('useId', useId);
        } else {
          this.$message.error(res.data.message);
        }
      });
    },
    // 浏览器tab切换检查uid是否变化
    changeTab() {
      document.addEventListener('visibilitychange', () => {
        if (document.visibilityState === 'visible') {
          request.get('/api-auth/get-login-user-info').then(res => {
            if (res.data.errorCode === 0) {
              const useId = res.data.result && res.data.result.userId;
              if (localStorage.getItem('useId') !== useId) {
                // 串号
                this.$confirm('当前登录账号已经发生变化，如果您在其他页面已经登录另一个账号，请退出重新登录！', '提示', {
                  type: 'error',
                  confirmButtonText: '退出重新登录',
                  cancelButtonText: '刷新页面'
                })
                  .then(() => {
                    localStorage.setItem('useId', useId);
                    // window.location.reload();
                    this.doLogout();
                  })
                  .catch(() => {
                    // window.location.reload();
                    this.doLogout();
                  });
              } else {
                localStorage.setItem('useId', useId);
              }
            } else {
              this.$message.error(res.data.message);
            }
          });
        }
      });
    },
    // 退出登录
    doLogout() {
      request.get('/api-auth/do-logout').then(res => {
        if (res.data.errorCode === 0) {
          this.$message.success('退出成功');
          // 跳转登录页,商户.集团判断.// 暂时不判断登录入口,20180629
          window.location.href = window.location.origin + '/gic-web/#/';
        } else {
          this.$message.error(res.data.message);
        }
      });
    }
  },
  created() {
    this.getUserId();
    this.changeTab();
  }
};
</script>

<style>
#app {
  background-color: #f0f2f5;
}
/*面包屑前面两个加粗*/
.mall-crumbs .el-breadcrumb__inner {
  height: 14px;
  font-size: 14px;
  font-family: PingFangSC-Medium;
  font-weight: bold;
  color: #303133;
}
</style>
