|
@@ -1,17 +1,17 @@
|
|
package io.github.nnkwrik.goodsservice.controller;
|
|
package io.github.nnkwrik.goodsservice.controller;
|
|
|
|
|
|
|
|
+import io.github.nnkwrik.common.dto.JWTUser;
|
|
import io.github.nnkwrik.common.dto.Response;
|
|
import io.github.nnkwrik.common.dto.Response;
|
|
|
|
+import io.github.nnkwrik.common.token.injection.JWT;
|
|
import io.github.nnkwrik.goodsservice.cache.SearchCache;
|
|
import io.github.nnkwrik.goodsservice.cache.SearchCache;
|
|
import io.github.nnkwrik.goodsservice.model.vo.SearchIndexPageVo;
|
|
import io.github.nnkwrik.goodsservice.model.vo.SearchIndexPageVo;
|
|
import io.github.nnkwrik.goodsservice.model.vo.inner.GoodsSimpleVo;
|
|
import io.github.nnkwrik.goodsservice.model.vo.inner.GoodsSimpleVo;
|
|
import io.github.nnkwrik.goodsservice.service.SearchService;
|
|
import io.github.nnkwrik.goodsservice.service.SearchService;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
-import org.springframework.util.StringUtils;
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
-import java.util.Map;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
* search相关的借口依赖openId,
|
|
* search相关的借口依赖openId,
|
|
@@ -32,42 +32,41 @@ public class SearchController {
|
|
private SearchService searchService;
|
|
private SearchService searchService;
|
|
|
|
|
|
|
|
|
|
- @PostMapping("/search/index")
|
|
|
|
- public Response<SearchIndexPageVo> searchIndex(@RequestBody Map<String, String> map) {
|
|
|
|
- String openId = map.get("openId");
|
|
|
|
|
|
+ @GetMapping("/search/index")
|
|
|
|
+ public Response<SearchIndexPageVo> searchIndex(@JWT JWTUser jwtUser) {
|
|
List<String> historyKeyword = null;
|
|
List<String> historyKeyword = null;
|
|
- if (!StringUtils.isEmpty(openId)) {
|
|
|
|
- historyKeyword = searchService.getUserHistory(openId);
|
|
|
|
|
|
+ if (jwtUser != null) {
|
|
|
|
+ historyKeyword = searchService.getUserHistory(jwtUser.getOpenId());
|
|
}
|
|
}
|
|
|
|
|
|
List<String> hotKeyword = searchCache.getHot(10);
|
|
List<String> hotKeyword = searchCache.getHot(10);
|
|
SearchIndexPageVo vo = new SearchIndexPageVo(historyKeyword, hotKeyword);
|
|
SearchIndexPageVo vo = new SearchIndexPageVo(historyKeyword, hotKeyword);
|
|
- log.info("用户openId= 【{}】获取搜索历史和热门关键词,搜索历史 = 【{}】,热门关键词 = 【{}】", openId, historyKeyword, hotKeyword);
|
|
|
|
|
|
+ log.info("用户openId= 【{}】获取搜索历史和热门关键词,搜索历史 = 【{}】,热门关键词 = 【{}】", jwtUser.getOpenId(), historyKeyword, hotKeyword);
|
|
|
|
|
|
return Response.ok(vo);
|
|
return Response.ok(vo);
|
|
}
|
|
}
|
|
|
|
|
|
- @PostMapping("/search/clearhistory")
|
|
|
|
- public Response clearHistory(@RequestBody Map<String, String> map) {
|
|
|
|
- String openId = map.get("openId");
|
|
|
|
- if (StringUtils.isEmpty(openId)) return Response.fail(Response.OPEN_ID_IS_EMPTY, "用户id为空,请登陆后再尝试");
|
|
|
|
- searchService.clearUserHistory(openId);
|
|
|
|
- log.info("用户openId= 【{}】清空搜索历史", openId);
|
|
|
|
|
|
+ @GetMapping("/search/clearhistory")
|
|
|
|
+ public Response clearHistory(@JWT JWTUser jwtUser) {
|
|
|
|
+ if (jwtUser == null) return Response.fail(Response.OPEN_ID_IS_EMPTY, "用户id为空,请登陆后再尝试");
|
|
|
|
+ searchService.clearUserHistory(jwtUser.getOpenId());
|
|
|
|
+ log.info("用户openId= 【{}】清空搜索历史", jwtUser.getOpenId());
|
|
return Response.ok();
|
|
return Response.ok();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
//TODO 后期提供排序和地区和卖家信用的筛选功能
|
|
//TODO 后期提供排序和地区和卖家信用的筛选功能
|
|
- @PostMapping("search/result/{keyword}")
|
|
|
|
|
|
+ @GetMapping("search/result/{keyword}")
|
|
public Response<List<GoodsSimpleVo>> searchGoods(@PathVariable("keyword") String keyword,
|
|
public Response<List<GoodsSimpleVo>> searchGoods(@PathVariable("keyword") String keyword,
|
|
@RequestParam(value = "page", defaultValue = "1") int page,
|
|
@RequestParam(value = "page", defaultValue = "1") int page,
|
|
@RequestParam(value = "limit", defaultValue = "10") int size,
|
|
@RequestParam(value = "limit", defaultValue = "10") int size,
|
|
- @RequestBody Map<String, String> map) {
|
|
|
|
- String openId = map.get("openId");
|
|
|
|
|
|
+ @JWT JWTUser jwtUser) {
|
|
List<GoodsSimpleVo> goodsListVo = searchService.searchByKeyword(keyword, page, size);
|
|
List<GoodsSimpleVo> goodsListVo = searchService.searchByKeyword(keyword, page, size);
|
|
|
|
|
|
//数据库改openid的搜索历史
|
|
//数据库改openid的搜索历史
|
|
- if (!StringUtils.isEmpty(openId)) {
|
|
|
|
|
|
+ String openId = null;
|
|
|
|
+ if (jwtUser != null) {
|
|
|
|
+ openId = jwtUser.getOpenId();
|
|
searchService.updateUserHistory(openId, keyword);
|
|
searchService.updateUserHistory(openId, keyword);
|
|
}
|
|
}
|
|
//加入热门搜索缓存
|
|
//加入热门搜索缓存
|