index.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { isArray, parse } from "../utils";
  2. type Page = {
  3. path: string;
  4. style?: UTSJSONObject;
  5. meta?: UTSJSONObject;
  6. };
  7. type SubPackage = {
  8. root: string;
  9. pages: Page[];
  10. };
  11. export type TabBarItem = {
  12. text?: string;
  13. pagePath: string;
  14. iconPath?: string;
  15. selectedIconPath?: string;
  16. visible?: boolean;
  17. };
  18. export type TabBar = {
  19. custom?: boolean;
  20. color?: string;
  21. selectedColor?: string;
  22. backgroundColor?: string;
  23. borderStyle?: string;
  24. blurEffect?: "dark" | "extralight" | "light" | "none";
  25. list?: TabBarItem[];
  26. position?: "top" | "bottom";
  27. fontSize?: string;
  28. iconWidth?: string;
  29. spacing?: string;
  30. height?: string;
  31. backgroundImage?: string;
  32. backgroundRepeat?: "repeat" | "repeat-x" | "repeat-y" | "no-repeat";
  33. redDotColor?: string;
  34. };
  35. export type Ctx = {
  36. appid: string;
  37. globalStyle: UTSJSONObject;
  38. pages: Page[];
  39. uniIdRouter: UTSJSONObject;
  40. theme: UTSJSONObject;
  41. tabBar: TabBar;
  42. subPackages: SubPackage[];
  43. SAFE_CHAR_MAP_LOCALE: string[][];
  44. color: UTSJSONObject;
  45. };
  46. // 初始化 ctx 对象,不可修改!!
  47. export const ctx = parse<Ctx>({})!;
  48. console.log(ctx);
  49. // PAGES 用于存储所有页面的路径及样式信息
  50. export let PAGES: Page[] = [...ctx.pages];
  51. // 遍历 ctx.subPackages,将所有子包下的页面信息合并到 PAGES 中
  52. if (isArray(ctx.subPackages)) {
  53. ctx.subPackages.forEach((a) => {
  54. a.pages.forEach((b) => {
  55. PAGES.push({
  56. path: a.root + "/" + b.path, // 拼接子包根路径和页面路径
  57. style: b.style,
  58. meta: b.meta ?? {},
  59. });
  60. });
  61. });
  62. }
  63. // 确保每个页面路径都以 "/" 开头,符合 uni-app x 规范
  64. PAGES.forEach((e) => {
  65. if (!e.path.startsWith("/")) {
  66. e.path = "/" + e.path;
  67. }
  68. });
  69. // TABS 用于存储 tabBar 配置项
  70. export let TABS: TabBarItem[] = [];
  71. // 如果 tabBar 配置存在且列表不为空,则初始化 TABS
  72. if (ctx.tabBar.list != null) {
  73. TABS = ctx.tabBar.list;
  74. // 确保每个 tabBar 页面的路径都以 "/" 开头
  75. TABS.forEach((e) => {
  76. if (!e.pagePath.startsWith("/")) {
  77. e.pagePath = "/" + e.pagePath;
  78. }
  79. });
  80. }