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

<script>
  import '@less/base.less';
  import axios from 'axios';
  import utils from '@api/utils.js';
  import api from './api/user.js';
  import icss from './api/icss.js';
  document.title='朗通云平台';         //修改标签页标题
  export default {
    name: 'app',
    created() {
      this.getUserEnumsData();
      this.getDiagbotManEnumsData();
      this.getIcssEnumsData();
      this.getKnowledgeEnums();
      //请求拦截器，添加token
      axios.interceptors.request.use((req) => {
        const tokenStr = localStorage.getItem('token');
        if (tokenStr) {
          const tokenInfo = utils.isJsonStr(tokenStr) && JSON.parse(tokenStr);
          req.headers.Authorization = `Bearer ${tokenInfo.accessToken}`;
          return req;
        } else {
          return req;
        }
      }, (error) => {
        return Promise.reject(error);
      });
      //请求响应拦截，401则调到登录页
      axios.interceptors.response.use(
        response => {
          const code = response.data.code;
          if(code ==="10020011"){       //token过期,清除token信息并跳转到登录页面
            localStorage.removeItem('token');
            this.logOut();
            return;
          }
          return response;
        },
        error => {
          if (error.response) {
            switch (error.response.status) {
              case 401:
                // 返回 401 清除token信息并跳转到登录页面
                localStorage.removeItem('token');
                this.logOut();
                return;
              case 500:
                // 返回 500
                this.message({
                  message:'该服务正在升级，请稍后再试',
                  type:'warning'
                });
                return;
            }
          }
          return Promise.reject(error)   // 返回接口返回的错误信息
        });
      
    },
    methods: {
      logOut(){
        this.$alert('账号异常，请重新登录', '提示', {
          confirmButtonText: '确定',
          callback: action => {
            window.location.href='/';
          }
        });
      },
      getUserEnumsData() {
        api.getUserEnumsData().then((res) => {
          if (res.data.code == '0') {
            const data = res.data.data;
            localStorage.setItem('enumsData', JSON.stringify(data));
          }
        });
      },
      getDiagbotManEnumsData(){
        api.getDiagbotManEnumsData().then((res) => {
          if (res.data.code == '0') {
            const data = res.data.data;
            localStorage.setItem('productEnumsData', JSON.stringify(data));
          }
        });
      },
      getIcssEnumsData(){
        icss.getIcssEnumsData().then((res) => {
          if (res.data.code == '0') {
            const data = res.data.data;
            localStorage.setItem('icssEnumsData', JSON.stringify(data));
          }
        });
      },
      getKnowledgeEnums(){
        icss.getKnowledgeEnums().then((res) => {
          if (res.data.code == '0') {
            const data = res.data.data;
            localStorage.setItem('knowledgeEnumsData', JSON.stringify(data));
          }
        });
      }
    }
  }
</script>

<style>
    #app {
        width: 100%;
        height: 100%;
    }
</style>
