main.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import Vue from 'vue';
  2. import VueRouter from 'vue-router';
  3. import App from './App.vue';
  4. import auth from './auth';
  5. import NotFound from './components/404.vue';
  6. import Home from './components/pages/Home.vue';
  7. import Station from './components/Station/Station.vue';
  8. import Admin from './components/pages/Admin.vue';
  9. import News from './components/pages/News.vue';
  10. import User from './components/User/Show.vue';
  11. import Settings from './components/User/Settings.vue';
  12. import Login from './components/Modals/Login.vue';
  13. Vue.use(VueRouter);
  14. let router = new VueRouter({ history: true });
  15. let _this = this;
  16. lofig.folder = '../config/default.json';
  17. lofig.get('serverDomain', function(res) {
  18. let socket = window.socket = io(res);
  19. socket.on("ready", (status, role, username, userId) => {
  20. auth.data(status, role, username, userId);
  21. });
  22. });
  23. router.beforeEach(transition => {
  24. if (window.stationInterval) {
  25. clearInterval(window.stationInterval);
  26. window.stationInterval = 0;
  27. }
  28. if (window.socket) {
  29. window.socket.removeAllListeners();
  30. }
  31. if (transition.to.loginRequired || transition.to.adminRequired) {
  32. auth.getStatus((authenticated, role) => {
  33. if (transition.to.loginRequired && !authenticated) transition.redirect('/login');
  34. else if (transition.to.adminRequired && role !== 'admin') transition.redirect('/');
  35. else transition.next();
  36. });
  37. } else {
  38. transition.next();
  39. }
  40. });
  41. router.map({
  42. '/': {
  43. component: Home
  44. },
  45. '*': {
  46. component: NotFound
  47. },
  48. '/news': {
  49. component: News
  50. },
  51. '/u/:username': {
  52. component: User
  53. },
  54. '/settings': {
  55. component: Settings,
  56. loginRequired: true
  57. },
  58. '/login': {
  59. component: Login
  60. },
  61. '/admin': {
  62. component: Admin,
  63. adminRequired: true
  64. },
  65. '/official/:id': {
  66. component: Station
  67. },
  68. '/community/:id': {
  69. component: Station
  70. }
  71. });
  72. router.start(App, 'body');