// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router';
// import axios from './http/http.js';
import Axios from 'axios';
// import '@babel/polyfill'
// import Vant from 'vant';
// Vue.use(Vant);
import Meta from 'vue-meta';
Vue.use(Meta)
import {Toast,Button,Field,Cell, CellGroup,Tabbar,TabbarItem, Icon,Uploader,Overlay,Form, Radio,RadioGroup,Checkbox,Dialog,CheckboxGroup,Step, Steps} from 'vant';
import 'vant/lib/index.css';
import './css/base.css';
Vue.prototype.$toast = Toast;
Vue.prototype.$axios = Axios;
import commonUtil from './script/commonUtil';
import Service from './script/api/service';

Vue.use(Toast).use(Field).use(Button).use(Cell).use(CellGroup).use(Tabbar).use(TabbarItem).use(Icon).use(Uploader).use(Overlay).use(Form).use(Radio).use(RadioGroup).use(Checkbox).use(Dialog).use(CheckboxGroup).use(Step).use(Steps);
Vue.config.productionTip = false
let apiUrl =process.env.baseUrl;
console.log(apiUrl);

Axios.defaults.baseURL = apiUrl;
Axios.interceptors.request.use(
  config => {
    Toast.loading({
      duration: 0,
      message: '加载中...',
      forbidClick: true,
    });
    config.headers.token = window.localStorage.getItem('token');
    return config;
  },
  err => {
    Toast('请求超时!');
    return Promise.reject(err);
  });
//axios统一错误处理
Axios.interceptors.response.use(data => {
  console.log(data);
  Toast.clear();
  if (data.data.code && data.data.code == 200) {
    // Message.success( data.data.messageToUser+ '111');
    // return;
  }
  // console.log(err)
  if (data.data.errno == 501) {
    Toast({ message: '服务端错误: ' + data.data.errmsg });
  }
  if (data.data.errno == 502) {
    Toast({ message: '服务端错误: ' + data.data.data + data.data.errmsg });
  }
  if (data.data.errno == 503) {
    Toast({ message: '服务器异常，请联系相关人员！' })
  }
  if (data.data.errno == 400) {
    Toast({ message: '权限不够' });
  }
  
  if (data.data.errno == 401) {
    // if (window.localStorage.getItem('token') == "") {
      Toast({ message: '身份过期，请重新登陆' });
       window.localStorage.removeItem('token');
      router.push('/qyb');
     
    // }

  }
  return data;
}, err => {
  Toast.clear();
  return Promise.resolve(err);
})
router.afterEach((to,from) => {
  console.log('changeview');
  console.log(to,from);
  window.scrollTo(0,0);

  // let qybUuid=sessionStorage.getItem('qybUuid');
  //  console.log(qybUuid);
  //  if(qybUuid==null){
  //    let newUuid=commonUtil.creadUuid();
  //    console.log(newUuid);
  //    sessionStorage.setItem('qybUuid',newUuid);
  //  }

  // let customerId=JSON.parse(localStorage.getItem("userInfo"))==null?'':(JSON.parse(localStorage.getItem("userInfo"))).id;
  // let customerPhone=JSON.parse(localStorage.getItem("userInfo"))==null?'':(JSON.parse(localStorage.getItem("userInfo"))).customerTel;
  // let params={
  //     accessUuid:sessionStorage.getItem('qybUuid'),
  //     customerId: customerId, 
  //     customerPhone:customerPhone, 
  //     opType:'2',
  //     pagePath:to.path,
  //     pagePathName:to.meta.title
  //   };
  //   Service.$axiosInsertAccessLog(params).then(res => {
  //     if (res.data.errno == 0) {
  //      console.log('log success');
  //     } else {
  //       console.log(res.data.errmsg);
  //     }
  //   });

});


/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
