main.js 3.4 KB

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