main.js 2.4 KB

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