index.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. color: UTSJSONObject;
  44. };
  45. // 初始化 ctx 对象,不可修改!!
  46. export const ctx = parse<Ctx>({})!;
  47. console.log(ctx);
  48. // PAGES 用于存储所有页面的路径及样式信息
  49. export let PAGES: Page[] = [...ctx.pages];
  50. // 遍历 ctx.subPackages,将所有子包下的页面信息合并到 PAGES 中
  51. if (isArray(ctx.subPackages)) {
  52. ctx.subPackages.forEach((a) => {
  53. a.pages.forEach((b) => {
  54. PAGES.push({
  55. path: a.root + "/" + b.path, // 拼接子包根路径和页面路径
  56. style: b.style,
  57. meta: b.meta
  58. });
  59. });
  60. });
  61. }
  62. // 确保每个页面路径都以 "/" 开头,符合 uni-app x 规范
  63. PAGES.forEach((e) => {
  64. if (!e.path.startsWith("/")) {
  65. e.path = "/" + e.path;
  66. }
  67. });
  68. // TABS 用于存储 tabBar 配置项
  69. export let TABS: TabBarItem[] = [];
  70. // 如果 tabBar 配置存在且列表不为空,则初始化 TABS
  71. if (ctx.tabBar.list != null) {
  72. TABS = ctx.tabBar.list;
  73. // 确保每个 tabBar 页面的路径都以 "/" 开头
  74. TABS.forEach((e) => {
  75. if (!e.pagePath.startsWith("/")) {
  76. e.pagePath = "/" + e.pagePath;
  77. }
  78. });
  79. }