main.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. if (transition.to.officialRequired) {
  56. io.getSocket(socket => {
  57. socket.emit('stations.findByName', transition.to.params.id, res => {
  58. if (res.status === 'success') {
  59. if (res.data.type === 'community') transition.redirect(`/community/${transition.to.params.id}`);
  60. else transition.next();
  61. }
  62. });
  63. });
  64. }
  65. if (transition.to.communityRequired) {
  66. io.getSocket(socket => {
  67. socket.emit('stations.findByName', transition.to.params.id, res => {
  68. if (res.status === 'success') {
  69. if (res.data.type === 'official') transition.redirect(`/official/${transition.to.params.id}`);
  70. else transition.next();
  71. }
  72. });
  73. });
  74. }
  75. });
  76. router.map({
  77. '/': {
  78. component: Home
  79. },
  80. '*': {
  81. component: NotFound
  82. },
  83. '404': {
  84. component: NotFound
  85. },
  86. '/terms': {
  87. component: Terms
  88. },
  89. '/privacy': {
  90. component: Privacy
  91. },
  92. '/team': {
  93. component: Team
  94. },
  95. '/news': {
  96. component: News
  97. },
  98. '/about': {
  99. component: About
  100. },
  101. '/u/:username': {
  102. component: User
  103. },
  104. '/settings': {
  105. component: Settings,
  106. loginRequired: true
  107. },
  108. '/reset_password': {
  109. component: ResetPassword
  110. },
  111. '/login': {
  112. component: Login
  113. },
  114. '/register': {
  115. component: Register
  116. },
  117. '/admin': {
  118. component: Admin,
  119. adminRequired: true
  120. },
  121. '/admin/:page': {
  122. component: Admin,
  123. adminRequired: true
  124. },
  125. '/official/:id': {
  126. component: Station,
  127. officialRequired: true
  128. },
  129. '/:id': {
  130. component: Station,
  131. officialRequired: true
  132. },
  133. '/community/:id': {
  134. component: Station,
  135. communityRequired: true
  136. }
  137. });
  138. router.start(App, 'body');