index.ts 1.9 KB

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