main.js 4.2 KB

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