You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.6 KiB

2 months ago
import fs from "fs";
import path from "path";
1 month ago
import type {
Floor,
NavItem,
HeroData,
AboutData,
TechData,
SolutionsData,
CasesData,
PartnersData,
NewsData,
CareersData,
ContactData,
} from "../types";
2 months ago
function readJson<T>(relativePath: string): T {
const filePath = path.join(process.cwd(), relativePath);
const raw = fs.readFileSync(filePath, "utf-8");
return JSON.parse(raw) as T;
}
function dataPathFor(locale: string | undefined, filename: string): string {
const base = "data";
const candidates = [
locale ? path.join(base, locale, filename) : undefined,
path.join(base, filename),
].filter(Boolean) as string[];
for (const p of candidates) {
if (fs.existsSync(path.join(process.cwd(), p))) return p;
}
return path.join(base, filename);
}
export function getMainNav(locale?: string): NavItem[] {
return readJson<NavItem[]>(dataPathFor(locale, "mainnav.json"));
}
export function getFloors(locale?: string): Floor[] {
return readJson<Floor[]>(dataPathFor(locale, "products.json"));
}
export function getFloorBySlug(slug: string, locale?: string): Floor | undefined {
const floors = getFloors(locale);
// 允许 id 带前缀,如 floor-phone,对应 slug phone
return floors.find((f) => f.id === slug || f.id === `floor-${slug}`);
}
export function getProductById(id: string, locale?: string) {
const floors = getFloors(locale);
for (const f of floors) {
const p = f.products.find((x) => x.id === id);
if (p) return { product: p, floor: f } as const;
}
return undefined;
}
1 month ago
export function getHero(locale?: string): HeroData {
return readJson<HeroData>(dataPathFor(locale, "hero.json"));
2 months ago
}
export function getAbout(locale?: string): AboutData {
return readJson<AboutData>(dataPathFor(locale, "about.json"));
}
1 month ago
export function getTech(locale?: string): TechData {
return readJson<TechData>(dataPathFor(locale, "tech.json"));
}
export function getSolutions(locale?: string): SolutionsData {
return readJson<SolutionsData>(dataPathFor(locale, "solutions.json"));
}
export function getCases(locale?: string): CasesData {
return readJson<CasesData>(dataPathFor(locale, "cases.json"));
}
export function getPartners(locale?: string): PartnersData {
return readJson<PartnersData>(dataPathFor(locale, "partners.json"));
}
export function getNews(locale?: string): NewsData {
return readJson<NewsData>(dataPathFor(locale, "news.json"));
}
export function getCareers(locale?: string): CareersData {
return readJson<CareersData>(dataPathFor(locale, "careers.json"));
}
export function getContact(locale?: string): ContactData {
return readJson<ContactData>(dataPathFor(locale, "contact.json"));
}
2 months ago