main.js 3.4 KB

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