index.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import {
  2. createRouter,
  3. createWebHistory,
  4. type RouteRecordRaw,
  5. } from "vue-router";
  6. import Home from "@/views/index.vue";
  7. import Management from "@/views/management.vue";
  8. import Command from "@/views/command.vue";
  9. import ChatBI from "@/views/chatbi.vue";
  10. import Login from "@/views/login.vue";
  11. import { getAccessToken } from "@utils/auth";
  12. import { socialLogin } from "@/api/user";
  13. import * as authUtil from "@/utils/auth";
  14. import { isRelogin } from "@/config/axios/service";
  15. import { useUserStoreWithOut } from "@/stores/useUserStore";
  16. const routes: RouteRecordRaw[] = [
  17. {
  18. path: "/",
  19. name: "Home",
  20. component: Home,
  21. },
  22. {
  23. path: "/login",
  24. name: "Login",
  25. component: Login,
  26. meta: {
  27. title: "DeepOil 智慧经营平台 | 登录",
  28. },
  29. },
  30. {
  31. path: "/management",
  32. name: "Management",
  33. component: Management,
  34. meta: {
  35. title: "DeepOil 智慧经营平台 | 数字运营平台",
  36. },
  37. },
  38. {
  39. path: "/command",
  40. name: "Command",
  41. component: Command,
  42. meta: {
  43. title: "DeepOil 智慧经营平台 | 智慧指挥平台",
  44. },
  45. },
  46. {
  47. path: "/chatbi",
  48. name: "ChatBI",
  49. component: ChatBI,
  50. meta: {
  51. title: "DeepOil 智慧经营平台 | Chat BI平台",
  52. },
  53. },
  54. ];
  55. const router = createRouter({
  56. history: createWebHistory(import.meta.env.BASE_URL),
  57. routes,
  58. scrollBehavior(to, from, savePosition) {
  59. // 始终滚动到顶部
  60. return { left: 0, top: 0 };
  61. },
  62. });
  63. export const resetRouter = (): void => {
  64. const resetWhiteNameList = ["Redirect", "Login", "NoFind", "Root"];
  65. router.getRoutes().forEach((route) => {
  66. const { name } = route;
  67. if (name && !resetWhiteNameList.includes(name as string)) {
  68. router.hasRoute(name) && router.removeRoute(name);
  69. }
  70. });
  71. };
  72. const parseURL = (
  73. url: string | null | undefined,
  74. ): { basePath: string; paramsObject: { [key: string]: string } } => {
  75. // 如果输入为 null 或 undefined,返回空字符串和空对象
  76. if (url == null) {
  77. return { basePath: "", paramsObject: {} };
  78. }
  79. // 找到问号 (?) 的位置,它之前是基础路径,之后是查询参数
  80. const questionMarkIndex = url.indexOf("?");
  81. let basePath = url;
  82. const paramsObject: { [key: string]: string } = {};
  83. // 如果找到了问号,说明有查询参数
  84. if (questionMarkIndex !== -1) {
  85. // 获取 basePath
  86. basePath = url.substring(0, questionMarkIndex);
  87. // 从 URL 中获取查询字符串部分
  88. const queryString = url.substring(questionMarkIndex + 1);
  89. // 使用 URLSearchParams 遍历参数
  90. const searchParams = new URLSearchParams(queryString);
  91. searchParams.forEach((value, key) => {
  92. // 封装进 paramsObject 对象
  93. paramsObject[key] = value;
  94. });
  95. }
  96. // 返回 basePath 和 paramsObject
  97. return { basePath, paramsObject };
  98. };
  99. const whiteList = ["/", "/login", "/social-login", "/auth-redirect"];
  100. router.beforeEach(async (to, from, next) => {
  101. // 设置页面标题
  102. const title = to.meta.title as string;
  103. if (title) {
  104. document.title = `${title}`;
  105. }
  106. if (getAccessToken()) {
  107. if (to.path === "/login") {
  108. next({ path: "/" });
  109. } else {
  110. const userStore = useUserStoreWithOut();
  111. if (!userStore.getIsSetUser) {
  112. isRelogin.show = true;
  113. await userStore.setUserInfoAction();
  114. isRelogin.show = false;
  115. const redirectPath = from.query.redirect || to.path;
  116. // 修复跳转时不带参数的问题
  117. const redirect = decodeURIComponent(redirectPath as string);
  118. const { paramsObject: query } = parseURL(redirect);
  119. const nextData =
  120. to.path === redirect
  121. ? { ...to, replace: true }
  122. : { path: redirect, query };
  123. next(nextData);
  124. } else {
  125. next();
  126. }
  127. }
  128. } else {
  129. if (whiteList.indexOf(to.path) !== -1) {
  130. const code = to.query.code;
  131. if (code) {
  132. const res = await socialLogin(
  133. "20",
  134. typeof code === "string" ? code : "",
  135. "22",
  136. );
  137. authUtil.setToken(res);
  138. next({ path: "/" });
  139. } else {
  140. next(); // 正常导航
  141. }
  142. // next();
  143. } else {
  144. next(`/login`);
  145. }
  146. }
  147. });
  148. export default router;