index.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import Vue from "vue";
  2. import VueRouter, { RouteConfig } from "vue-router";
  3. import layout from "@/layout/index.vue";
  4. import user from "@/store/modules/user";
  5. import { Loading } from "element-ui";
  6. import { Message } from "@/utils/message";
  7. import { login } from "@/utils/cookies";
  8. import { setPageTitle } from "@/utils";
  9. Vue.use(VueRouter);
  10. export const nav: RouteConfig[] = [
  11. {
  12. path: "/home",
  13. component: () => import("../views/home/index.vue"),
  14. meta: { title: "home" }
  15. },
  16. {
  17. path: "/mall/:q?",
  18. props: true,
  19. component: () => import("../views/mall/index.vue"),
  20. meta: { title: "mall" }
  21. },
  22. {
  23. path: "/shops/:kw?",
  24. props: true,
  25. component: () => import("../views/shops/index.vue"),
  26. meta: { title: "shops" }
  27. },
  28. {
  29. path: "/info/:index?",
  30. component: () => import("../views/info/index.vue"),
  31. props: true,
  32. meta: { title: "info" }
  33. }
  34. ];
  35. const router = new VueRouter({
  36. mode: process.env.NODE_ENV !== "development" ? "hash" : "history",
  37. base: process.env.BASE_URL,
  38. scrollBehavior(to, from, savedPosition) {
  39. if (savedPosition) {
  40. return savedPosition;
  41. } else {
  42. return { x: 0, y: 0 };
  43. }
  44. },
  45. routes: [
  46. {
  47. path: "/",
  48. component: layout,
  49. redirect: "/home",
  50. children: [
  51. {
  52. path: "/login",
  53. component: () => import("../views/account/login.vue"),
  54. meta: { title: "login", NoHeader: true }
  55. },
  56. {
  57. path: "/register",
  58. component: () => import("../views/account/register.vue"),
  59. meta: { title: "register", NoHeader: true }
  60. },
  61. {
  62. path: "/findPassword",
  63. component: () => import("../views/account/findPassword.vue"),
  64. meta: { title: "findPassword", NoHeader: true }
  65. },
  66. {
  67. path: "/user/:index?",
  68. component: () => import("../views/account/user/index.vue"),
  69. props: true,
  70. meta: { title: "user", auth: true }
  71. },
  72. {
  73. path: "/editPassword",
  74. component: () => import("../views/account/user/editPassword.vue"),
  75. meta: { title: "editPassword", auth: true }
  76. },
  77. {
  78. path: "/shop/:sid",
  79. component: () => import("../views/shops/details/index.vue"),
  80. redirect: "/shop/:sid/products",
  81. props: true,
  82. meta: { NoHeader: true },
  83. children: [
  84. {
  85. path: "products",
  86. component: () => import("../views/shops/details/products.vue"),
  87. meta: { title: "shopDetails" }
  88. },
  89. {
  90. path: "honor",
  91. component: () => import("../views/shops/details/honor.vue"),
  92. meta: { title: "shopHonor" }
  93. },
  94. {
  95. path: "profile",
  96. component: () => import("../views/shops/details/profile.vue"),
  97. meta: { title: "shopProfile" }
  98. },
  99. {
  100. path: ":pid",
  101. props: true,
  102. component: () => import("../views/shops/details/product.vue"),
  103. meta: { title: "productDetails" }
  104. }
  105. ]
  106. },
  107. {
  108. path: "/shopEnter",
  109. component: () => import("../views/shops/enter/index.vue"),
  110. meta: { title: "shopEnter" }
  111. },
  112. {
  113. path: "/shopEnter/add",
  114. component: () => import("../views/shops/enter/add.vue"),
  115. meta: { title: "shopEnterAdd" }
  116. },
  117. {
  118. path: "/info/buy/:id",
  119. component: () => import("../views/info/details/buy.vue"),
  120. props: true,
  121. meta: { title: "infoBuy" }
  122. },
  123. {
  124. path: "/info/sell/:id",
  125. component: () => import("../views/info/details/sell.vue"),
  126. props: true,
  127. meta: { title: "infoSell" }
  128. },
  129. {
  130. path: "/info/add",
  131. component: () => import("../views/info/add.vue"),
  132. meta: { title: "infoAdd", auth: true }
  133. },
  134. {
  135. path: "/news/:index?",
  136. component: () => import("../views/news/index.vue"),
  137. props: true,
  138. meta: { title: "news" }
  139. },
  140. {
  141. path: "/newsDetail/:noticeId",
  142. component: () => import("../views/news/details.vue"),
  143. props: true,
  144. meta: { title: "newsDetail" }
  145. },
  146. ...nav
  147. ]
  148. },
  149. {
  150. path: "/error/:code",
  151. component: () => import("../views/errorPage/index.vue")
  152. }
  153. ]
  154. });
  155. let loading: any;
  156. router.beforeEach(async (to, from, next) => {
  157. loading = Loading.service({
  158. fullscreen: true,
  159. text: "跳转中..."
  160. });
  161. setTimeout(() => loading.close(), 3e3);
  162. if (user.Token && !user.UserInfo) {
  163. await user.getUserInfo();
  164. }
  165. if (!to.matched.length && process.env.NODE_ENV !== "development")
  166. return next({
  167. path: `/error/404`,
  168. replace: true
  169. });
  170. if (to.meta.auth && !user.UserInfo) return next("/login");
  171. setPageTitle(to.meta.title);
  172. next();
  173. });
  174. router.afterEach(() => loading.close());
  175. export default router;