main.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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({
  22. history: true,
  23. suppressTransitionError: true
  24. });
  25. let _this = this;
  26. lofig.folder = '../config/default.json';
  27. lofig.get('serverDomain', function(res) {
  28. io.init(res);
  29. io.getSocket((socket) => {
  30. socket.on("ready", (status, role, username, userId) => {
  31. auth.data(status, role, username, userId);
  32. });
  33. socket.on("keep.me.isBanned", () => {
  34. console.log("BANNED");
  35. auth.setBanned();
  36. });
  37. });
  38. });
  39. document.onkeydown = event => {
  40. event = event || window.event;
  41. if (event.keyCode === 27) router.app.$dispatch('closeModal');
  42. };
  43. router.beforeEach(transition => {
  44. window.location.hash = '';
  45. //
  46. if (window.stationInterval) {
  47. clearInterval(window.stationInterval);
  48. window.stationInterval = 0;
  49. }
  50. if (window.socket) io.removeAllListeners();
  51. io.clear();
  52. if (transition.to.loginRequired || transition.to.adminRequired) {
  53. auth.getStatus((authenticated, role) => {
  54. if (transition.to.loginRequired && !authenticated) transition.redirect('/login');
  55. else if (transition.to.adminRequired && role !== 'admin') transition.redirect('/');
  56. else transition.next();
  57. });
  58. } else transition.next();
  59. if (transition.to.officialRequired) {
  60. io.getSocket(socket => {
  61. socket.emit('stations.findByName', transition.to.params.id, res => {
  62. if (res.status === 'success') {
  63. if (res.data.type === 'community') transition.redirect(`/community/${transition.to.params.id}`);
  64. else transition.next();
  65. }
  66. });
  67. });
  68. }
  69. if (transition.to.communityRequired) {
  70. io.getSocket(socket => {
  71. socket.emit('stations.findByName', transition.to.params.id, res => {
  72. if (res.status === 'success') {
  73. if (res.data.type === 'official') transition.redirect(`/official/${transition.to.params.id}`);
  74. else transition.next();
  75. }
  76. });
  77. });
  78. }
  79. });
  80. router.afterEach((data) => {
  81. ga('set', 'page', data.to.path);
  82. ga('send', 'pageview');
  83. });
  84. router.map({
  85. '/': {
  86. component: Home
  87. },
  88. '*': {
  89. component: NotFound
  90. },
  91. '404': {
  92. component: NotFound
  93. },
  94. '/terms': {
  95. component: Terms
  96. },
  97. '/privacy': {
  98. component: Privacy
  99. },
  100. '/team': {
  101. component: Team
  102. },
  103. '/news': {
  104. component: News
  105. },
  106. '/about': {
  107. component: About
  108. },
  109. '/u/:username': {
  110. component: User
  111. },
  112. '/settings': {
  113. component: Settings,
  114. loginRequired: true
  115. },
  116. '/reset_password': {
  117. component: ResetPassword
  118. },
  119. '/login': {
  120. component: Login
  121. },
  122. '/register': {
  123. component: Register
  124. },
  125. '/admin': {
  126. component: Admin,
  127. adminRequired: true
  128. },
  129. '/admin/:page': {
  130. component: Admin,
  131. adminRequired: true
  132. },
  133. '/official/:id': {
  134. component: Station,
  135. officialRequired: true
  136. },
  137. '/:id': {
  138. component: Station,
  139. officialRequired: true
  140. },
  141. '/community/:id': {
  142. component: Station,
  143. communityRequired: true
  144. }
  145. });
  146. router.start(App, 'body');