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

<script>
import request from './service/request.js';
export default {
  name: 'App',
  data () {
    return {
      userId: '',
    };
  },
  created () {
    this.getUserId();
    this.changeTab();
  },
  methods: {
    // 保存用户id，串号标志
    getUserId () {
      request.get('/api-auth/get-login-user-info').then(res => {
        if (res.data.errorCode === 0) {
          this.userId = res.data.result.userId;
          const limitCodeType = res.data.result.limitCodeList;
          if (limitCodeType) {
            const jifenquanxian = limitCodeType.indexOf('integralCashPurchase') != -1;
            localStorage.setItem('limitCodeType', jifenquanxian);
          }
        } 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 (this.userId !== useId) {
                this.userId = useId;
                // 切换账号后，不缓存权限数据、菜单数据、面包屑数据
                window.notCache = true;
                // 串号
                this.$confirm('当前登录账号已经发生变化，如果您在其他页面已经登录另一个账号，请重新登录！', '登录账号变更提示', {
                  confirmButtonText: '重新登录',
                  cancelButtonText: '刷新页面',
                  type: 'error',
                })
                  .then(() => {
                    window.location.href = '/gic-web/#/login';
                  })
                  .catch(() => {
                    window.location.href = '/report';
                  });
              }
            } 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);
        }
      });
    },
  },
};
</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>
