Sfoglia il codice sorgente

项目仪器页面静态化

chao 4 anni fa
parent
commit
7e65dc3127

+ 9 - 3
src/main/java/com/caimei/www/controller/GenerateApi.java

@@ -1,10 +1,7 @@
 package com.caimei.www.controller;
 
 import com.caimei.www.mapper.ProductDao;
-import com.caimei.www.pojo.JsonModel;
 import com.caimei.www.service.generate.GenerateHtml;
-import com.caimei.www.utils.GetProductImgUtil;
-import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.server.ServerWebExchange;
@@ -32,6 +29,15 @@ public class GenerateApi {
         return generateHtml.generateStaticHome(exchange);
     }
 
+    /**
+     * 生成产品仪器页面
+     * @param pageId 页面Id
+     */
+    @PostMapping("/generate/product/type")
+    public  String generateProductType(ServerWebExchange exchange, Integer pageId) {
+        return generateHtml.generateProductType(exchange, pageId);
+    }
+
     /**
      * 拷贝静态资源文件
      */

+ 8 - 0
src/main/java/com/caimei/www/controller/unlimited/ProductController.java

@@ -10,6 +10,8 @@ import org.springframework.ui.Model;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
 
+import java.util.Map;
+
 /**
  * 商品(product)
  *
@@ -88,6 +90,12 @@ public class ProductController extends BaseController {
         String title = singlePageService.getPageTitleById(id);
         model.addAttribute("pageId", id);
         model.addAttribute("pageTitle", title);
+        // 产品仪器一级分类
+        Map<String, Object> classifyMap = productService.getTypeClassifyJson(pageId);
+        // 产品仪器楼层数据
+        Map<String, Object> floorMap = productService.getTypeFloorJson(pageId);
+        model.addAttribute("bigTypeJson", classifyMap);
+        model.addAttribute("typeFloorJson", floorMap);
         return INSTRUMENT_PAGE_PATH;
     }
 

+ 2 - 0
src/main/java/com/caimei/www/mapper/ProductDao.java

@@ -71,4 +71,6 @@ public interface ProductDao {
      * 修复商品图片(临时)
      */
     List<String> getProductInfo();
+
+    Integer getPageTypeSort(Integer pageId);
 }

+ 5 - 2
src/main/java/com/caimei/www/service/generate/GenerateHtml.java

@@ -16,11 +16,14 @@ public interface GenerateHtml {
     String generateStaticHome(ServerWebExchange exchange);
 
     /**
-     * 删除静态首页
+     * 生成产品仪器页面
+     * @param pageId 页面Id
      */
-    String deleteStaticHome(String pageName);
+    String generateProductType(ServerWebExchange exchange, Integer pageId);
+
     /**
      * 拷贝静态资源文件
      */
     String generateStaticFiles();
+
 }

+ 74 - 16
src/main/java/com/caimei/www/service/generate/impl/GenerateHtmlImpl.java

@@ -7,6 +7,8 @@ import com.caimei.www.pojo.page.TopMenu;
 import com.caimei.www.service.generate.GenerateHtml;
 import com.caimei.www.service.page.BaseService;
 import com.caimei.www.service.page.HomeService;
+import com.caimei.www.service.page.ProductService;
+import com.caimei.www.service.page.SinglePageService;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.io.FileUtils;
 import org.springframework.beans.factory.annotation.Value;
@@ -54,6 +56,10 @@ public class GenerateHtmlImpl implements GenerateHtml {
     private BaseService baseService;
     @Resource
     private HomeService homeService;
+    @Resource
+    private SinglePageService singlePageService;
+    @Resource
+    private ProductService productService;
     /**
      * 生成静态首页
      */
@@ -81,19 +87,30 @@ public class GenerateHtmlImpl implements GenerateHtml {
     }
 
     /**
-     * 删除静态页
+     * 生成产品仪器页面
+     * @param pageId 页面Id
      */
     @Override
-    public String deleteStaticHome(String pageName) {
-        // 输出流
-        File dest = new File(destPath, pageName + ".html");
+    public String generateProductType(ServerWebExchange exchange, Integer pageId){
+        // 上下文
+        SpringWebFluxContext context = new SpringWebFluxContext(exchange);
+        // 设置页面数据
+        context.setVariables(setStaticProductType(pageId));
+        // 输出流  /product/type-287.html
+        File dest = new File(destPath + "/product", "type-"+pageId+".html");
         if (dest.exists()) {
             boolean delete = dest.delete();
-            if (delete) {
-                return "[静态页服务]:删除静态页成功! ^_^";
-            }
         }
-        return "[静态页服务]:删除静态页失败!";
+        try (PrintWriter writer = new PrintWriter(dest, "UTF-8")) {
+            // 生成html
+            templateEngine.process("index", context, writer);
+            log.info("[静态页服务]:生成静态产品仪器页("+pageId+")成功! ^_^");
+            return "[静态页服务]:生成静态产品仪器页("+pageId+")成功! ^_^";
+        } catch (Exception e) {
+            boolean delete = dest.delete();
+            log.error("[静态页服务]:生成静态产品仪器页("+pageId+")异常!", e);
+            return "[静态页服务]:生成静态产品仪器页("+pageId+")异常!"+e.toString();
+        }
     }
 
     /**
@@ -116,9 +133,7 @@ public class GenerateHtmlImpl implements GenerateHtml {
      * 设置静态首页数据
      */
     private Map<String, Object> setStaticHomeData() {
-        // 这里加载的数据是从数据库查询出来的,demo就写固定了
         Map<String, Object> map = new HashMap<>();
-
 		// 环境变量,(2:正式环境,1:测试环境,0:开发环境)
 	    map.put("siteEnv", siteEnv);
 		map.put("agent", "");
@@ -136,6 +151,13 @@ public class GenerateHtmlImpl implements GenerateHtml {
 		// 分类菜单
 		List<JSONObject> classifyList = baseService.getClassifyJson();
 		map.put("classifyJson", classifyList);
+        // 底部帮助页
+        List<BaseLink> helpPages = baseService.getHelpPages();
+        map.put("helpPages", helpPages);
+        // 友情链接
+        List<BaseLink> friendLinks = baseService.getFriendLinks();
+        map.put("friendLinks", friendLinks);
+
 		// 获取banner图
 		List<ImageLink> bannerList = homeService.getHomeBanners();
 		map.put("bannerList", bannerList);
@@ -145,12 +167,48 @@ public class GenerateHtmlImpl implements GenerateHtml {
 		// 右侧侧边栏数据
         Map<String, Object> sideMap = homeService.getHomeSideJson();
 		map.put("sideJson", sideMap);
-		// 底部帮助页
-		List<BaseLink> helpPages = baseService.getHelpPages();
-		map.put("helpPages", helpPages);
-		// 友情链接
-		List<BaseLink> friendLinks = baseService.getFriendLinks();
-		map.put("friendLinks", friendLinks);
+
+        log.debug(map.toString());
+
+        return map;
+    }
+
+
+    private Map<String, Object> setStaticProductType(Integer pageId) {
+        Map<String, Object> map = new HashMap<>();
+        // 环境变量,(2:正式环境,1:测试环境,0:开发环境)
+        map.put("siteEnv", siteEnv);
+        map.put("agent", "");
+        // 静态文件版本号
+        map.put("version", buildTime);
+        // spi服务器地址
+        map.put("coreServer", coreServer);
+        map.put("spiServer", spiServer);
+        // 搜索热门关键字
+        List<String> searchHotWord = baseService.getSearchHotWord();
+        map.put("searchHotWord", searchHotWord);
+        // 头部菜单
+        List<TopMenu> menuList = baseService.getNavMenu();
+        map.put("topMenuList", menuList);
+        // 分类菜单
+        List<JSONObject> classifyList = baseService.getClassifyJson();
+        map.put("classifyJson", classifyList);
+        // 底部帮助页
+        List<BaseLink> helpPages = baseService.getHelpPages();
+        map.put("helpPages", helpPages);
+        // 友情链接
+        List<BaseLink> friendLinks = baseService.getFriendLinks();
+        map.put("friendLinks", friendLinks);
+
+        String title = singlePageService.getPageTitleById(pageId);
+        map.put("pageId", pageId);
+        map.put("pageTitle", title);
+        // 产品仪器一级分类
+        Map<String, Object> classifyMap = productService.getTypeClassifyJson(pageId);
+        // 产品仪器楼层数据
+        Map<String, Object> floorMap = productService.getTypeFloorJson(pageId);
+        map.put("bigTypeJson", classifyMap);
+        map.put("typeFloorJson", floorMap);
 
         log.debug(map.toString());
 

+ 13 - 0
src/main/java/com/caimei/www/service/page/ProductService.java

@@ -2,6 +2,8 @@ package com.caimei.www.service.page;
 
 import com.caimei.www.pojo.page.ProductDetail;
 
+import java.util.Map;
+
 
 /**
  * Description
@@ -17,4 +19,15 @@ public interface ProductService {
      */
     ProductDetail getProductDetailById(Integer productId);
 
+    /**
+     * 产品仪器一级分类
+     * @param pageId
+     */
+    Map<String, Object> getTypeClassifyJson(Integer pageId);
+
+    /**
+     * 产品仪器楼层数据
+     * @param pageId
+     */
+    Map<String, Object> getTypeFloorJson(Integer pageId);
 }

+ 44 - 0
src/main/java/com/caimei/www/service/page/impl/ProductServiceImpl.java

@@ -1,16 +1,19 @@
 package com.caimei.www.service.page.impl;
 
+import com.alibaba.fastjson.JSONObject;
 import com.caimei.www.mapper.ProductDao;
 import com.caimei.www.pojo.page.ProductDetail;
 import com.caimei.www.service.page.ProductService;
 import com.caimei.www.utils.ImageUtil;
 import com.caimei.www.utils.PriceUtil;
+import com.caimei.www.utils.RequestUtil;
 import io.netty.util.internal.StringUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
+import java.util.Map;
 
 
 /**
@@ -24,6 +27,8 @@ import javax.annotation.Resource;
 public class ProductServiceImpl implements ProductService {
     @Value("${caimei.wwwDomain}")
     private String domain;
+    @Value("${caimei.coreServer}")
+    private String coreServer;
     @Resource
     private ProductDao productDao;
 
@@ -48,4 +53,43 @@ public class ProductServiceImpl implements ProductService {
         return product;
     }
 
+    /**
+     * 产品仪器一级分类
+     *
+     * @param pageId
+     */
+    @Override
+    public Map<String, Object> getTypeClassifyJson(Integer pageId) {
+        Integer typeSort = productDao.getPageTypeSort(pageId);
+        String dataUrl = coreServer+"/commodity/classify?typeSort="+typeSort+"&source=www";
+        try {
+            String classifyResult = RequestUtil.sendGet(dataUrl);
+            log.debug(classifyResult);
+            Map<String, Object> classifyMap = JSONObject.parseObject(classifyResult, Map.class);
+            return JSONObject.parseObject(String.valueOf(classifyMap.get("data")), Map.class);
+        } catch (Exception e) {
+            log.error("try-catch:",e);
+            return null;
+        }
+    }
+
+    /**
+     * 产品仪器楼层数据
+     *
+     * @param pageId
+     */
+    @Override
+    public Map<String, Object> getTypeFloorJson(Integer pageId) {
+        String dataUrl = coreServer+"/commodity/classify/product?pageId="+ pageId +"&source=1";
+        try {
+            String floorResult = RequestUtil.sendGet(dataUrl);
+            log.debug(floorResult);
+            Map<String, Object> floorMap = JSONObject.parseObject(floorResult, Map.class);
+            return JSONObject.parseObject(String.valueOf(floorMap.get("data")), Map.class);
+        } catch (Exception e) {
+            log.error("try-catch:",e);
+            return null;
+        }
+    }
+
 }

+ 4 - 1
src/main/resources/mapper/ProductMapper.xml

@@ -120,6 +120,9 @@
     <select id="getProductInfo" resultType="java.lang.String">
         select detailinfo from productdetailinfo where detailinfo like '%img-b.caimei365.com%'
 	</select>
-
+    <select id="getPageTypeSort" resultType="java.lang.Integer">
+		select typeSort from cm_page
+		where id = #{pageId} and enabledStatus = '1'
+	</select>
 
 </mapper>