| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- import {
- createRouter,
- createWebHistory,
- type RouteRecordRaw,
- } from "vue-router";
- import Home from "@/views/index.vue";
- import Management from "@/views/management.vue";
- import Command from "@/views/command.vue";
- import ChatBI from "@/views/chatbi.vue";
- import Login from "@/views/login.vue";
- import { getAccessToken } from "@utils/auth";
- import { socialLogin } from "@/api/user";
- import * as authUtil from "@/utils/auth";
- import { isRelogin } from "@/config/axios/service";
- import { useUserStoreWithOut } from "@/stores/useUserStore";
- const routes: RouteRecordRaw[] = [
- {
- path: "/",
- name: "Home",
- component: Home,
- },
- {
- path: "/login",
- name: "Login",
- component: Login,
- meta: {
- title: "DeepOil 智慧经营平台 | 登录",
- },
- },
- {
- path: "/management",
- name: "Management",
- component: Management,
- meta: {
- title: "DeepOil 智慧经营平台 | 数字运营平台",
- },
- },
- {
- path: "/command",
- name: "Command",
- component: Command,
- meta: {
- title: "DeepOil 智慧经营平台 | 智慧指挥平台",
- },
- },
- {
- path: "/chatbi",
- name: "ChatBI",
- component: ChatBI,
- meta: {
- title: "DeepOil 智慧经营平台 | Chat BI平台",
- },
- },
- ];
- const router = createRouter({
- history: createWebHistory(import.meta.env.BASE_URL),
- routes,
- scrollBehavior(to, from, savePosition) {
- // 始终滚动到顶部
- return { left: 0, top: 0 };
- },
- });
- export const resetRouter = (): void => {
- const resetWhiteNameList = ["Redirect", "Login", "NoFind", "Root"];
- router.getRoutes().forEach((route) => {
- const { name } = route;
- if (name && !resetWhiteNameList.includes(name as string)) {
- router.hasRoute(name) && router.removeRoute(name);
- }
- });
- };
- const parseURL = (
- url: string | null | undefined,
- ): { basePath: string; paramsObject: { [key: string]: string } } => {
- // 如果输入为 null 或 undefined,返回空字符串和空对象
- if (url == null) {
- return { basePath: "", paramsObject: {} };
- }
- // 找到问号 (?) 的位置,它之前是基础路径,之后是查询参数
- const questionMarkIndex = url.indexOf("?");
- let basePath = url;
- const paramsObject: { [key: string]: string } = {};
- // 如果找到了问号,说明有查询参数
- if (questionMarkIndex !== -1) {
- // 获取 basePath
- basePath = url.substring(0, questionMarkIndex);
- // 从 URL 中获取查询字符串部分
- const queryString = url.substring(questionMarkIndex + 1);
- // 使用 URLSearchParams 遍历参数
- const searchParams = new URLSearchParams(queryString);
- searchParams.forEach((value, key) => {
- // 封装进 paramsObject 对象
- paramsObject[key] = value;
- });
- }
- // 返回 basePath 和 paramsObject
- return { basePath, paramsObject };
- };
- const whiteList = ["/", "/login", "/social-login", "/auth-redirect"];
- router.beforeEach(async (to, from, next) => {
- // 设置页面标题
- const title = to.meta.title as string;
- if (title) {
- document.title = `${title}`;
- }
- if (getAccessToken()) {
- if (to.path === "/login") {
- next({ path: "/" });
- } else {
- const userStore = useUserStoreWithOut();
- if (!userStore.getIsSetUser) {
- isRelogin.show = true;
- await userStore.setUserInfoAction();
- isRelogin.show = false;
- const redirectPath = from.query.redirect || to.path;
- // 修复跳转时不带参数的问题
- const redirect = decodeURIComponent(redirectPath as string);
- const { paramsObject: query } = parseURL(redirect);
- const nextData =
- to.path === redirect
- ? { ...to, replace: true }
- : { path: redirect, query };
- next(nextData);
- } else {
- next();
- }
- }
- } else {
- if (whiteList.indexOf(to.path) !== -1) {
- const code = to.query.code;
- if (code) {
- const res = await socialLogin(
- "20",
- typeof code === "string" ? code : "",
- "22",
- );
- authUtil.setToken(res);
- next({ path: "/" });
- } else {
- next(); // 正常导航
- }
- // next();
- } else {
- next(`/login`);
- }
- }
- });
- export default router;
|