浏览代码

搜索关键词与菜单2

chao 4 年之前
父节点
当前提交
90e8068b1a

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

@@ -8,6 +8,8 @@ import org.springframework.context.annotation.Bean;
 import org.springframework.core.Ordered;
 import org.thymeleaf.spring5.view.ThymeleafViewResolver;
 
+import javax.annotation.Resource;
+
 /**
  * @author Charles
  */

+ 0 - 42
src/main/java/com/caimei/www/api/BaseApi.java

@@ -1,42 +0,0 @@
-package com.caimei.www.api;
-
-import com.caimei.module.base.entity.bo.JsonModel;
-import com.caimei.www.service.BaseService;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RestController;
-
-/**
- * Description
- *
- * @author : Charles
- * @date : 2020/6/16
- */
-@RestController
-public class BaseApi {
-
-    private BaseService baseService;
-    @Autowired
-    public void setBaseService(BaseService baseService) {
-        this.baseService = baseService;
-    }
-
-    /**
-     * 获取搜索热门关键字
-     */
-    @GetMapping("/search/hot/word")
-    public JsonModel hotWord() {
-        return baseService.getSearchHotWord();
-    }
-
-    /**
-     * 获取头部菜单
-     */
-    @GetMapping("/nav/menu")
-    public JsonModel navMenu() {
-        return baseService.getNavMenu();
-    }
-
-
-
-}

+ 45 - 0
src/main/java/com/caimei/www/controller/BaseController.java

@@ -0,0 +1,45 @@
+package com.caimei.www.controller;
+
+import com.caimei.module.base.entity.bo.JsonModel;
+import com.caimei.www.pojo.TopMenuVo;
+import com.caimei.www.service.BaseService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.ModelAttribute;
+
+import java.util.List;
+
+/**
+ * 页面全局变量
+ *
+ * @author : Charles
+ * @date : 2020/6/18
+ */
+@Controller
+public class BaseController {
+    @Value("${caimei.spiServer}")
+    private String spiServer;
+
+    private BaseService baseService;
+    @Autowired
+    public void setBaseService(BaseService baseService) {
+        this.baseService = baseService;
+    }
+
+	@ModelAttribute
+	public Model init(final Model model) {
+		// spi服务器地址
+		model.addAttribute("spiServer", spiServer);
+		// 搜索热门关键字
+		List<String> searchHotWord = baseService.getSearchHotWord();
+		model.addAttribute("searchHotWord", searchHotWord);
+		// 头部菜单
+		List<TopMenuVo> menuList = baseService.getNavMenu();
+		model.addAttribute("topMenuList", menuList);
+
+		return model;
+	}
+
+}

+ 6 - 1
src/main/java/com/caimei/www/controller/HomeController.java

@@ -3,6 +3,11 @@ package com.caimei.www.controller;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
 import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.ModelAttribute;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.thymeleaf.spring5.view.ThymeleafViewResolver;
+
+import javax.annotation.Resource;
 
 /**
  * Description
@@ -11,7 +16,7 @@ import org.springframework.web.bind.annotation.GetMapping;
  * @date : 2020/6/15
  */
 @Controller
-public class HomeController {
+public class HomeController extends BaseController {
 
 	private static final String HOME_PATH = "index";
 

+ 1 - 1
src/main/java/com/caimei/www/controller/ProductController.java

@@ -12,7 +12,7 @@ import org.springframework.web.bind.annotation.RequestParam;
  * @date : 2020/6/12
  */
 @Controller
-public class ProductController {
+public class ProductController extends BaseController {
 
 	private static final String PRODUCT_LIST_PATH = "product/list";
 	private static final String PRODUCT_DETAIL_PATH = "product/detail";

+ 5 - 3
src/main/java/com/caimei/www/service/BaseService.java

@@ -1,6 +1,8 @@
 package com.caimei.www.service;
 
-import com.caimei.module.base.entity.bo.JsonModel;
+import com.caimei.www.pojo.TopMenuVo;
+
+import java.util.List;
 
 /**
  * Description
@@ -13,10 +15,10 @@ public interface BaseService {
     /**
      * 获取搜索热门关键字
      */
-    JsonModel getSearchHotWord();
+    List<String> getSearchHotWord();
 
     /**
      * 获取头部菜单
      */
-    JsonModel getNavMenu();
+    List<TopMenuVo> getNavMenu();
 }

+ 5 - 5
src/main/java/com/caimei/www/service/impl/BaseServiceImpl.java

@@ -27,15 +27,15 @@ public class BaseServiceImpl implements BaseService {
      * 获取搜索热门关键字
      */
     @Override
-    public JsonModel getSearchHotWord() {
+    public List<String> getSearchHotWord() {
         List<String> hotSearch = baseDao.getSearchKeyword();
         if (!CollectionUtils.isEmpty(hotSearch) && hotSearch.size() > 8) {
             List<String> newList = hotSearch.stream()
                 .filter(str -> !StringUtils.isEmpty(str)).limit(8)
                 .collect(Collectors.toList());
-            return JsonModel.newInstance().success(newList);
+            return newList;
         } else {
-            return JsonModel.newInstance().success(hotSearch);
+            return hotSearch;
         }
     }
 
@@ -43,12 +43,12 @@ public class BaseServiceImpl implements BaseService {
      * 获取头部菜单
      */
     @Override
-    public JsonModel getNavMenu() {
+    public List<TopMenuVo> getNavMenu() {
         List<TopMenuVo> menuList = baseDao.getTopMenus();
         menuList.forEach(item -> {
             List<SubMenuVo> subList = baseDao.getSubMenus(item.getId());
             item.setSubMenus(subList);
         });
-        return JsonModel.newInstance().success(menuList);
+        return menuList;
     }
 }

+ 20 - 17
src/main/resources/static/js/base.js

@@ -1,21 +1,24 @@
-window.onload=function(){
-    if ($('#hotWord').length >0 ){
-        var hotWords = [];
-        $.get("/search/hot/word").done(function(r){
+/*
+var topMenu = new Vue({
+    el: '#topMenu',
+    data: {
+        menuList: [],
+        classify: []
+    },
+    methods: {},
+    created: function () {
+        var _self = this;
+        $.getJSON("/nav/menu").done(function(r){
             if (r.code === 0 && r.data) {
-                hotWords = r.data;
-                var html = '';
-                for (var i=0; i<hotWords.length; i++){
-                    if (i>0) {
-                        html += '/';
-                    }
-                    html += '<a href="/product/search.shtml?keyword='+ hotWords[i] +'" target="_blank">'+ hotWords[i] +'</a>';
-                }
-                $('#hotWord').html(html);
+                _self.menuList = r.data;
+            }
+        });
+        $.getJSON(this.requestUrl+"/product/classify").done(function (r) {
+            if (r.code === 0 && r.data) {
+                _self.classify = r.data;
             }
         });
     }
-};
-
-
-
+});
+*/
+var spiServer = $("#spiServer").val();

+ 42 - 16
src/main/resources/templates/components/header.html

@@ -29,25 +29,51 @@
         </li>
     </ul>
 
-    <nav>
-        <a href="/" class="logo">
-            <img src="/img/logo.png" alt="采美"/>
-            <span>生美/医美采购服务平台</span>
-        </a>
-        <div class="search">
+    <div>
+        <div>
+            <a href="/" class="logo">
+                <img src="/img/logo.png" alt="采美"/>
+                <span>生美/医美采购服务平台</span>
+            </a>
+            <div class="search">
+                <div>
+                    <span class="current" data-id="0">产品</span>
+                    <span data-id="1">供应商</span>
+                    <span data-id="2">项目仪器</span>
+                </div>
+                <label>
+                    <input type="text" placeholder="请输入关键字" th:value="${path}" />
+                    <a href="javascript:void(0);">搜索</a>
+                </label>
+                <!-- 热门搜索关键词 -->
+                <div>
+                    <span th:each="word,wordStat: ${searchHotWord}">
+                        <i th:if="${wordStat.index}>0">/</i>
+                        <a th:href="@{/product/search.shtml( keyword=${word} )}" target="_blank" th:text="${word}"></a>
+                    </span>
+                </div>
+            </div>
+        </div>
+        <div id="topMenu">
             <div>
-                <span class="current" data-id="0">产品</span>
-                <span data-id="1">供应商</span>
-                <span data-id="2">项目仪器</span>
+                <a href="javascript:void(0);">商品分类</a>
+                <div>
+
+                </div>
             </div>
-            <label>
-                <input type="text" placeholder="请输入关键字" th:value="${path}" />
-                <a href="javascript:void(0);">搜索</a>
-            </label>
-            <!-- 热门搜索关键词 -->
-            <div id="hotWord"></div>
+            <nav>
+                <ul>
+                    <li th:each="menu: ${topMenuList}">
+                        <a href="javascript:void(0);" th:text="${menu.name}"></a>
+                        <div th:if="${not #lists.isEmpty(menu.subMenus)}">
+                            <a th:each="sub: ${menu.subMenus}" href="javascript:void(0);" th:text="${sub.name}"></a>
+                        </div>
+                    </li>
+                </ul>
+            </nav>
         </div>
-    </nav>
+    </div>
+    <input type="hidden" th:value="${spiServer}" id="spiServer">
 <style>
     ul{
         width:1200px;

+ 6 - 2
src/main/resources/templates/index.html

@@ -6,8 +6,6 @@
           content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
     <meta http-equiv="X-UA-Compatible" content="ie=edge">
     <title>Document</title>
-    <script type="text/javascript" src="/lib/jquery-3.5.1.js"></script>
-    <script type="text/javascript" src="/js/base.js"></script>
 </head>
 <body>
     <!-- 引用头部 -->
@@ -15,8 +13,14 @@
     <div class="container">
         欢迎
         <p th:text="${msg}"></p>
+        <p th:text="${spiUrl}"></p>
     </div>
     <!-- 引入底部 -->
     <div th:replace="components/footer"></div>
+
+
+    <script type="text/javascript" src="/lib/jquery-3.5.1.js"></script>
+    <script type="text/javascript" src="/lib/vue2.6.11.js"></script>
+    <script type="text/javascript" src="/js/base.js"></script>
 </body>
 </html>