index.ts 2.0 KB

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