浏览代码

商品分类SEO优化

chao 3 年之前
父节点
当前提交
665fbee4a3
共有 1 个文件被更改,包括 155 次插入3 次删除
  1. 155 3
      src/main/java/com/caimei/www/controller/unlimited/ProductController.java

+ 155 - 3
src/main/java/com/caimei/www/controller/unlimited/ProductController.java

@@ -1,6 +1,7 @@
 package com.caimei.www.controller.unlimited;
 
 import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
 import com.caimei.www.controller.BaseController;
 import com.caimei.www.pojo.page.ProductDetail;
 import com.caimei.www.service.page.ProductService;
@@ -10,8 +11,10 @@ import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
+import org.thymeleaf.util.StringUtils;
 
-import java.util.Map;
+import java.util.*;
+import java.util.concurrent.atomic.AtomicReference;
 
 /**
  * 商品(product)
@@ -32,6 +35,8 @@ public class ProductController extends BaseController {
     private static final String ACTIVITY_TOP_PATH = "activity/activityTopic";
     private static final String CLOUD_BEAUTY_PATH = "activity/beautyTopic";
     private static final String ACTIVITY_COUPON_EXP = "activity/couponExp";
+    /** 优惠券活动商品列表 */
+    private static final String PRODUCT_COUPON = "product/product-coupon";
     /**
      * 美博会专题页
      */
@@ -59,6 +64,12 @@ public class ProductController extends BaseController {
         return PRODUCT_LIST_PATH;
     }
 
+    /**
+     * 优惠券活动商品列表
+     */
+    @GetMapping("/product/product-coupon.html")
+    public String productCoupon() { return PRODUCT_COUPON; }
+
     /**
      * 促销活动商品列表
      */
@@ -107,8 +118,149 @@ public class ProductController extends BaseController {
     /**
      * 商品分类列表页面
      */
-    @GetMapping("/product/classify{ids}.html")
-    public String instruelist(@PathVariable("ids") String ids) {
+    @GetMapping("/product/classify-{ids}.html")
+    public String instruelist(final Model model, @PathVariable("ids") String ids) {
+        String[] split = ids.split("-");
+        // 分类类型:1产品,2仪器,0全部
+        Integer typeSort =  split.length>=1 ? Integer.valueOf(split[0]) : 0;
+        model.addAttribute("typeSort", typeSort);
+        // 一级分类Id
+        Integer bigTypeId = split.length>=2 ? Integer.valueOf(split[1]) : 0;
+        model.addAttribute("bigTypeId", bigTypeId);
+        // 二级分类Id
+        Integer smallTypeId = split.length>=3 ? Integer.valueOf(split[2]) : 0;
+        model.addAttribute("smallTypeId", smallTypeId);
+        // 三级级分类Id
+        Integer tinyTypeId = split.length>=4 ? Integer.valueOf(split[3]) : 0;
+        model.addAttribute("tinyTypeId", tinyTypeId);
+        // 页码
+        Integer pageNum = split.length>=5 ? Integer.valueOf(split[4]) : 1;
+        // 每页数量
+        Integer pageSize = split.length>=6 ? Integer.valueOf(split[5]) : 24;
+        // 排序字段:价格price,销量sales,人气favorite
+        String sortField = split.length>=7 ? split[6] : "";
+        // 排序规则:1降序,其他升序
+        Integer sortType = split.length>=8 ? Integer.valueOf(split[7]) : 1;
+        /*
+         * 获取一级分类
+         */
+        JSONArray bigTypeArr = productService.getBigTypeJson(typeSort);
+        model.addAttribute("bigTypeJson", bigTypeArr);
+        AtomicReference<String> bigTypeName = new AtomicReference<>("");
+        bigTypeArr.forEach(temp -> {
+            JSONObject bigType = (JSONObject) temp;
+            if (null != bigType && bigTypeId.equals(bigType.getInteger("bigTypeId"))) {
+                bigTypeName.set(bigType.getString("name"));
+            }
+        });
+        model.addAttribute("bigTypeName", bigTypeName.get());
+        /*
+         * 获取二级分类
+         */
+        JSONArray smallTypeArr = productService.getSmallTypeJson(bigTypeId);
+        model.addAttribute("smallTypeJson", smallTypeArr);
+        AtomicReference<String> smallTypeName = new AtomicReference<>("");
+        smallTypeArr.forEach(temp -> {
+            JSONObject smallType = (JSONObject) temp;
+            if (null != smallType && smallTypeId.equals(smallType.getInteger("smallTypeId"))) {
+                smallTypeName.set(smallType.getString("name"));
+            }
+        });
+        model.addAttribute("smallTypeName", smallTypeName.get());
+        /*
+         * 获取三级分类
+         */
+        JSONArray tinyTypeArr = productService.getTinyTypeJson(smallTypeId);
+        model.addAttribute("tinyTypeJson", tinyTypeArr);
+        AtomicReference<String> tinyTypeName = new AtomicReference<>("");
+        tinyTypeArr.forEach(temp -> {
+            JSONObject tinyType = (JSONObject) temp;
+            if (null != tinyType && tinyTypeId.equals(tinyType.getInteger("tinyTypeId"))) {
+                tinyTypeName.set(tinyType.getString("name"));
+            }
+        });
+        model.addAttribute("tinyTypeName", tinyTypeName.get());
+        /*
+         * 获取商品信息
+         */
+        StringBuilder productParams = new StringBuilder();
+        if (tinyTypeId > 0) {
+            productParams.append("?id=").append(tinyTypeId).append("&idType=3");
+        } else if (smallTypeId > 0) {
+            productParams.append("?id=").append(smallTypeId).append("&idType=2");
+        } else {
+            productParams.append("?id=").append(bigTypeId).append("&idType=1");
+        }
+        productParams.append("&pageNum=").append(pageNum).append("&pageSize=").append(pageSize).append("&sortField=").append(sortField).append("&sortType=").append(sortType);
+        JSONObject productObj = productService.getProductListJson(productParams.toString());
+        Integer totalCount = productObj.getInteger("total");
+        JSONArray productList = (JSONArray) productObj.get("items");
+        model.addAttribute("productCount", totalCount);
+        model.addAttribute("productListJson", productList);
+
+        int totalPage = (int) Math.ceil((double) totalCount/pageSize);
+        totalPage = totalPage > 0 ? totalPage : 1;
+        int[] arr = null;
+        if (totalPage <= 6) {
+            int[] tmp = new int[]{1, 2, 3, 4, 5, 6};
+            arr = Arrays.copyOf(tmp, totalPage);
+        }else if (pageNum <= 3) {
+            arr = new int[]{1, 2, 3, 4, 5, 0, totalPage};
+        }else if (pageNum >= totalPage - 2) {
+            arr = new int[]{1, 0, totalPage - 4, totalPage - 3, totalPage - 2, totalPage - 1, totalPage};
+        }else{
+            arr = new int[]{1, 0, pageNum - 2, pageNum - 1, pageNum, pageNum + 1, pageNum + 2, 0, totalPage};
+        }
+
+        List<Map<String,Object>> arrPath = new ArrayList<>();
+
+        final String basePath = "/product/classify-" + typeSort + "-" + bigTypeId + "-" + smallTypeId + "-" + tinyTypeId;
+        if (pageNum > 1) {
+            StringBuilder prevPath = new StringBuilder(basePath);
+            prevPath.append("-").append(pageNum-1).append("-").append(pageSize);
+            if (!StringUtils.isEmpty(sortField)) {
+                prevPath.append("-").append(sortField).append("-").append(sortType).append(".html");
+            } else {
+                prevPath.append(".html");
+            }
+            Map<String,Object> tempPath = new HashMap<>();
+            tempPath.put("btn", -1);
+            tempPath.put("path", prevPath);
+            arrPath.add(tempPath);
+        }
+        for (int j : arr) {
+            Map<String,Object> tempPath = new HashMap<>();
+            tempPath.put("btn", j);
+            if (j > 0) {
+                StringBuilder btnPath = new StringBuilder(basePath);
+                btnPath.append("-").append(j).append("-").append(pageSize);
+                if (!StringUtils.isEmpty(sortField)) {
+                    btnPath.append("-").append(sortField).append("-").append(sortType).append(".html");
+                } else {
+                    btnPath.append(".html");
+                }
+                tempPath.put("path", btnPath);
+            } else {
+                tempPath.put("path", "");
+            }
+            arrPath.add(tempPath);
+        }
+        if (pageNum < totalPage) {
+            StringBuilder nextPath = new StringBuilder(basePath);
+            nextPath.append("-").append(pageNum+1).append("-").append(pageSize);
+            if (!StringUtils.isEmpty(sortField)) {
+                nextPath.append("-").append(sortField).append("-").append(sortType).append(".html");
+            } else {
+                nextPath.append(".html");
+            }
+            Map<String,Object> tempPath = new HashMap<>();
+            tempPath.put("btn", -2);
+            tempPath.put("path", nextPath);
+            arrPath.add(tempPath);
+        }
+        model.addAttribute("pageBtnNum", pageNum);
+        model.addAttribute("pageBtnList", arrPath);
+
         return INSTRUMENT_LIST_PATH;
     }