index.ts 1.9 KB

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