main.js 4.0 KB

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