main.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import Vue from 'vue';
  2. import VueRouter from 'vue-router';
  3. import App from './App.vue';
  4. import auth from './auth';
  5. import io from './io';
  6. import NotFound from './components/404.vue';
  7. import Home from './components/pages/Home.vue';
  8. import Station from './components/Station/Station.vue';
  9. import Admin from './components/pages/Admin.vue';
  10. import News from './components/pages/News.vue';
  11. import About from './components/pages/About.vue';
  12. import Terms from './components/pages/Terms.vue';
  13. import Privacy from './components/pages/Privacy.vue';
  14. import Team from './components/pages/Team.vue';
  15. import User from './components/User/Show.vue';
  16. import Settings from './components/User/Settings.vue';
  17. import ResetPassword from './components/User/ResetPassword.vue';
  18. import Login from './components/Modals/Login.vue';
  19. import Register from './components/Modals/Register.vue';
  20. Vue.use(VueRouter);
  21. let router = new VueRouter({ history: true });
  22. let _this = this;
  23. lofig.folder = '../config/default.json';
  24. lofig.get('serverDomain', function(res) {
  25. io.init(res);
  26. io.getSocket((socket) => {
  27. socket.on("ready", (status, role, username, userId) => {
  28. auth.data(status, role, username, userId);
  29. });
  30. });
  31. });
  32. document.onkeydown = event => {
  33. event = event || window.event;
  34. if (event.keyCode === 27) router.app.$dispatch('closeModal');
  35. };
  36. router.beforeEach(transition => {
  37. window.location.hash = '';
  38. if (window.stationInterval) {
  39. clearInterval(window.stationInterval);
  40. window.stationInterval = 0;
  41. }
  42. if (window.socket) {
  43. io.removeAllListeners();
  44. }
  45. io.clear();
  46. if (transition.to.loginRequired || transition.to.adminRequired) {
  47. auth.getStatus((authenticated, role) => {
  48. if (transition.to.loginRequired && !authenticated) transition.redirect('/login');
  49. else if (transition.to.adminRequired && role !== 'admin') transition.redirect('/');
  50. else transition.next();
  51. });
  52. } else {
  53. transition.next();
  54. }
  55. });
  56. router.map({
  57. '/': {
  58. component: Home
  59. },
  60. '*': {
  61. component: NotFound
  62. },
  63. '404': {
  64. component: NotFound
  65. },
  66. '/terms': {
  67. component: Terms
  68. },
  69. '/privacy': {
  70. component: Privacy
  71. },
  72. '/team': {
  73. component: Team
  74. },
  75. '/news': {
  76. component: News
  77. },
  78. '/about': {
  79. component: About
  80. },
  81. '/u/:username': {
  82. component: User
  83. },
  84. '/settings': {
  85. component: Settings,
  86. loginRequired: true
  87. },
  88. '/reset_password': {
  89. component: ResetPassword
  90. },
  91. '/login': {
  92. component: Login
  93. },
  94. '/register': {
  95. component: Register
  96. },
  97. '/admin': {
  98. component: Admin,
  99. adminRequired: true
  100. },
  101. '/admin/:page': {
  102. component: Admin,
  103. adminRequired: true
  104. },
  105. '/official/:id': {
  106. component: Station
  107. },
  108. '/:id': {
  109. component: Station
  110. },
  111. '/community/:id': {
  112. component: Station
  113. }
  114. });
  115. router.start(App, 'body');