bug修复

This commit is contained in:
张锐 committed 2026-11-24 16:06:54 +08:00
1 parent 4d55b6177e
commit d6f05ae975
5 files changed
+98 -13

No files matched your search

+64 -9
View File
@@ -1,6 +1,6 @@
import React, { useEffect, useState } from "react";
import { createRoot } from "react-dom/client";
import { ArrowRight, ChevronRight, Mail, Menu, Search } from "lucide-react";
import { ArrowRight, ChevronRight, Image as ImageIcon, Mail, Menu, Search } from "lucide-react";
import "./styles.css";
import { initializeAnalytics, routePageCode, trackInquirySubmitted, trackPageLeave, trackPageView } from "./analytics";
@@ -47,6 +47,7 @@ type SeoData = {
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",
@@ -57,6 +58,23 @@ async function post(path: string, body: unknown) {
throw new Error((await r.json()).message || "Unable to load content");
return r.json();
}
/** 兼容历史数据中被转义后保存的富文本标签。 */
function normalizeRichTextHtml(content?: string): string {
if (!content || !/&(?:amp;)?lt;\/?(?:h[1-6]|p|ul|ol|li|blockquote|table|img|a)\b/i.test(content)) return content || "";
const decoder = document.createElement("textarea");
let decoded = 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*<pre><code>/i, "").replace(/<\/code><\/pre>(?:<p><\/p>)?\s*$/i, "");
if (!/<\/?(?:h[1-6]|p|ul|ol|li|blockquote|table|img|a)\b/i.test(decoded)) return content;
const container = document.createElement("div");
container.innerHTML = decoded;
return container.innerHTML;
}
function setMeta(name: string, content?: string) {
const selector = `meta[name="${name}"]`;
let element = document.head.querySelector<HTMLMetaElement>(selector);
@@ -204,11 +222,22 @@ function LiveSection({section}:{section:DecorationSection}) {
if(section.type==="HEADER")return <LiveHeader style={style}/>;
if(section.type==="HERO")return <section className="live-hero" style={{...style,backgroundImage:section.imageUrl?`linear-gradient(90deg,rgba(0,0,0,.72),rgba(0,0,0,.12)),url(${section.imageUrl})`:undefined}}><div><p>ENGINEERED FOR INDUSTRY</p><h1>{section.title}</h1><span>{section.subtitle}</span>{section.buttonText&&<a href={section.buttonUrl||"#"}>{section.buttonText}<ArrowRight size={17}/></a>}</div></section>;
if(section.type==="RICH_TEXT")return <section className="live-rich" style={style}><h2>{section.title}</h2><p>{section.subtitle}</p></section>;
if(section.type==="PRODUCT_DETAIL")return <RuntimeProductDetail/>;
if(section.type==="PRODUCTS"||section.type==="CONTENT")return <LiveCollection section={section}/>;
if(section.type==="INQUIRY")return <section className="live-inquiry" style={style}><div><p>START A PROJECT</p><h2>{section.title}</h2><span>{section.subtitle}</span></div><InquiryForm scene="GENERAL"/></section>;
if(section.type==="FOOTER")return <footer className="live-footer" style={style}><div><b>SHOPLIX</b><p>Industrial Solutions</p></div><div><b>Explore</b><a href="/products">Products</a><a href="/cases">Cases</a><a href="/news">News</a></div><div><b>Company</b><a href="/about-us">About Us</a><a href="/contact-us">Contact Us</a><a href="/privacy-policy">Privacy Policy</a></div><small>{section.subtitle||"Copyright © 2026. All rights reserved."}</small></footer>;
return null;
}
/** 装修模板中的产品详情组件,按当前访问路径加载真实商品。 */
function RuntimeProductDetail() {
const slug = route().slug;
const [product, setProduct] = useState<Record<string, any>>();
const [error, setError] = useState("");
useEffect(() => { if (!slug) return; post("/products/detail", { slug }).then(setProduct).catch((reason) => setError(reason instanceof Error ? reason.message : "Unable to load product")); }, [slug]);
if (error) return <ErrorState message={error}/>;
if (!product) return <div className="loading">Loading product…</div>;
return <ProductDetailView data={product}/>;
}
/** 装修页面使用的动态页头。 */
function LiveHeader({ style }: { style: React.CSSProperties }) {
const items = useNavigation("HEADER");
@@ -218,7 +247,7 @@ function LiveCollection({section}:{section:DecorationSection}) {
const [items,setItems]=useState<Item[]>([]);
useEffect(()=>{const path=section.type==="PRODUCTS"?"/products/list":"/contents/list";const body=section.type==="PRODUCTS"?{}:{contentType:section.sourceType||"CASE"};post(path,body).then(data=>setItems((data.items||[]).slice(0,section.limit||3))).catch(()=>setItems([]))},[section.type,section.sourceType,section.limit]);
const base=section.type==="PRODUCTS"?"products":(section.sourceType||"CASE").toLowerCase();
return <section className="live-collection" style={{background:section.background,color:section.textColor}}><p>FEATURED</p><h2>{section.title}</h2><div>{items.map(item=><a href={`/${base}/${item.slug}`} key={item.productCode||item.contentCode}><div>{item.coverMediaUrl&&<img src={`http://localhost:8082${item.coverMediaUrl}`} alt={item.productName||item.title}/>}</div><span>{section.type==="PRODUCTS"?"PRODUCT":section.sourceType}</span><h3>{item.productName||item.title}</h3><p>{item.summary}</p></a>)}</div></section>;
return <section className="live-collection" style={{background:section.background,color:section.textColor}}><p>FEATURED</p><h2>{section.title}</h2><div>{items.map(item=><a href={`/${base}/${item.slug}`} key={item.productCode||item.contentCode}><div>{item.coverMediaUrl&&<img src={assetUrl(item.coverMediaUrl)} alt={item.productName||item.title}/>}</div><span>{section.type==="PRODUCTS"?"PRODUCT":section.sourceType}</span><h3>{item.productName||item.title}</h3><p>{item.summary}</p></a>)}</div></section>;
}
function Header() {
const items = useNavigation("HEADER");
@@ -338,17 +367,17 @@ function Collection({
if (error) return <ErrorState message={error} />;
if (slug && !Array.isArray(data)) {
const d = data as Record<string, any>;
if (type === "products") return <ProductDetailView data={d}/>;
return (
<article className="detail">
<Crumbs current={String(d.productName || d.title || title)} />
<p className="eyebrow">{title.toUpperCase()}</p>
<h1>{d.productName || d.title}</h1>
<p className="lead">{d.summary}</p>
{d.coverMediaUrl && <img className="product-cover" src={`http://localhost:8082${d.coverMediaUrl}`} alt={d.productName || d.title}/>}
{d.attributes?.length > 0 && <section className="specs"><h2>Product attributes</h2>{d.attributes.map((a:any,i:number)=><div key={i}><span>{a.name}</span><strong>{a.value} {a.unit}</strong></div>)}</section>}
<div
className="content"
dangerouslySetInnerHTML={{ __html: d.content || "" }}
dangerouslySetInnerHTML={{ __html: normalizeRichTextHtml(d.content) }}
/>
{d.specifications?.length > 0 && (
<section className="specs">
@@ -366,9 +395,6 @@ function Collection({
))}
</section>
)}
{type === "products" && d.inquiryEnabled && (
<InquiryForm scene="PRODUCT" sourceCode={d.productCode} />
)}
</article>
);
}
@@ -393,7 +419,12 @@ function Collection({
key={item.productCode || item.contentCode}
>
<div className="thumb">
{item.coverMediaUrl && <img src={`http://localhost:8082${item.coverMediaUrl}`} alt={item.productName || item.title}/>}
{item.coverMediaUrl && (
<img
src={assetUrl(item.coverMediaUrl)}
alt={item.productName || item.title}
/>
)}
<span>{type === "products" ? "PRODUCT" : type}</span>
</div>
<div>
@@ -414,6 +445,30 @@ function Collection({
</section>
);
}
/** B2B 商品详情主体,集中展示主图、核心信息、参数、详情和询盘入口。 */
function ProductDetailView({ data: d }: { data: Record<string, any> }) {
return <article className="detail product-detail">
<Crumbs current={String(d.productName || "Product")}/>
<section className="product-overview">
<div className="product-media">
{d.coverMediaUrl
? <img src={assetUrl(d.coverMediaUrl)} alt={d.productName || "Product image"}/>
: <div className="product-image-placeholder"><ImageIcon size={48}/><span>Product image coming soon</span></div>}
</div>
<div className="product-summary">
<p className="eyebrow">PRODUCT</p>
<h1>{d.productName}</h1>
{d.model && <div className="product-model">Model <strong>{d.model}</strong></div>}
<p className="lead">{d.summary}</p>
{d.inquiryEnabled && <a className="product-inquiry-cta" href="#product-inquiry"><Mail size={18}/>Request a quote</a>}
</div>
</section>
{d.attributes?.length > 0 && <section className="specs product-attributes"><h2>Product attributes</h2>{d.attributes.map((a:any,i:number)=><div key={i}><span>{a.name}</span><strong>{a.value} {a.unit}</strong></div>)}</section>}
<div className="content product-content" dangerouslySetInnerHTML={{__html:normalizeRichTextHtml(d.content)}}/>
{d.specifications?.length > 0 && <section className="specs"><h2>Technical specifications</h2>{d.specifications.map((s:any,i:number)=><div key={i}><span>{s.groupName&&`${s.groupName} · `}{s.name}</span><strong>{s.value} {s.unit}</strong></div>)}</section>}
{d.inquiryEnabled && <div id="product-inquiry"><InquiryForm scene="PRODUCT" sourceCode={d.productCode}/></div>}
</article>;
}
function Page({ slug }: { slug: string }) {
if (slug === "contact-us")
return (
@@ -588,7 +643,7 @@ function InquiryForm({
/>
{uploadingField === field.fieldCode && <small>Uploading…</small>}
{custom[field.fieldCode] && (
<img src={`http://localhost:8082${custom[field.fieldCode]}`} alt={field.label}/>
<img src={assetUrl(custom[field.fieldCode])} alt={field.label}/>
)}
</div>
) : field.fieldType === "TEXTAREA" ? (