generateRoutes.ts 3.36 KB
Newer Older
zhangsan's avatar
zhangsan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
import { RouteRecordRaw } from "vue-router";
import type { Component } from "vue";

interface RouteConfig {
  path: string;
  name: string;
  component: () => Promise<Component>;
  meta: {
    title: string;
    layout?: "tab" | "header" | "base";
  };
}

// 自动导入所有视图组件
const views = import.meta.glob("../views/**/*.vue");

// 标题映射表
const titleMap: Record<string, string> = {
  home: "首页",
  xbgz: "习币购置",
  xbxz: "习币须知",
  user: "个人中心",
  shiming: "实名认证",
  zjmx: "资金明细",
  invite: "邀请好友",
  mybankCard: "绑定银行卡",
  group: "官方群聊",
  shouhuo: "收货地址",
  addaddRess: "收货地址详情",
  signIn: "签到中心",
  myteam: "我的团队",
  withdraw: "提现记录",
  login: "登录",
  register: "注册",
  appDownload: "APP下载",
  tixian: "提现",
  paoshou: "抛售习币",
  paoshoudetail: "抛售详情",
};

// 处理路径转换为路由配置
function pathToRoute(path: string): RouteConfig {
  // 移除 ../views 和 .vue
  const routePath = path
    .replace("../views", "")
    .replace(".vue", "")
    .replace("/index", "");

  // 获取路由名称
  const name = routePath.split("/").filter(Boolean).pop() || "index";

  // 判断是否是一级路由
  const isFirstLevel = routePath.split("/").filter(Boolean).length === 1;

  // 设置布局类型
  let layout: "tab" | "header" | "base" = "base";
  if (isFirstLevel && ["home", "xbgz", "xbxz", "user"].includes(name)) {
    layout = "tab";
  } else if (!isFirstLevel) {
    layout = "header";
  }
  return {
    path: routePath || "/",
    name,
    component: views[path] as () => Promise<Component>,
    meta: {
      title: titleMap[name] || name, // 使用标题映射表获取标题
      layout,
    },
  };
}

// 生成路由配置
export function generateRoutes(): RouteRecordRaw[] {
  const routes: RouteConfig[] = Object.keys(views).map(pathToRoute);

  // 添加根路由重定向
  routes.unshift({
    path: "/",
    name: "root",
    component: () => import("../views/home/index.vue"),
    meta: {
      title: "首页",
      layout: "tab",
    },
  });

  // 添加404路由
  routes.push({
    path: "/:pathMatch(.*)*",
    name: "NotFound",
    component: () => import("../views/error/404.vue"),
    meta: {
      title: "404",
      layout: "base",
    },
  });

  // 按布局分组路由
  const layoutMap = {
    tab: () => import("../layouts/TabBarLayout.vue"),
    header: () => import("../layouts/HeaderLayout.vue"),
    base: () => import("../layouts/BaseLayout.vue"),
  };

  const processedRoutes = routes.reduce((acc: RouteRecordRaw[], route) => {
    const layout = route.meta.layout || "base";
    const layoutComponent = layoutMap[layout];

    // 查找是否已存在相同布局的路由组
    const existingLayoutRoute = acc.find(
      (r) => r.component === layoutComponent
    );

    if (existingLayoutRoute) {
      existingLayoutRoute.children?.push({
        path: route.path,
        name: route.name,
        component: route.component,
        meta: route.meta,
      });
    } else {
      acc.push({
        path: "/",
        component: layoutComponent,
        children: [
          {
            path: route.path,
            name: route.name,
            component: route.component,
            meta: route.meta,
          },
        ],
      });
    }

    return acc;
  }, []);

  return processedRoutes;
}