/* 项目启动 */
import Vue from 'vue'
import App from './App.vue'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import VueRouter from 'vue-router';
import routes from './routes.js';
import axios from 'axios'
import qs from 'qs';
import utils from '@api/utils.js';
import Crumbs from '@components/common/Crumbs.vue';
import './assets/css/common.css'

Vue.config.productionTip = false;
Vue.prototype.axios = axios;
Vue.prototype.qs = qs;

Vue.use(VueRouter);
Vue.use(ElementUI);

const router = new VueRouter({
  routes
});

//全局注册组件
Vue.component('crumbs', Crumbs);

//全局设置，导航进入前验证是否登录，否则跳转到登录
router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {      //进入非登录/注册页时需要验证
    const tokenStr = localStorage.getItem('token');
    const tokenInfo = utils.isJsonStr(tokenStr) && JSON.parse(tokenStr);
    if (tokenInfo) {
      next();
    } else {
      next('/');
    }
  } else {
    next();
  }
});

new Vue({
  router,
  render: h => h(App)
}).$mount('#app');
