main.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import Vue from 'vue';
  2. import VueRouter from 'vue-router';
  3. import App from './App.vue';
  4. import auth from './auth';
  5. import NotFound from './components/404.vue';
  6. import Home from './components/pages/Home.vue';
  7. import Station from './components/Station/Station.vue';
  8. import Admin from './components/pages/Admin.vue';
  9. import News from './components/pages/News.vue';
  10. import User from './components/User/Show.vue';
  11. import Settings from './components/User/Settings.vue';
  12. import Login from './components/Modals/Login.vue';
  13. Vue.use(VueRouter);
  14. let router = new VueRouter({ history: true });
  15. let _this = this;
  16. lofig.folder = '../config/default.json';
  17. lofig.get('serverDomain', function(res) {
  18. let socket = window.socket = io(res);
  19. socket.on("ready", (status, role, username, userId) => {
  20. auth.data(status, role, username, userId);
  21. });
  22. setInterval(() => {
  23. if (!socket.connected) {
  24. window.socketConnected = false;
  25. router.app.$dispatch("handleSocketConnection");
  26. } else if (!window.socketConnected && socket.connected) {
  27. window.socketConnected = true;
  28. router.app.$dispatch("handleSocketConnection");
  29. }
  30. }, 10000);
  31. });
  32. $(document).keydown(function(e) {
  33. if (e.which === 27) {
  34. router.app.$dispatch("closeModal");
  35. }
  36. });
  37. router.beforeEach(transition => {
  38. if (window.stationInterval) {
  39. clearInterval(window.stationInterval);
  40. window.stationInterval = 0;
  41. }
  42. if (window.socket) {
  43. window.socket.removeAllListeners();
  44. }
  45. if (transition.to.loginRequired || transition.to.adminRequired) {
  46. auth.getStatus((authenticated, role) => {
  47. if (transition.to.loginRequired && !authenticated) transition.redirect('/login');
  48. else if (transition.to.adminRequired && role !== 'admin') transition.redirect('/');
  49. else transition.next();
  50. });
  51. } else {
  52. transition.next();
  53. }
  54. });
  55. router.map({
  56. '/': {
  57. component: Home
  58. },
  59. '*': {
  60. component: NotFound
  61. },
  62. '/news': {
  63. component: News
  64. },
  65. '/u/:username': {
  66. component: User
  67. },
  68. '/settings': {
  69. component: Settings,
  70. loginRequired: true
  71. },
  72. '/login': {
  73. component: Login
  74. },
  75. '/admin': {
  76. component: Admin,
  77. adminRequired: true
  78. },
  79. '/official/:id': {
  80. component: Station
  81. },
  82. '/community/:id': {
  83. component: Station
  84. }
  85. });
  86. router.start(App, 'body');