index.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { isArray, parse } from "../utils";
  2. type Page = {
  3. path: string;
  4. style?: UTSJSONObject;
  5. };
  6. type SubPackage = {
  7. root: string;
  8. pages: Page[];
  9. };
  10. export type TabBarItem = {
  11. text?: string;
  12. pagePath: string;
  13. iconPath?: string;
  14. selectedIconPath?: string;
  15. visible?: boolean;
  16. };
  17. export type TabBar = {
  18. custom?: boolean;
  19. color?: string;
  20. selectedColor?: string;
  21. backgroundColor?: string;
  22. borderStyle?: string;
  23. blurEffect?: "dark" | "extralight" | "light" | "none";
  24. list?: TabBarItem[];
  25. position?: "top" | "bottom";
  26. fontSize?: string;
  27. iconWidth?: string;
  28. spacing?: string;
  29. height?: string;
  30. backgroundImage?: string;
  31. backgroundRepeat?: "repeat" | "repeat-x" | "repeat-y" | "no-repeat";
  32. redDotColor?: string;
  33. };
  34. export type Ctx = {
  35. appid: string;
  36. globalStyle: UTSJSONObject;
  37. pages: Page[];
  38. uniIdRouter: UTSJSONObject;
  39. theme: UTSJSONObject;
  40. tabBar: TabBar;
  41. subPackages: SubPackage[];
  42. SAFE_CHAR_MAP_LOCALE: string[][];
  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. });
  58. });
  59. });
  60. }
  61. // 确保每个页面路径都以 "/" 开头,符合 uni-app x 规范
  62. PAGES.forEach((e) => {
  63. if (!e.path.startsWith("/")) {
  64. e.path = "/" + e.path;
  65. }
  66. });
  67. // TABS 用于存储 tabBar 配置项
  68. export let TABS: TabBarItem[] = [];
  69. // 如果 tabBar 配置存在且列表不为空,则初始化 TABS
  70. if (ctx.tabBar.list != null) {
  71. TABS = ctx.tabBar.list;
  72. // 确保每个 tabBar 页面的路径都以 "/" 开头
  73. TABS.forEach((e) => {
  74. if (!e.pagePath.startsWith("/")) {
  75. e.pagePath = "/" + e.pagePath;
  76. }
  77. });
  78. }