ctx.uts 2.07 KB
import { isArray, parse } from "../utils";

type Page = {
	meta?: UTSJSONObject;
	path: string;
	style?: UTSJSONObject;
};

type SubPackage = {
	pages: Page[];
	root: string;
};

export type TabBarItem = {
	iconPath?: string;
	pagePath: string;
	selectedIconPath?: string;
	text?: string;
	visible?: boolean;
};

export type TabBar = {
	backgroundColor?: string;
	backgroundImage?: string;
	backgroundRepeat?: "no-repeat" | "repeat" | "repeat-x" | "repeat-y";
	blurEffect?: "dark" | "extralight" | "light" | "none";
	borderStyle?: string;
	color?: string;
	custom?: boolean;
	fontSize?: string;
	height?: string;
	iconWidth?: string;
	list?: TabBarItem[];
	position?: "bottom" | "top";
	redDotColor?: string;
	selectedColor?: string;
	spacing?: string;
};

export type Ctx = {
	appid: string;
	color: UTSJSONObject;
	globalStyle: UTSJSONObject;
	pages: Page[];
	SAFE_CHAR_MAP_LOCALE: string[][];
	subPackages: SubPackage[];
	tabBar: TabBar;
	theme: UTSJSONObject;
	uniIdRouter: UTSJSONObject;
};

// 初始化 ctx 对象,不可修改!!
export const ctx = parse<Ctx>({})!;
// PAGES 用于存储所有页面的路径及样式信息
export let PAGES: Page[] = [];

if (isArray(ctx.pages)) {
	PAGES = [...ctx.pages];
}
console.log(ctx);

// 遍历 ctx.subPackages,将所有子包下的页面信息合并到 PAGES 中
if (isArray(ctx.subPackages)) {
	ctx.subPackages.forEach((a) => {
		a.pages.forEach((b) => {
			PAGES.push({
				path: `${a.root  }/${  b.path}`, // 拼接子包根路径和页面路径
				style: b.style,
				meta: b.meta
			});
		});
	});
}

// 确保每个页面路径都以 "/" 开头,符合 uni-app x 规范
PAGES.forEach((e) => {
	if (!e.path.startsWith("/")) {
		e.path = `/${  e.path}`;
	}
});

// TABS 用于存储 tabBar 配置项
// export let TABS: TabBarItem[] = [];

// 如果 tabBar 配置存在且列表不为空,则初始化 TABS
// if (ctx.tabBar.list != null) {
// 	TABS = ctx.tabBar.list;

// 	// 确保每个 tabBar 页面的路径都以 "/" 开头
// 	TABS.forEach((e) => {
// 		if (!e.pagePath.startsWith("/")) {
// 			e.pagePath = "/" + e.pagePath;
// 		}
// 	});
// }