Prechádzať zdrojové kódy

:honey_pot: 商品服务,把获取openId的方式从json改为jwt

nnkwrik 6 rokov pred
rodič
commit
2eac708aac

+ 5 - 2
common/src/main/java/io/github/nnkwrik/common/token/injection/JWT.java

@@ -1,6 +1,9 @@
 package io.github.nnkwrik.common.token.injection;
 
-import java.lang.annotation.*;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
 
 /**
  * @author nnkwrik
@@ -10,5 +13,5 @@ import java.lang.annotation.*;
 @Retention(RetentionPolicy.RUNTIME)
 public @interface JWT {
 
-    boolean required() default true;
+    boolean required() default false;
 }

+ 1 - 2
common/src/main/java/io/github/nnkwrik/common/token/injection/JWTResolver.java

@@ -60,8 +60,7 @@ public class JWTResolver implements HandlerMethodArgumentResolver {
             }
         }
 
-        if (user == null &&
-                parameter.getParameterAnnotation(JWT.class).required()) {
+        if (user == null && parameter.getParameterAnnotation(JWT.class).required()) {
             if (isExpired) throw new JWTException(JWTException.TOKEN_IS_EXPIRED, "凭证已过期");
             throw new JWTException(JWTException.TOKEN_IS_EMPTY, "用户的Authorization头错误,无法获取jwt");
         }

+ 7 - 5
goods-service/src/main/java/io/github/nnkwrik/goodsservice/controller/GoodsController.java

@@ -1,6 +1,8 @@
 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.token.injection.JWT;
 import io.github.nnkwrik.goodsservice.model.vo.CategoryPageVo;
 import io.github.nnkwrik.goodsservice.model.vo.GoodsDetailPageVo;
 import io.github.nnkwrik.goodsservice.model.vo.GoodsRelatedVo;
@@ -56,9 +58,9 @@ public class GoodsController {
 
     }
 
-    @PostMapping("/goods/detail/{goodsId}")
+    @GetMapping("/goods/detail/{goodsId}")
     public Response<GoodsDetailPageVo> getGoodsDetail(@PathVariable("goodsId") int goodsId,
-                                                      @RequestBody Map<String, String> json) {
+                                                      @JWT JWTUser jwtUser) {
         //更新浏览次数
         GoodsDetailVo goodsDetail = goodsService.getGoodsDetail(goodsId);
         if (goodsDetail == null) {
@@ -69,10 +71,10 @@ public class GoodsController {
         List<CommentVo> comment = goodsService.getGoodsComment(goodsId);
 
         boolean userHasCollect = false;
-        if (json.get("openId") != null)
-            userHasCollect = goodsService.userHasCollect(json.get("openId"), goodsId);
+        if (jwtUser != null)
+            userHasCollect = goodsService.userHasCollect(jwtUser.getOpenId(), goodsId);
         GoodsDetailPageVo vo = new GoodsDetailPageVo(goodsDetail, goodsGallery, comment, userHasCollect);
-        log.debug("浏览商品详情 : {}", vo);
+        log.debug("浏览商品详情 : 商品id={},商品名={}", vo.getInfo().getId(),vo.getInfo().getName());
 
         return Response.ok(vo);
     }

+ 17 - 18
goods-service/src/main/java/io/github/nnkwrik/goodsservice/controller/SearchController.java

@@ -1,17 +1,17 @@
 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.token.injection.JWT;
 import io.github.nnkwrik.goodsservice.cache.SearchCache;
 import io.github.nnkwrik.goodsservice.model.vo.SearchIndexPageVo;
 import io.github.nnkwrik.goodsservice.model.vo.inner.GoodsSimpleVo;
 import io.github.nnkwrik.goodsservice.service.SearchService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.util.StringUtils;
 import org.springframework.web.bind.annotation.*;
 
 import java.util.List;
-import java.util.Map;
 
 /**
  * search相关的借口依赖openId,
@@ -32,42 +32,41 @@ public class SearchController {
     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;
-        if (!StringUtils.isEmpty(openId)) {
-            historyKeyword = searchService.getUserHistory(openId);
+        if (jwtUser != null) {
+            historyKeyword = searchService.getUserHistory(jwtUser.getOpenId());
         }
 
         List<String> hotKeyword = searchCache.getHot(10);
         SearchIndexPageVo vo = new SearchIndexPageVo(historyKeyword, hotKeyword);
-        log.info("用户openId= 【{}】获取搜索历史和热门关键词,搜索历史 = 【{}】,热门关键词 = 【{}】", openId, historyKeyword, hotKeyword);
+        log.info("用户openId= 【{}】获取搜索历史和热门关键词,搜索历史 = 【{}】,热门关键词 = 【{}】", jwtUser.getOpenId(), historyKeyword, hotKeyword);
 
         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();
     }
 
 
     //TODO 后期提供排序和地区和卖家信用的筛选功能
-    @PostMapping("search/result/{keyword}")
+    @GetMapping("search/result/{keyword}")
     public Response<List<GoodsSimpleVo>> searchGoods(@PathVariable("keyword") String keyword,
                                                      @RequestParam(value = "page", defaultValue = "1") int page,
                                                      @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);
 
         //数据库改openid的搜索历史
-        if (!StringUtils.isEmpty(openId)) {
+        String openId = null;
+        if (jwtUser != null) {
+            openId = jwtUser.getOpenId();
             searchService.updateUserHistory(openId, keyword);
         }
         //加入热门搜索缓存

+ 0 - 1
goods-service/src/main/java/io/github/nnkwrik/goodsservice/service/impl/GoodsServiceImpl.java

@@ -119,7 +119,6 @@ public class GoodsServiceImpl implements GoodsService {
         Set<String> userIdSet = new HashSet<>();
         List<CommentVo> voList = baseComment.stream()
                 .map(base -> {
-                    System.out.println(base);
                     CommentVo baseVo = PO2VO.convert(PO2VO.comment, base);
                     userIdSet.add(baseVo.getUser_id());
                     return baseVo;