fe-drone-ci/src/main.js

110 lines
3.8 KiB
JavaScript
Raw Normal View History

2017-04-18 07:09:13 +00:00
// 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 store from './store';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-default/index.css';
2017-06-30 03:31:21 +00:00
import 'assets/custom-theme/index.css'; // 换肤版本element-ui css
2017-05-16 02:44:18 +00:00
import NProgress from 'nprogress'; // Progress 进度条
import 'nprogress/nprogress.css';// Progress 进度条 样式
import 'normalize.css/normalize.css';// normalize.css 样式格式化
import 'assets/iconfont/iconfont'; // iconfont 具体图标见https://github.com/PanJiaChen/vue-element-admin/wiki
import * as filters from './filters'; // 全局vue filter
import Multiselect from 'vue-multiselect';// 使用的一个多选框组件element-ui的select不能满足所有需求
import 'vue-multiselect/dist/vue-multiselect.min.css';// 多选框组件css
import Sticky from 'components/Sticky'; // 粘性header组件
2017-07-05 03:42:29 +00:00
import IconSvg from 'components/Icon-svg';// svg 组件
2017-05-16 02:44:18 +00:00
import vueWaves from './directive/waves';// 水波纹指令
import errLog from 'store/errLog';// error log组件
import './mock/index.js'; // 该项目所有请求使用mockjs模拟
2017-07-20 05:26:09 +00:00
import { getToken } from 'utils/auth';
2017-04-18 07:09:13 +00:00
// register globally
Vue.component('multiselect', Multiselect);
Vue.component('Sticky', Sticky);
2017-07-05 03:42:29 +00:00
Vue.component('icon-svg', IconSvg)
2017-04-18 07:09:13 +00:00
Vue.use(ElementUI);
Vue.use(vueWaves);
// register global utility filters.
Object.keys(filters).forEach(key => {
Vue.filter(key, filters[key])
});
2017-05-11 09:48:20 +00:00
// permissiom judge
2017-04-18 07:09:13 +00:00
function hasPermission(roles, permissionRoles) {
2017-05-11 09:48:20 +00:00
if (roles.indexOf('admin') >= 0) return true; // admin权限 直接通过
2017-05-17 08:26:33 +00:00
if (!permissionRoles) return true;
2017-04-18 07:09:13 +00:00
return roles.some(role => permissionRoles.indexOf(role) >= 0)
}
2017-05-11 09:48:20 +00:00
2017-04-18 07:09:13 +00:00
// register global progress.
const whiteList = ['/login', '/authredirect', '/reset', '/sendpwd'];// 不重定向白名单
router.beforeEach((to, from, next) => {
2017-05-11 09:48:20 +00:00
NProgress.start(); // 开启Progress
2017-07-20 05:26:09 +00:00
if (getToken()) { // 判断是否有token
2017-04-18 07:09:13 +00:00
if (to.path === '/login') {
next({ path: '/' });
} else {
2017-05-17 09:59:13 +00:00
if (store.getters.roles.length === 0) { // 判断当前用户是否已拉取完user_info信息
store.dispatch('GetInfo').then(res => { // 拉取user_info
2017-05-17 08:26:33 +00:00
const roles = res.data.role;
store.dispatch('GenerateRoutes', { roles }).then(() => { // 生成可访问的路由表
router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
2017-07-04 08:11:53 +00:00
next({ ...to }); // hack方法 确保addRoutes已完成
2017-05-17 08:26:33 +00:00
})
2017-07-25 07:56:14 +00:00
}).catch(() => {
store.dispatch('FedLogOut').then(() => {
next({ path: '/login' });
})
2017-07-03 03:00:36 +00:00
})
2017-05-17 08:26:33 +00:00
} else {
// 没有动态改变权限的需求可直接next() 删除下方权限判断 ↓
if (hasPermission(store.getters.roles, to.meta.role)) {
next();//
2017-05-16 09:31:48 +00:00
} else {
2017-05-17 08:26:33 +00:00
next({ path: '/401', query: { noGoBack: true } });
2017-05-16 09:31:48 +00:00
}
2017-05-17 08:26:33 +00:00
// 可删 ↑
2017-04-18 07:09:13 +00:00
}
}
} else {
2017-05-16 10:20:47 +00:00
if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入
2017-04-18 07:09:13 +00:00
next()
} else {
2017-05-11 09:48:20 +00:00
next('/login'); // 否则全部重定向到登录页
NProgress.done(); // 在hash模式下 改变手动改变hash 重定向回来 不会触发afterEach 暂时hack方案 pshistory模式下无问题可删除该行
2017-04-18 07:09:13 +00:00
}
}
});
router.afterEach(() => {
2017-05-11 09:48:20 +00:00
NProgress.done(); // 结束Progress
2017-04-18 07:09:13 +00:00
});
2017-07-06 09:56:17 +00:00
Vue.config.productionTip = false;
2017-04-18 07:09:13 +00:00
// 生产环境错误日志
2017-07-27 05:55:06 +00:00
if (process.env.NODE_ENV === 'production') {
2017-04-18 07:09:13 +00:00
Vue.config.errorHandler = function(err, vm) {
console.log(err, window.location.href);
errLog.pushLog({
err,
url: window.location.href,
vm
})
};
}
new Vue({
2017-06-26 03:21:53 +00:00
el: '#app',
2017-04-18 07:09:13 +00:00
router,
store,
2017-06-26 03:21:53 +00:00
template: '<App/>',
components: { App }
})
2017-04-18 07:09:13 +00:00