main.js 3.4 KB

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