import store from '@/store/index';
const _ = require('lodash');
const jwt = require('jsonwebtoken');
let $dev_env = process.env.NODE_ENV === 'development';

export default (router) => {
  router.beforeEach((to, from, next) => {
    if (to.path.includes('dev')) {
      if (!$dev_env) next('/');
    }
    // 检查是不是登陆,登陆页面放过
    if (to.path === '/login') {
      next();
      return;
    }
    const user = store.state.user;
    if (!_.get(user, '_id')) {
      const str = sessionStorage.getItem('user');
      if (!str) next('/login');
      const obj = jwt.decode(str);
      store.commit('setUser', obj, { root: true });
    }
    next();
  });
};
