From 5fc7d4564c04896076aaee6d70f4c6145188d44a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E9=94=90?= Date: Tue, 1 Dec 2026 17:45:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=9F=E8=83=BD=E8=BF=AD=E4=BB=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../req/SaveProductTemplateAdminReq.java | 3 ++ .../product/res/ProductTemplateAdminRes.java | 3 ++ .../admin/service/AdminProductService.java | 17 +++++- ...V19__product_template_related_products.sql | 2 + .../entity/ProductContentTemplateEntity.java | 1 + .../product/res/ProductDetailSiteRes.java | 3 +- .../site/service/SiteProductService.java | 38 ++++++++++++- web-admin/src/ProductTemplateManager.tsx | 46 ++++++++++++++-- web-admin/src/styles.css | 50 +++++++++++++++++ web-site/src/main.tsx | 22 ++++++-- web-site/src/styles.css | 53 +++++++++++++++++++ 11 files changed, 227 insertions(+), 11 deletions(-) create mode 100644 admin/src/main/resources/db/migration/V19__product_template_related_products.sql diff --git a/admin/src/main/java/com/shoplix/admin/modules/product/req/SaveProductTemplateAdminReq.java b/admin/src/main/java/com/shoplix/admin/modules/product/req/SaveProductTemplateAdminReq.java index 6f44fe6..1f1569c 100644 --- a/admin/src/main/java/com/shoplix/admin/modules/product/req/SaveProductTemplateAdminReq.java +++ b/admin/src/main/java/com/shoplix/admin/modules/product/req/SaveProductTemplateAdminReq.java @@ -5,6 +5,8 @@ import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Size; +import java.util.List; + /** 后台保存产品公共内容模板请求。 */ public record SaveProductTemplateAdminReq( /** 模板业务编号,新增时为空。 */ @Size(max = 16) String templateCode, @@ -12,6 +14,7 @@ public record SaveProductTemplateAdminReq( /** 模板名称。 */ @NotBlank @Size(max = 100) String templateName, /** 模板用途说明。 */ @Size(max = 500) String description, /** 模板英文富文本内容。 */ @NotBlank String templateContent, + /** 关联推荐产品业务编号,最多选择 12 个。 */ @Size(max = 12) List<@Size(max = 16) String> relatedProductCodes, /** 发布状态。 */ @NotNull PublishStatusEnum status, /** 模板排序值。 */ @NotNull Integer sortNo) { } diff --git a/admin/src/main/java/com/shoplix/admin/modules/product/res/ProductTemplateAdminRes.java b/admin/src/main/java/com/shoplix/admin/modules/product/res/ProductTemplateAdminRes.java index 1550485..3a7930f 100644 --- a/admin/src/main/java/com/shoplix/admin/modules/product/res/ProductTemplateAdminRes.java +++ b/admin/src/main/java/com/shoplix/admin/modules/product/res/ProductTemplateAdminRes.java @@ -1,11 +1,14 @@ package com.shoplix.admin.modules.product.res; +import java.util.List; + /** 后台产品公共内容模板响应。 */ public record ProductTemplateAdminRes( /** 模板业务编号。 */ String templateCode, /** 模板名称。 */ String templateName, /** 模板用途说明。 */ String description, /** 模板英文富文本内容。 */ String templateContent, + /** 关联推荐产品业务编号。 */ List relatedProductCodes, /** 发布状态。 */ String status, /** 模板排序值。 */ Integer sortNo, /** 更新时间毫秒时间戳。 */ Long updateTime) { diff --git a/admin/src/main/java/com/shoplix/admin/service/AdminProductService.java b/admin/src/main/java/com/shoplix/admin/service/AdminProductService.java index 7268b4b..5f894c4 100644 --- a/admin/src/main/java/com/shoplix/admin/service/AdminProductService.java +++ b/admin/src/main/java/com/shoplix/admin/service/AdminProductService.java @@ -245,6 +245,14 @@ public class AdminProductService { entity.setTemplateName(request.templateName().trim()); entity.setDescription(request.description()); entity.setTemplateContent(request.templateContent()); + List relatedProductCodes = request.relatedProductCodes() == null ? List.of() + : request.relatedProductCodes().stream().filter(code -> code != null && !code.isBlank()).distinct().toList(); + for (String productCode : relatedProductCodes) { + if (repository.findByCode(request.siteCode(), productCode).isEmpty()) { + throw templateException("PRODUCT_TEMPLATE_RELATED_PRODUCT_NOT_FOUND", ProductTemplateMessageKeys.UNAVAILABLE); + } + } + entity.setRelatedProductCodes(String.join(",", relatedProductCodes)); entity.setStatus(request.status().name()); entity.setSortNo(request.sortNo()); entity.setUpdateTime(System.currentTimeMillis()); @@ -283,7 +291,14 @@ public class AdminProductService { /** 转换产品公共内容模板响应。 */ private ProductTemplateAdminRes toTemplateResponse(ProductContentTemplateEntity entity) { return new ProductTemplateAdminRes(entity.getTemplateCode(), entity.getTemplateName(), entity.getDescription(), - entity.getTemplateContent(), entity.getStatus(), entity.getSortNo(), entity.getUpdateTime()); + entity.getTemplateContent(), splitProductCodes(entity.getRelatedProductCodes()), entity.getStatus(), + entity.getSortNo(), entity.getUpdateTime()); + } + + /** 拆分模板关联产品业务编号。 */ + private List splitProductCodes(String value) { + return value == null || value.isBlank() ? List.of() + : java.util.Arrays.stream(value.split(",")).map(String::trim).filter(code -> !code.isBlank()).toList(); } /** 创建已完成多语言解析的产品模板业务异常。 */ diff --git a/admin/src/main/resources/db/migration/V19__product_template_related_products.sql b/admin/src/main/resources/db/migration/V19__product_template_related_products.sql new file mode 100644 index 0000000..8fd8099 --- /dev/null +++ b/admin/src/main/resources/db/migration/V19__product_template_related_products.sql @@ -0,0 +1,2 @@ +ALTER TABLE product_content_template + ADD COLUMN related_product_codes VARCHAR(1000) NOT NULL DEFAULT '' COMMENT '关联推荐产品业务编号列表,多个编号使用英文逗号分隔' AFTER template_content; diff --git a/service/src/main/java/com/shoplix/modules/product/repository/entity/ProductContentTemplateEntity.java b/service/src/main/java/com/shoplix/modules/product/repository/entity/ProductContentTemplateEntity.java index eb2f72c..a07a174 100644 --- a/service/src/main/java/com/shoplix/modules/product/repository/entity/ProductContentTemplateEntity.java +++ b/service/src/main/java/com/shoplix/modules/product/repository/entity/ProductContentTemplateEntity.java @@ -15,6 +15,7 @@ public class ProductContentTemplateEntity { /** 模板名称。 */ private String templateName; /** 模板用途说明。 */ private String description; /** 模板英文富文本内容。 */ private String templateContent; + /** 关联推荐产品业务编号列表,多个编号使用英文逗号分隔。 */ private String relatedProductCodes; /** 发布状态:DRAFT草稿,PUBLISHED已发布,OFFLINE已下线。 */ private String status; /** 模板排序值。 */ private Integer sortNo; /** 逻辑删除状态:0未删除,1已删除。 */ private Integer deleted; diff --git a/site/src/main/java/com/shoplix/site/modules/product/res/ProductDetailSiteRes.java b/site/src/main/java/com/shoplix/site/modules/product/res/ProductDetailSiteRes.java index 92e8d5e..1a1e962 100644 --- a/site/src/main/java/com/shoplix/site/modules/product/res/ProductDetailSiteRes.java +++ b/site/src/main/java/com/shoplix/site/modules/product/res/ProductDetailSiteRes.java @@ -19,6 +19,7 @@ public record ProductDetailSiteRes( /** 产品图片列表。 */ List images, /** 是否允许提交产品询盘。 */ Boolean inquiryEnabled, /** 产品属性列表。 */ List attributes, - /** 产品规格列表。 */ List specifications) { + /** 产品规格列表。 */ List specifications, + /** 关联推荐产品列表。 */ List relatedProducts) { } diff --git a/site/src/main/java/com/shoplix/site/service/SiteProductService.java b/site/src/main/java/com/shoplix/site/service/SiteProductService.java index c8942a2..81d8afc 100644 --- a/site/src/main/java/com/shoplix/site/service/SiteProductService.java +++ b/site/src/main/java/com/shoplix/site/service/SiteProductService.java @@ -139,7 +139,43 @@ public class SiteProductService { primaryCategory == null ? null : primaryCategory.getSlug(), categoryBreadcrumbs(siteCode, primaryCategory), item.getSummary(), resolveProductContent(item), item.getCoverMediaCode(), mediaUrl, images, - item.getInquiryEnabled() == 1, attributes, specifications); + item.getInquiryEnabled() == 1, attributes, specifications, resolveRelatedProducts(siteCode, item)); + } + + /** 优先读取内容模板关联产品,并使用同分类及全站产品补足推荐位。 */ + private List resolveRelatedProducts(String siteCode, ProductEntity currentProduct) { + List products = new ArrayList<>(); + Set selectedCodes = new LinkedHashSet<>(); + selectedCodes.add(currentProduct.getProductCode()); + relatedProductCodes(siteCode, currentProduct).stream() + .map(code -> repository.findByCode(siteCode, code).orElse(null)) + .forEach(item -> addRecommendedProduct(products, selectedCodes, item)); + repository.listPublished(siteCode, currentProduct.getPrimaryCategoryCode()) + .forEach(item -> addRecommendedProduct(products, selectedCodes, item)); + if (products.size() < 4) { + repository.listPublished(siteCode, null) + .forEach(item -> addRecommendedProduct(products, selectedCodes, item)); + } + return products.stream().map(item -> new ProductItemSiteRes(item.getProductCode(), item.getProductName(), + item.getSlug(), item.getModel(), item.getSummary(), item.getCoverMediaCode(), + resolveMediaUrl(siteCode, item.getCoverMediaCode()), item.getInquiryEnabled() == 1)).toList(); + } + + /** 将有效且未重复的已发布产品加入推荐列表。 */ + private void addRecommendedProduct(List products, Set selectedCodes, ProductEntity product) { + if (products.size() >= 4 || product == null || !"PUBLISHED".equals(product.getStatus())) return; + if (selectedCodes.add(product.getProductCode())) products.add(product); + } + + /** 读取产品所选模板配置的关联产品编号。 */ + private List relatedProductCodes(String siteCode, ProductEntity product) { + if (product.getContentTemplateCode() == null || product.getContentTemplateCode().isBlank()) return List.of(); + return templateRepository.findPublished(siteCode, product.getContentTemplateCode()) + .map(template -> template.getRelatedProductCodes() == null || template.getRelatedProductCodes().isBlank() + ? List.of() + : java.util.Arrays.stream(template.getRelatedProductCodes().split(",")) + .map(String::trim).filter(code -> !code.isBlank()).toList()) + .orElse(List.of()); } /** 构建从顶级分类到产品主分类的完整面包屑。 */ diff --git a/web-admin/src/ProductTemplateManager.tsx b/web-admin/src/ProductTemplateManager.tsx index 1c7147f..24df6be 100644 --- a/web-admin/src/ProductTemplateManager.tsx +++ b/web-admin/src/ProductTemplateManager.tsx @@ -30,11 +30,20 @@ type ProductTemplate = { /** 模板名称。 */ templateName: string; /** 模板用途说明。 */ description: string; /** 模板英文富文本内容。 */ templateContent: string; + /** 模板关联推荐产品业务编号。 */ relatedProductCodes: string[]; /** 发布状态。 */ status: string; /** 排序值。 */ sortNo: number; /** 更新时间毫秒时间戳。 */ updateTime?: number; }; +type ProductOption = { + /** 产品业务编号。 */ productCode: string; + /** 产品英文名称。 */ productName: string; + /** 产品型号。 */ model?: string; + /** 产品封面图片地址。 */ coverMediaUrl?: string; + /** 发布状态。 */ status: string; +}; + type MediaItem = { /** 素材业务编号。 */ mediaCode: string; /** 素材访问地址。 */ url: string; @@ -47,6 +56,7 @@ const emptyTemplate: ProductTemplate = { templateName: "", description: "", templateContent: "

", + relatedProductCodes: [], status: "DRAFT", sortNo: 30, }; @@ -54,6 +64,7 @@ const emptyTemplate: ProductTemplate = { /** 产品公共详情模板列表和编辑页面。 */ export default function ProductTemplateManager() { const [items, setItems] = useState([]); + const [products, setProducts] = useState([]); const [editing, setEditing] = useState(null); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); @@ -72,10 +83,12 @@ export default function ProductTemplateManager() { const loadTemplates = async () => { setLoading(true); try { - const data = await apiPost("/products/templates/list", { - siteCode: SITE, - }); - setItems(data.items || []); + const [data, productData] = await Promise.all([ + apiPost("/products/templates/list", { siteCode: SITE }), + apiPost("/products/list", { siteCode: SITE, pageNo: 1, pageSize: 100 }), + ]); + setItems((data.items || []).map((item: ProductTemplate) => ({ ...item, relatedProductCodes: item.relatedProductCodes || [] }))); + setProducts(productData.items || []); } catch (error) { setNotice(error instanceof Error ? error.message : "模板加载失败"); } finally { @@ -106,6 +119,7 @@ export default function ProductTemplateManager() { templateName: editing.templateName, description: editing.description, templateContent: editor.getHTML(), + relatedProductCodes: editing.relatedProductCodes || [], status: editing.status, sortNo: editing.sortNo, }); @@ -247,6 +261,30 @@ export default function ProductTemplateManager() { +
+
+

关联推荐产品

+ 已选择 {editing.relatedProductCodes?.length || 0} / 12 +
+

+ 使用该模板的产品详情页底部将统一展示所选产品;未选择时按当前产品分类自动推荐。 +

+
+ {products.filter((product) => product.status === "PUBLISHED").map((product) => { + const checked = (editing.relatedProductCodes || []).includes(product.productCode); + return ; + })} +
+

公共富文本内容

diff --git a/web-admin/src/styles.css b/web-admin/src/styles.css index a5dfff1..638dcfe 100644 --- a/web-admin/src/styles.css +++ b/web-admin/src/styles.css @@ -3966,3 +3966,53 @@ header p { min-width: 30px; } } +.template-related-products { + display: grid; + gap: 10px; + grid-template-columns: repeat(3, minmax(0, 1fr)); + max-height: 430px; + overflow: auto; +} + +.template-related-products label { + align-items: center; + border: 1px solid #dce4e1; + cursor: pointer; + display: grid; + gap: 10px; + grid-template-columns: 54px 1fr auto; + padding: 9px; +} + +.template-related-products label.selected { + background: #edf6f3; + border-color: #14735f; +} + +.template-related-products img, +.template-related-products .product-image-empty { + align-items: center; + background: #f4f6f5; + display: flex; + height: 54px; + justify-content: center; + object-fit: contain; + width: 54px; +} + +.template-related-products label > span:nth-child(2) { + display: grid; + gap: 4px; + min-width: 0; +} + +.template-related-products strong, +.template-related-products small { + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +@media (max-width: 900px) { + .template-related-products { grid-template-columns: 1fr; } +} diff --git a/web-site/src/main.tsx b/web-site/src/main.tsx index ba1b378..4349541 100644 --- a/web-site/src/main.tsx +++ b/web-site/src/main.tsx @@ -6,6 +6,10 @@ import { initializeAnalytics, routePageCode, trackInquirySubmitted, trackPageLea const API = import.meta.env.VITE_SITE_API_BASE || "/api/site/v1"; type ContentType = "NEWS" | "CASE" | "HELP"; +/** 将内容类型映射为前台标准复数路径。 */ +function contentPath(type: ContentType): string { + return type === "CASE" ? "cases" : type.toLowerCase(); +} type Item = { productCode?: string; contentCode?: string; @@ -403,7 +407,7 @@ function LiveHeader({ section, style }: { section: DecorationSection; style: Rea function LiveCollection({section}:{section:DecorationSection}) { const [items,setItems]=useState([]); useEffect(()=>{const path=section.type==="PRODUCTS"?"/products/list":"/contents/list";const body=section.type==="PRODUCTS"?{featured:true,pageNo:1,pageSize:section.limit||3}:{contentType:section.sourceType||"CASE",featured:true,pageNo:1,pageSize:section.limit||3};post(path,body).then(data=>setItems(data.items||[])).catch(()=>setItems([]))},[section.type,section.sourceType,section.limit]); - const base=section.type==="PRODUCTS"?"products":(section.sourceType||"CASE").toLowerCase(); + const base=section.type==="PRODUCTS"?"products":contentPath((section.sourceType||"CASE") as ContentType); return

FEATURED

{section.title}

; } function Header() { @@ -581,7 +585,7 @@ function Collection({ ); } - const basePath = type === "products" ? "/products" : `/${type.toLowerCase()}`; + const basePath = type === "products" ? "/products" : `/${contentPath(type as ContentType)}`; return (
@@ -607,7 +611,7 @@ function Collection({ {(data as Item[]).map((item) => (
@@ -666,7 +670,7 @@ function SearchResults({ query }: { query: string }) { setLoading(true); Promise.all([ post("/products/list", { keyword: query.trim(), pageNo: 1, pageSize: 100 }).then((data) => ({ items: data.items || [], base: "products", label: "Product" })), - ...(["NEWS", "CASE", "HELP"] as ContentType[]).map((contentType) => post("/contents/list", { contentType, keyword: query.trim(), pageNo: 1, pageSize: 100 }).then((data) => ({ items: data.items || [], base: contentType.toLowerCase(), label: contentType === "CASE" ? "Case" : contentType.charAt(0) + contentType.slice(1).toLowerCase() }))), + ...(["NEWS", "CASE", "HELP"] as ContentType[]).map((contentType) => post("/contents/list", { contentType, keyword: query.trim(), pageNo: 1, pageSize: 100 }).then((data) => ({ items: data.items || [], base: contentPath(contentType), label: contentType === "CASE" ? "Case" : contentType.charAt(0) + contentType.slice(1).toLowerCase() }))), ]).then((groups) => { const matched = groups.flatMap((group) => group.items.map((item: Item) => ({ item, base: group.base, label: group.label }))) .filter(({ item }) => [item.productName, item.title, item.summary, item.model].some((value) => value?.toLowerCase().includes(keyword))); @@ -810,6 +814,16 @@ function ProductDetailView({ data: d, configuration }: { data: Record )}
+ {(d.relatedProducts || []).length > 0 &&
+

YOU MAY ALSO LIKE

+

Related products

+
+
} {imageZoomOpen && productImages[selectedImage]?.url &&
setImageZoomOpen(false)}>{`${d.productName event.stopPropagation()}/>
} {d.inquiryEnabled && inquiryOpen && (
setInquiryOpen(false)}> diff --git a/web-site/src/styles.css b/web-site/src/styles.css index bba60da..c167bb2 100644 --- a/web-site/src/styles.css +++ b/web-site/src/styles.css @@ -1507,3 +1507,56 @@ footer small { .content img { cursor: zoom-in; } +.related-products { + border-top: 1px solid #dfe6e3; + margin-top: 54px; + padding-top: 50px; +} + +.related-products > h2 { + font-family: Georgia, serif; + font-size: clamp(30px, 4vw, 46px); + margin: 6px 0 28px; +} + +.related-products > div { + display: grid; + gap: 22px; + grid-template-columns: repeat(4, minmax(0, 1fr)); +} + +.related-products a { + color: inherit; + text-decoration: none; +} + +.related-products figure { + align-items: center; + background: #f3f6f5; + display: flex; + height: 260px; + justify-content: center; + margin: 0 0 17px; + overflow: hidden; +} + +.related-products img { + height: 100%; + object-fit: contain; + transition: transform .25s ease; + width: 100%; +} + +.related-products a:hover img { transform: scale(1.04); } +.related-products small { color: #6c7974; } +.related-products h3 { font-family: Georgia, serif; font-size: 20px; margin: 8px 0 13px; } +.related-products a > span { align-items: center; color: #08715e; display: flex; font-size: 13px; font-weight: 700; gap: 4px; } + +@media (max-width: 900px) { + .related-products > div { grid-template-columns: repeat(2, minmax(0, 1fr)); } + .related-products figure { height: 210px; } +} + +@media (max-width: 560px) { + .related-products > div { grid-template-columns: 1fr; } +}