index.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import {
  2. createRouter,
  3. createWebHistory,
  4. type RouteRecordRaw,
  5. } from "vue-router";
  6. import Home from "@/views/index.vue";
  7. import Flow from "@/views/flow/index.vue";
  8. import Login from "@/views/login.vue";
  9. import { getAccessToken } from "@utils/auth";
  10. import { socialLogin } from "@/api/user";
  11. import * as authUtil from "@/utils/auth";
  12. import {
  13. isRelogin,
  14. manualLogoutKey,
  15. reloginCancelKey,
  16. } from "@/config/axios/service";
  17. import { useUserStoreWithOut } from "@/stores/useUserStore";
  18. const routes: RouteRecordRaw[] = [
  19. {
  20. path: "/",
  21. name: "Home",
  22. component: Home,
  23. },
  24. {
  25. path: "/login",
  26. name: "Login",
  27. component: Login,
  28. meta: {
  29. title: "DeepOil 智慧经营平台 | 登录",
  30. },
  31. },
  32. {
  33. path: "/flow",
  34. name: "Flow",
  35. component: Flow,
  36. meta: {
  37. title: "DeepOil 智慧经营平台 | 流程门户",
  38. },
  39. },
  40. {
  41. path: "/todo-list",
  42. name: "TodoList",
  43. component: () => import("@/views/flow/todoList.vue"),
  44. meta: {
  45. title: "DeepOil 智慧经营平台 | 待办列表",
  46. },
  47. },
  48. {
  49. path: "/oa-done-list",
  50. name: "OADoneList",
  51. component: () => import("@/views/flow/oaDoneList.vue"),
  52. meta: {
  53. title: "DeepOil 智慧经营平台 | 已办列表",
  54. },
  55. },
  56. {
  57. path: "/crm-todo-list",
  58. name: "CRMTodoList",
  59. component: () => import("@/views/flow/crmTodoList.vue"),
  60. meta: {
  61. title: "DeepOil 智慧经营平台 | CRM待办列表",
  62. },
  63. },
  64. {
  65. path: "/crm-done-list",
  66. name: "CRMDoneList",
  67. component: () => import("@/views/flow/crmDoneList.vue"),
  68. meta: {
  69. title: "DeepOil 智慧经营平台 | CRM已办列表",
  70. },
  71. },
  72. {
  73. path: "/drive",
  74. name: "Drive",
  75. component: () => import("@/views/drive/index.vue"),
  76. meta: {
  77. title: "DeepOil 智慧经营平台 | 驾驶舱",
  78. },
  79. },
  80. {
  81. path: "/news",
  82. name: "News",
  83. component: () => import("@/views/news/index.vue"),
  84. meta: {
  85. title: "DeepOil 智慧经营平台 | 新闻",
  86. },
  87. },
  88. {
  89. path: "/notice-redhead",
  90. name: "NoticeRedhead",
  91. component: () => import("@/views/notices/index.vue"),
  92. meta: {
  93. // 动态设置页面标题
  94. dynamicTitle: (route) => {
  95. const { paramsObject } = parseURL(route.fullPath);
  96. const { title } = paramsObject;
  97. return title || "新闻";
  98. },
  99. },
  100. },
  101. ];
  102. const router = createRouter({
  103. history: createWebHistory(import.meta.env.BASE_URL),
  104. routes,
  105. scrollBehavior(to, from, savePosition) {
  106. // 始终滚动到顶部
  107. return { left: 0, top: 0 };
  108. },
  109. });
  110. export const resetRouter = (): void => {
  111. const resetWhiteNameList = ["Redirect", "Login", "NoFind", "Root"];
  112. router.getRoutes().forEach((route) => {
  113. const { name } = route;
  114. if (name && !resetWhiteNameList.includes(name as string)) {
  115. router.hasRoute(name) && router.removeRoute(name);
  116. }
  117. });
  118. };
  119. const parseURL = (
  120. url: string | null | undefined,
  121. ): { basePath: string; paramsObject: { [key: string]: string } } => {
  122. // 如果输入为 null 或 undefined,返回空字符串和空对象
  123. if (url == null) {
  124. return { basePath: "", paramsObject: {} };
  125. }
  126. // 找到问号 (?) 的位置,它之前是基础路径,之后是查询参数
  127. const questionMarkIndex = url.indexOf("?");
  128. let basePath = url;
  129. const paramsObject: { [key: string]: string } = {};
  130. // 如果找到了问号,说明有查询参数
  131. if (questionMarkIndex !== -1) {
  132. // 获取 basePath
  133. basePath = url.substring(0, questionMarkIndex);
  134. // 从 URL 中获取查询字符串部分
  135. const queryString = url.substring(questionMarkIndex + 1);
  136. // 使用 URLSearchParams 遍历参数
  137. const searchParams = new URLSearchParams(queryString);
  138. searchParams.forEach((value, key) => {
  139. // 封装进 paramsObject 对象
  140. paramsObject[key] = value;
  141. });
  142. }
  143. // 返回 basePath 和 paramsObject
  144. return { basePath, paramsObject };
  145. };
  146. const whiteList = ["/", "/login", "/social-login", "/auth-redirect"];
  147. router.beforeEach(async (to, from, next) => {
  148. // 设置页面标题
  149. const title = to.meta.title as string;
  150. if (title) {
  151. document.title = `${title}`;
  152. }
  153. if (getAccessToken()) {
  154. if (to.path === "/login") {
  155. next({ path: "/" });
  156. } else {
  157. const userStore = useUserStoreWithOut();
  158. if (!userStore.getIsSetUser) {
  159. isRelogin.show = true;
  160. await userStore.setUserInfoAction();
  161. isRelogin.show = false;
  162. const redirectPath = from.query.redirect || to.path;
  163. // 修复跳转时不带参数的问题
  164. const redirect = decodeURIComponent(redirectPath as string);
  165. const { paramsObject: query } = parseURL(redirect);
  166. const nextData =
  167. to.path === redirect
  168. ? { ...to, replace: true }
  169. : { path: redirect, query };
  170. next(nextData);
  171. } else {
  172. next();
  173. }
  174. }
  175. } else {
  176. if (whiteList.indexOf(to.path) !== -1) {
  177. const code = to.query.code;
  178. if (code) {
  179. const res = await socialLogin(
  180. "20",
  181. typeof code === "string" ? code : "",
  182. "22",
  183. );
  184. authUtil.setToken(res);
  185. sessionStorage.removeItem(manualLogoutKey);
  186. sessionStorage.removeItem(reloginCancelKey);
  187. next({ path: "/" });
  188. } else {
  189. next(); // 正常导航
  190. }
  191. // next();
  192. } else {
  193. next(`/login`);
  194. }
  195. }
  196. });
  197. export default router;