index.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. ...nav,
  52. {
  53. path: "/login",
  54. component: () => import("../views/account/login.vue"),
  55. meta: { title: "login", NoHeader: true }
  56. },
  57. {
  58. path: "/register",
  59. component: () => import("../views/account/register.vue"),
  60. meta: { title: "register", NoHeader: true }
  61. },
  62. {
  63. path: "/findPassword",
  64. component: () => import("../views/account/findPassword.vue"),
  65. meta: { title: "findPassword", NoHeader: true }
  66. },
  67. {
  68. path: "/user/:index?",
  69. component: () => import("../views/account/user/index.vue"),
  70. props: true,
  71. meta: { title: "user", auth: true }
  72. },
  73. {
  74. path: "/editPassword",
  75. component: () => import("../views/account/user/editPassword.vue"),
  76. meta: { title: "editPassword", auth: true }
  77. },
  78. {
  79. path: "/shop/:sid",
  80. component: () => import("../views/shops/details/index.vue"),
  81. redirect: "/shop/:sid/products",
  82. props: true,
  83. meta: { NoHeader: true },
  84. children: [
  85. {
  86. path: "products",
  87. component: () => import("../views/shops/details/products.vue"),
  88. meta: { title: "shopDetails" }
  89. },
  90. {
  91. path: "honor",
  92. component: () => import("../views/shops/details/honor.vue"),
  93. meta: { title: "shopHonor" }
  94. },
  95. {
  96. path: "profile",
  97. component: () => import("../views/shops/details/profile.vue"),
  98. meta: { title: "shopProfile" }
  99. },
  100. {
  101. path: ":pid",
  102. props: true,
  103. component: () => import("../views/shops/details/product.vue"),
  104. meta: { title: "productDetails" }
  105. }
  106. ]
  107. },
  108. {
  109. path: "/shopEnter",
  110. component: () => import("../views/shops/enter/index.vue"),
  111. meta: { title: "shopEnter" }
  112. },
  113. {
  114. path: "/shopEnter/add",
  115. component: () => import("../views/shops/enter/add.vue"),
  116. meta: { title: "shopEnterAdd" }
  117. },
  118. {
  119. path: "/info/buy/:id",
  120. component: () => import("../views/info/details/buy.vue"),
  121. props: true,
  122. meta: { title: "infoBuy" }
  123. },
  124. {
  125. path: "/info/sell/:id",
  126. component: () => import("../views/info/details/sell.vue"),
  127. props: true,
  128. meta: { title: "infoSell" }
  129. },
  130. {
  131. path: "/info/add",
  132. component: () => import("../views/info/add.vue"),
  133. meta: { title: "infoAdd", auth: true }
  134. },
  135. {
  136. path: "/news/:index?",
  137. component: () => import("../views/news/index.vue"),
  138. props: true,
  139. meta: { title: "news" }
  140. },
  141. {
  142. path: "/newsDetail/:noticeId",
  143. component: () => import("../views/news/details.vue"),
  144. props: true,
  145. meta: { title: "newsDetail" }
  146. }
  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;