main.js 3.5 KB

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