main.js 3.5 KB

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