main.js 3.6 KB

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