import React, { useEffect, useState } from "react"; import { createRoot } from "react-dom/client"; import { ArrowRight, ChevronRight, Image as ImageIcon, Mail, Menu, Search, X } from "lucide-react"; import "./styles.css"; import { initializeAnalytics, routePageCode, trackInquirySubmitted, trackPageLeave, trackPageView } from "./analytics"; const API = import.meta.env.VITE_SITE_API_BASE || "/api/site/v1"; type ContentType = "NEWS" | "CASE" | "HELP"; type Item = { productCode?: string; contentCode?: string; productName?: string; title?: string; slug: string; summary?: string; model?: string; publishTime?: number; coverMediaUrl?: string; }; type InquiryField = { fieldCode: string; fieldKey: string; label: string; fieldType: string; placeholder?: string; helpText?: string; maxLength?: number; required: boolean; optionsJson?: string; widthType: string; }; type InquiryFormData = { formCode: string; description?: string; submitButtonText: string; successMessage: string; fields: InquiryField[]; }; type SeoData = { title: string; description?: string; keywords?: string; canonicalUrl?: string; robots?: string; ogTitle?: string; ogDescription?: string; ogImageUrl?: string; structuredData?: string; }; function assetUrl(url?: string): string { return url || ""; } async function post(path: string, body: unknown) { const r = await fetch(API + path, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), }); if (!r.ok) throw new Error((await r.json()).message || "Unable to load content"); return r.json(); } const RICH_TEXT_ALLOWED_TAGS = new Set([ "A", "B", "BLOCKQUOTE", "BR", "CODE", "EM", "H2", "H3", "H4", "HR", "I", "IMG", "LI", "OL", "P", "PRE", "S", "STRONG", "TABLE", "TBODY", "TD", "TH", "THEAD", "TR", "U", "UL", ]); const RICH_TEXT_REMOVED_TAGS = new Set(["BUTTON", "EMBED", "FORM", "IFRAME", "INPUT", "OBJECT", "SCRIPT", "STYLE"]); /** 判断富文本链接或图片地址是否为可安全输出的站内、HTTP 或 HTTPS 地址。 */ function isSafeRichTextUrl(value: string): boolean { const normalized = value.trim().replace(/[\u0000-\u0020]+/g, "").toLowerCase(); return normalized.startsWith("/") || normalized.startsWith("#") || normalized.startsWith("http://") || normalized.startsWith("https://") || normalized.startsWith("mailto:") || normalized.startsWith("tel:"); } /** 清理富文本节点,仅保留前台展示所需的安全标签和属性。 */ function sanitizeRichTextHtml(content: string): string { const container = document.createElement("div"); container.innerHTML = content; Array.from(container.querySelectorAll("*")).forEach((element) => { if (RICH_TEXT_REMOVED_TAGS.has(element.tagName)) { element.remove(); return; } if (!RICH_TEXT_ALLOWED_TAGS.has(element.tagName)) { element.replaceWith(...Array.from(element.childNodes)); return; } Array.from(element.attributes).forEach((attribute) => { const allowed = element.tagName === "A" && ["href", "target", "rel"].includes(attribute.name) || element.tagName === "IMG" && ["src", "alt", "title"].includes(attribute.name) || ["TD", "TH"].includes(element.tagName) && ["colspan", "rowspan"].includes(attribute.name); if (!allowed) element.removeAttribute(attribute.name); }); if (element instanceof HTMLAnchorElement) { if (!isSafeRichTextUrl(element.getAttribute("href") || "")) element.removeAttribute("href"); if (element.target === "_blank") element.rel = "noopener noreferrer"; } if (element instanceof HTMLImageElement && !isSafeRichTextUrl(element.getAttribute("src") || "")) { element.removeAttribute("src"); } }); return container.innerHTML; } /** 兼容历史数据中被转义后保存的富文本标签,并统一执行安全清理。 */ function normalizeRichTextHtml(content?: string): string { if (!content) return ""; const decoder = document.createElement("textarea"); let decoded = content; if (/&(?:amp;)?lt;\/?(?:h[1-6]|p|ul|ol|li|blockquote|table|img|a)\b/i.test(content)) { for (let index = 0; index < 2; index += 1) { decoder.innerHTML = decoded; const next = decoder.value; if (next === decoded) break; decoded = next; } } decoded = decoded.replace(/^\s*
/i, "").replace(/<\/code><\/pre>(?:

<\/p>)?\s*$/i, ""); return sanitizeRichTextHtml(decoded); } function setMeta(name: string, content?: string) { const selector = `meta[name="${name}"]`; let element = document.head.querySelector(selector); if (!content) { element?.remove(); return; } if (!element) { element = document.createElement("meta"); element.name = name; document.head.appendChild(element); } element.content = content; } function setProperty(property: string, content?: string) { const selector = `meta[property="${property}"]`; let element = document.head.querySelector(selector); if (!content) { element?.remove(); return; } if (!element) { element = document.createElement("meta"); element.setAttribute("property", property); document.head.appendChild(element); } element.content = content; } function applySeo(data?: SeoData) { const canonicalValue = data?.canonicalUrl || `${location.origin}${location.pathname}`; const canonical = new URL(canonicalValue, location.origin).href; if (data?.title) document.title = data.title; setMeta("description", data?.description); setMeta("keywords", data?.keywords); setMeta("robots", data?.robots || "index,follow"); let link = document.head.querySelector('link[rel="canonical"]'); if (!link) { link = document.createElement("link"); link.rel = "canonical"; document.head.appendChild(link); } link.href = canonical; setProperty("og:title", data?.ogTitle || data?.title); setProperty("og:description", data?.ogDescription || data?.description); setProperty("og:image", data?.ogImageUrl ? new URL(data.ogImageUrl, location.origin).href : undefined); setProperty("og:url", canonical); let structuredData = document.head.querySelector('script[data-shoplix-seo="json-ld"]'); structuredData?.remove(); if (data?.structuredData) { structuredData = document.createElement("script"); structuredData.type = "application/ld+json"; structuredData.dataset.shoplixSeo = "json-ld"; structuredData.text = data.structuredData; document.head.appendChild(structuredData); } } function usePageSeo(pageType: string, targetCode = "") { useEffect(() => { let active = true; applySeo(); if (!pageType) return () => { active = false; }; post("/seo/detail", { pageType, targetCode }) .then((data: SeoData) => { if (active) applySeo(data); }) .catch(() => undefined); return () => { active = false; }; }, [pageType, targetCode]); } const nav = [ ["Products", "/products"], ["Cases", "/cases"], ["News", "/news"], ["Help", "/help"], ["About", "/about-us"], ]; type NavigationItem = { title: string; url: string; openType: "CURRENT" | "NEW"; children: NavigationItem[] }; /** 读取已发布导航,没有配置时返回既有默认菜单。 */ function useNavigation(position: "HEADER" | "FOOTER"): NavigationItem[] { const fallback = nav.map(([title, url]) => ({ title, url, openType: "CURRENT" as const, children: [] })); const [items, setItems] = useState(fallback); useEffect(() => { let active = true; post("/navigations/detail", { position }) .then((data) => { if (active && data.items?.length) setItems(data.items); }) .catch(() => undefined); return () => { active = false; }; }, [position]); return items; } /** 渲染支持二级菜单的前台导航链接。 */ function NavigationLinks({ items }: { items: NavigationItem[] }) { return <>{items.map((item) =>

{item.title}{item.children?.length > 0 &&
{item.children.map((child) => {child.title})}
}
)}; } function route() { const p = location.pathname.split("/").filter(Boolean); const previewPage = new URLSearchParams(location.search).get("previewPage") || undefined; if (!p.length) return { kind: "home", previewPage }; if (["products", "news", "cases", "help"].includes(p[0])) return { kind: p[0], slug: p[1], previewPage }; return { kind: "page", slug: p[0], previewPage }; } function App() { const r = route(); useEffect(() => { initializeAnalytics(); const context = { pageCode: routePageCode(), pageName: document.title, scene: r.kind }; trackPageView(context); const leave = () => trackPageLeave(context); window.addEventListener("pagehide", leave, { once: true }); return () => window.removeEventListener("pagehide", leave); }, [r.kind, r.slug]); const runtime = ; const seoRuntime = ; if (r.kind === "home") return <>{runtime}{seoRuntime}