main.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 Terms from './components/pages/Terms.vue';
  12. import Privacy from './components/pages/Privacy.vue';
  13. import User from './components/User/Show.vue';
  14. import Settings from './components/User/Settings.vue';
  15. import Login from './components/Modals/Login.vue';
  16. Vue.use(VueRouter);
  17. let router = new VueRouter({ history: true });
  18. let _this = this;
  19. lofig.folder = '../config/default.json';
  20. lofig.get('serverDomain', function(res) {
  21. io.init(res);
  22. io.getSocket((socket) => {
  23. socket.on("ready", (status, role, username, userId) => {
  24. auth.data(status, role, username, userId);
  25. });
  26. });
  27. });
  28. document.onkeydown = event => {
  29. event = event || window.event;
  30. if (event.keyCode === 27) router.app.$dispatch('closeModal');
  31. };
  32. router.beforeEach(transition => {
  33. window.location.hash = '';
  34. if (window.stationInterval) {
  35. clearInterval(window.stationInterval);
  36. window.stationInterval = 0;
  37. }
  38. if (window.socket) {
  39. io.removeAllListeners();
  40. }
  41. io.clear();
  42. if (transition.to.loginRequired || transition.to.adminRequired) {
  43. auth.getStatus((authenticated, role) => {
  44. if (transition.to.loginRequired && !authenticated) transition.redirect('/login');
  45. else if (transition.to.adminRequired && role !== 'admin') transition.redirect('/');
  46. else transition.next();
  47. });
  48. } else {
  49. transition.next();
  50. }
  51. });
  52. router.map({
  53. '/': {
  54. component: Home
  55. },
  56. '*': {
  57. component: NotFound
  58. },
  59. '/terms': {
  60. component: Terms
  61. },
  62. '/privacy': {
  63. component: Privacy
  64. },
  65. '/news': {
  66. component: News
  67. },
  68. '/u/:username': {
  69. component: User
  70. },
  71. '/settings': {
  72. component: Settings,
  73. loginRequired: true
  74. },
  75. '/login': {
  76. component: Login
  77. },
  78. '/admin': {
  79. component: Admin,
  80. adminRequired: true
  81. },
  82. '/official/:id': {
  83. component: Station
  84. },
  85. '/community/:id': {
  86. component: Station
  87. }
  88. });
  89. router.start(App, 'body');