main.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // import Vue from 'vue'
  2. // import VueRouter from 'vue-router'
  3. // Vue.use(VueRouter)
  4. // import ElementUI from 'element-ui'
  5. //全局修改默认配置,点击空白处不能关闭弹窗
  6. // Dialog.props.closeOnClickModal.default = false
  7. // Vue.use(ElementUI)
  8. // import Vuex from 'vuex'
  9. // Vue.use(Vuex)
  10. // import echarts from 'echarts'
  11. // import 'element-ui/lib/theme-chalk/index.css'
  12. import App from './App'
  13. import store from './vuex/store'
  14. // import routes from './routes'
  15. import http from './http'
  16. import port from './port'
  17. import $ from 'jquery'
  18. Vue.prototype.http = http
  19. Vue.prototype.port = port
  20. Vue.prototype.echarts = echarts
  21. import VueClipboard from 'vue-clipboard2'
  22. Vue.use(VueClipboard)
  23. import 'font-awesome/css/font-awesome.min.css'
  24. import './assets/iconfont/iconfont.css'
  25. import './assets/myfont/iconfont.css'
  26. // const router = new VueRouter({
  27. // routes
  28. // })
  29. import { staffRouter, manageRouter, fixedRouter,leaderRouter } from './routes'
  30. import router from './routes'
  31. import NProgress from 'nprogress'
  32. import 'nprogress/nprogress.css'
  33. var addRouFlag = false;
  34. router.beforeEach((to, from, next) => {
  35. NProgress.start();
  36. if (to.path == '/register') {
  37. next();
  38. return;
  39. }
  40. if(to.path != '/daily') {
  41. sessionStorage.removeItem("from")
  42. }
  43. if (to.path == '/login') {
  44. sessionStorage.removeItem('user');
  45. }
  46. let user = JSON.parse(sessionStorage.getItem('user'));
  47. if (!user && to.path != '/login') {
  48. next({ path: '/login' })
  49. } else {
  50. if (!addRouFlag) {
  51. if(user != null) {
  52. addRouFlag = true
  53. if(user.role == 0) {
  54. var getRoutes = baseRoleGetRouters(user.leader?leaderRouter:staffRouter, 1);
  55. global.antRouter = fixedRouter.concat(getRoutes);
  56. router.addRoutes(fixedRouter.concat(getRoutes));
  57. router.options.routes = fixedRouter.concat(getRoutes);
  58. router.push({ path: to.path })
  59. } else {
  60. var getRoutes = baseRoleGetRouters(manageRouter, 1);
  61. global.antRouter = fixedRouter.concat(getRoutes);
  62. router.addRoutes(fixedRouter.concat(getRoutes));
  63. router.options.routes = fixedRouter.concat(getRoutes);
  64. router.push({ path: to.path })
  65. }
  66. }
  67. }
  68. next()
  69. }
  70. })
  71. function baseRoleGetRouters(allRoutes, roles) {
  72. // allRoutes是动态路由表
  73. // roles是取到的用户角色,数组
  74. let rightRoutes = allRoutes.filter((route, index) => {
  75. if (true) {
  76. if (route.children && route.children.length) {
  77. route.children = baseRoleGetRouters(route.children, roles)
  78. }
  79. return true
  80. }
  81. return false
  82. })
  83. return rightRoutes
  84. }
  85. function hasPermission(route, roles) {
  86. if (route.meta && route.meta.roles) {
  87. return roles.some(role => route.meta.roles.indexOf(role) >= 0)
  88. } else {
  89. return true
  90. }
  91. }
  92. router.afterEach(() => {
  93. NProgress.done();
  94. // 百度统计
  95. setTimeout(()=>{
  96. var _hmt = _hmt || [];
  97. (function() {
  98. //每次执行前,先移除上次插入的代码
  99. document.getElementById('baidu_tj') && document.getElementById('baidu_tj').remove();
  100. var hm = document.createElement("script");
  101. hm.src = "https://hm.baidu.com/hm.js?b485cd24e5dcbe73d4dde5abf7dc8ae0";
  102. hm.id = "baidu_tj"
  103. var s = document.getElementsByTagName("script")[0];
  104. s.parentNode.insertBefore(hm, s);
  105. })();
  106. },0);
  107. })
  108. new Vue({
  109. router,
  110. store,
  111. render: h => h(App)
  112. }).$mount('#app')