Commit 7375407db5fde7ef95e6bc56b1a12aa609618dc1

Authored by 曾国涛
1 parent 4ee4435a

feat(静态页面生成): 实现产品列表静态页面生成功能

- 新增静态页面配置和生成逻辑
- 添加 FreeMarker 模板支持
- 实现产品数据动态加载和渲染
- 优化产品价格显示逻辑
- 新增静态资源路径配置
shop/src/main/java/com/canrd/shop/config/StaticHtmlConfig.java
... ... @@ -14,4 +14,6 @@ public class StaticHtmlConfig {
14 14  
15 15 private String url;
16 16  
  17 + private String secret;
  18 +
17 19 }
... ...
shop/src/main/java/com/canrd/shop/controller/ProductController.java
1 1 package com.canrd.shop.controller;
2 2  
  3 +import cn.hutool.core.collection.CollUtil;
3 4 import com.canrd.shop.common.constant.ServerResult;
  5 +import com.canrd.shop.common.exception.BusinessException;
4 6 import com.canrd.shop.common.jsr303.OperateGroup;
5 7 import com.canrd.shop.config.StaticHtmlConfig;
  8 +import com.canrd.shop.module.dto.ProductCategoryDO;
6 9 import com.canrd.shop.module.dto.ProductDO;
  10 +import com.canrd.shop.module.dto.ProductFunctionDO;
7 11 import com.canrd.shop.module.vo.ProductCategoryQueryVO;
8 12 import com.canrd.shop.module.vo.ProductQueryVO;
9 13 import com.canrd.shop.module.vo.ProductVO;
10 14 import com.canrd.shop.service.ProductCategoryService;
  15 +import com.canrd.shop.service.ProductFunctionService;
11 16 import com.canrd.shop.service.ProductService;
12 17 import freemarker.template.Configuration;
13 18 import freemarker.template.Template;
... ... @@ -51,6 +56,9 @@ public class ProductController {
51 56 @Autowired
52 57 private ProductCategoryService productCategoryService;
53 58  
  59 + @Autowired
  60 + private ProductFunctionService productFunctionService;
  61 +
54 62 /**
55 63 * 产品分类
56 64 *
... ... @@ -85,12 +93,46 @@ public class ProductController {
85 93 * @return 查询结果
86 94 */
87 95 @GetMapping("/generateList")
88   - public ServerResult generateList(@Validated({OperateGroup.List.class}) ProductQueryVO productQueryVO) throws IOException, TemplateException {
  96 + public ServerResult generateList(@Validated({OperateGroup.List.class}) ProductQueryVO productQueryVO
  97 + ,@RequestParam(value = "secret",required = true) String secret)
  98 + throws IOException, TemplateException {
  99 + if (!Objects.equals(secret, staticHtmlConfig.getSecret())){
  100 + return ServerResult.fail("secret error");
  101 + }
  102 + if(StringUtils.isNotBlank(productQueryVO.getProductCategoryName())){
  103 + Optional<ProductCategoryDO> productCategoryDOOpt = productCategoryService.lambdaQuery()
  104 + .eq(ProductCategoryDO::getName, productQueryVO.getProductCategoryName())
  105 + .oneOpt();
  106 + productQueryVO
  107 + .setProductCategoryId(productCategoryDOOpt
  108 + .orElseThrow(()-> new BusinessException("productCategoryName not exist"))
  109 + .getId());
  110 + }
  111 + if(StringUtils.isNotBlank(productQueryVO.getRootProductCategoryName())){
  112 + Optional<ProductCategoryDO> productCategoryDOOpt = productCategoryService.lambdaQuery()
  113 + .eq(ProductCategoryDO::getName, productQueryVO.getRootProductCategoryName())
  114 + .oneOpt();
  115 + productQueryVO
  116 + .setRootProductCategoryId(productCategoryDOOpt
  117 + .orElseThrow(()-> new BusinessException("productCategoryName not exist"))
  118 + .getId());
  119 + }
  120 + if(StringUtils.isNotBlank(productQueryVO.getProductFunctionName())){
  121 + Optional<ProductFunctionDO> productFunctionDO = productFunctionService.lambdaQuery()
  122 + .eq(ProductFunctionDO::getName, productQueryVO.getProductFunctionName())
  123 + .oneOpt();
  124 + productQueryVO
  125 + .setProductFunctionId(productFunctionDO.orElseThrow(()-> new BusinessException("productFunctionName not exist"))
  126 + .getId());
  127 + }
89 128 String path = staticHtmlConfig.getUrl();
90   - String fileName = Optional.ofNullable(productQueryVO.getRootProductCategoryId()).orElse("-")+"_"
91   - +Optional.ofNullable(productQueryVO.getProductCategoryId()).orElse("-")+"_"
92   - +Optional.ofNullable(productQueryVO.getProductFunctionId()).orElse("-")+"_"
93   - +Optional.ofNullable(productQueryVO.getKeyword()).orElse("-").trim();
  129 + String fileName = (StringUtils.isNotBlank(productQueryVO.getRootProductCategoryName())?productQueryVO.getRootProductCategoryName():"-")
  130 + +"_"
  131 + +(StringUtils.isNotBlank(productQueryVO.getProductCategoryName())?productQueryVO.getProductCategoryName():"-")
  132 + +"_"
  133 + +(StringUtils.isNotBlank(productQueryVO.getProductFunctionName())?productQueryVO.getProductFunctionName():"-")
  134 + +"_"
  135 + +(StringUtils.isNotBlank(productQueryVO.getKeyword())?productQueryVO.getKeyword():"-");
94 136 Map res = (Map) productService.listBySimilar(productQueryVO).getData();
95 137 List<ProductDO> itemList = (List<ProductDO>) res.get("records");
96 138  
... ...
shop/src/main/java/com/canrd/shop/module/vo/ProductQueryVO.java
... ... @@ -45,5 +45,20 @@ public class ProductQueryVO extends BasePageVO implements Serializable {
45 45 */
46 46 private String productFunctionId;
47 47  
  48 + /**
  49 + * 顶级产品分类
  50 + */
  51 + private String rootProductCategoryName;
  52 +
  53 + /**
  54 + * 产品分类id
  55 + */
  56 + private String productCategoryName;
  57 +
  58 + /**
  59 + * 产品功能id
  60 + */
  61 + private String productFunctionName;
  62 +
48 63 }
49 64  
... ...
shop/src/main/resources/application-prod.yml
... ... @@ -68,4 +68,5 @@ logging:
68 68  
69 69 static-html:
70 70 product-list:
71   - url: /etc/nginx/canrud/products_html
72 71 \ No newline at end of file
  72 + url: /etc/nginx/canrud/products_html
  73 + secret: canrd@2024
73 74 \ No newline at end of file
... ...
shop/src/main/resources/application-test.yml
... ... @@ -69,4 +69,5 @@ logging:
69 69  
70 70 static-html:
71 71 product-list:
72   - url: E:\productList
73 72 \ No newline at end of file
  73 + url: E:\productList
  74 + secret: canrd@2024
74 75 \ No newline at end of file
... ...
shop/src/main/resources/application.yml
... ... @@ -8,8 +8,3 @@ spring:
8 8 throw-exception-if-no-handler-found: true
9 9 resources:
10 10 add-mappings: false
11   -static-html:
12   - product-list:
13   - urls:
14   - test: E:\productList
15   - prod: /etc/nginx/canrud/products_html
16 11 \ No newline at end of file
... ...