|
|
|
|
import fs from "fs";
|
|
|
|
|
import path from "path";
|
|
|
|
|
import type {
|
|
|
|
|
Floor,
|
|
|
|
|
NavItem,
|
|
|
|
|
HeroData,
|
|
|
|
|
AboutData,
|
|
|
|
|
TechData,
|
|
|
|
|
SolutionsData,
|
|
|
|
|
CasesData,
|
|
|
|
|
PartnersData,
|
|
|
|
|
NewsData,
|
|
|
|
|
CareersData,
|
|
|
|
|
ContactData,
|
|
|
|
|
} from "../types";
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getHero(locale?: string): HeroData {
|
|
|
|
|
return readJson<HeroData>(dataPathFor(locale, "hero.json"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getAbout(locale?: string): AboutData {
|
|
|
|
|
return readJson<AboutData>(dataPathFor(locale, "about.json"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|