main.js 3.8 KB

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