Bläddra i källkod

修改返回类型并增加校验

xueyiming 3 månader sedan
förälder
incheckning
dc88bee68c

+ 5 - 4
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/video/MatchVideoService.java

@@ -1,5 +1,6 @@
 package com.tzld.longarticle.recommend.server.service.video;
 
+import com.alibaba.fastjson.JSONObject;
 import com.tzld.longarticle.recommend.server.common.response.CommonResponse;
 import com.tzld.longarticle.recommend.server.model.param.ChooseMinigramParam;
 import com.tzld.longarticle.recommend.server.model.param.GetOffVideosParam;
@@ -9,11 +10,11 @@ import com.tzld.longarticle.recommend.server.model.param.SearchVideoParam;
 import java.io.UnsupportedEncodingException;
 
 public interface MatchVideoService {
-    String searchVideo(SearchVideoParam searchVideoParam);
+    JSONObject searchVideo(SearchVideoParam searchVideoParam);
 
-    String chooseMinigram(ChooseMinigramParam chooseMinigramParam);
+    JSONObject chooseMinigram(ChooseMinigramParam chooseMinigramParam);
 
-    String getOffVideos(GetOffVideosParam getOffVideosParam);
+    JSONObject getOffVideos(GetOffVideosParam getOffVideosParam);
 
-    String recallVideos(RecallVideosParam recallVideosParam) throws UnsupportedEncodingException;
+    JSONObject recallVideos(RecallVideosParam recallVideosParam) throws UnsupportedEncodingException;
 }

+ 22 - 15
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/video/impl/MatchVideoServiceImpl.java

@@ -73,7 +73,7 @@ public class MatchVideoServiceImpl implements MatchVideoService {
 
     @Transactional
     @Override
-    public String searchVideo(SearchVideoParam searchVideoParam) {
+    public JSONObject searchVideo(SearchVideoParam searchVideoParam) {
         JSONObject res = new JSONObject();
         if (StringUtils.isEmpty(searchVideoParam.getContent())
                 || StringUtils.isEmpty(searchVideoParam.getArticleId())
@@ -83,7 +83,7 @@ public class MatchVideoServiceImpl implements MatchVideoService {
             res.put("code", 1);
             res.put("status", "fail");
             res.put("info", "params check error");
-            return res.toJSONString();
+            return res;
         }
         String traceId = String.format("search-%s-%s", UUID.randomUUID(), System.currentTimeMillis() / 1000);
         LongArticlesMatchVideo longArticlesMatchVideo = new LongArticlesMatchVideo();
@@ -120,13 +120,20 @@ public class MatchVideoServiceImpl implements MatchVideoService {
         res.put("code", 0);
         res.put("status", "success input to article queue");
         res.put("traceId", traceId);
-        return res.toJSONString();
+        return res;
     }
 
     @Override
-    public String chooseMinigram(ChooseMinigramParam chooseMinigramParam) {
-        String ghId = chooseMinigramParam.getGhId();
+    public JSONObject chooseMinigram(ChooseMinigramParam chooseMinigramParam) {
         JSONObject res = new JSONObject();
+        if (StringUtils.isEmpty(chooseMinigramParam.getTraceId())
+                || StringUtils.isEmpty(chooseMinigramParam.getBusinessType())
+                || StringUtils.isEmpty(chooseMinigramParam.getMiniCode())) {
+            res.put("status", "fail");
+            res.put("code", 1);
+            res.put("info", "params check error");
+        }
+        String ghId = chooseMinigramParam.getGhId();
         if (ghId != null && EXPERIMENT_27_GH_ID_SET.contains(ghId)) {
             res.put("programAvatar", MinigramEnum.XYMHFQDD_27.getAvatar());
             res.put("programId", MinigramEnum.XYMHFQDD_27.getId());
@@ -143,19 +150,19 @@ public class MatchVideoServiceImpl implements MatchVideoService {
             res.put("programName", MinigramEnum.VLOG0.getName());
             res.put("trace_id", chooseMinigramParam.getTraceId());
         }
-        return res.toJSONString();
+        return res;
     }
 
     @Override
-    public String getOffVideos(GetOffVideosParam getOffVideosParam) {
+    public JSONObject getOffVideos(GetOffVideosParam getOffVideosParam) {
         JSONObject res = new JSONObject();
         if (StringUtils.isEmpty(getOffVideosParam.getPushType()) || StringUtils.isEmpty(getOffVideosParam.getTraceId())) {
             res.put("error", "params error");
-            return res.toJSONString();
+            return res;
         }
         if (!getOffVideosParam.getTraceId().startsWith("search")) {
             res.put("msg", "do not process trace id");
-            return res.toJSONString();
+            return res;
         }
         String traceId = getOffVideosParam.getTraceId();
         LongArticlesMatchVideo matchVideo = longArticlesMatchVideoRepository.getByTraceId(traceId);
@@ -165,7 +172,7 @@ public class MatchVideoServiceImpl implements MatchVideoService {
             param.put("traceId", getOffVideosParam.getTraceId());
             HttpResponseContent httpResponseContent = HttpClientUtils.postData(url, param.toJSONString(),
                     HttpClientUtils.contentTypeJson);
-            return httpResponseContent.getBodyContent();
+            return JSONObject.parseObject(httpResponseContent.getBodyContent());
         }
         String ghId = matchVideo.getGhId();
         String response = matchVideo.getResponse();
@@ -191,11 +198,11 @@ public class MatchVideoServiceImpl implements MatchVideoService {
 
         res.put("status", "success");
         res.put("traceId", traceId);
-        return res.toJSONString();
+        return res;
     }
 
     @Override
-    public String recallVideos(RecallVideosParam recallVideosParam) throws UnsupportedEncodingException {
+    public JSONObject recallVideos(RecallVideosParam recallVideosParam) throws UnsupportedEncodingException {
         JSONObject res = new JSONObject();
         String traceId = recallVideosParam.getTraceId();
         Integer miniprogramUseType = recallVideosParam.getMiniprogramUseType();
@@ -207,7 +214,7 @@ public class MatchVideoServiceImpl implements MatchVideoService {
             param.put("miniprogramUseType", miniprogramUseType);
             HttpResponseContent httpResponseContent = HttpClientUtils.postData(url, param.toJSONString(),
                     HttpClientUtils.contentTypeJson);
-            return httpResponseContent.getBodyContent();
+            return JSONObject.parseObject(httpResponseContent.getBodyContent());
         }
         LongArticlesMatchVideo longArticlesMatchVideo = longArticlesMatchVideoRepository.getByTraceId(traceId);
         if (Objects.equals(longArticlesMatchVideo.getContentStatus(), ConstantStatusEnum.TASK_PUBLISHED_CODE.getCode())) {
@@ -219,11 +226,11 @@ public class MatchVideoServiceImpl implements MatchVideoService {
             longArticlesMatchVideo.setResponse(cardData.getNewItemList().toJSONString());
             longArticlesMatchVideo.setSuccessStatus(1);
             longArticlesMatchVideoRepository.save(longArticlesMatchVideo);
-            return res.toJSONString();
+            return res;
         }
 
         res.put("traceId", traceId);
         res.put("code", longArticlesMatchVideo.getContentStatus());
-        return res.toJSONString();
+        return res;
     }
 }

+ 5 - 4
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/web/video/MatchVideoController.java

@@ -1,5 +1,6 @@
 package com.tzld.longarticle.recommend.server.web.video;
 
+import com.alibaba.fastjson.JSONObject;
 import com.tzld.longarticle.recommend.server.common.response.CommonResponse;
 import com.tzld.longarticle.recommend.server.model.param.ChooseMinigramParam;
 import com.tzld.longarticle.recommend.server.model.param.GetOffVideosParam;
@@ -22,22 +23,22 @@ public class MatchVideoController {
     private MatchVideoService matchVideoService;
 
     @PostMapping("/searchVideos")
-    public String searchVideo(@RequestBody SearchVideoParam searchVideoParam) {
+    public JSONObject searchVideo(@RequestBody SearchVideoParam searchVideoParam) {
         return matchVideoService.searchVideo(searchVideoParam);
     }
 
     @PostMapping("/chooseMinigram")
-    public String chooseMinigram(@RequestBody ChooseMinigramParam chooseMinigramParam) {
+    public JSONObject chooseMinigram(@RequestBody ChooseMinigramParam chooseMinigramParam) {
         return matchVideoService.chooseMinigram(chooseMinigramParam);
     }
 
     @PostMapping("/getOffVideos")
-    public String getOffVideos(@RequestBody GetOffVideosParam getOffVideosParam) {
+    public JSONObject getOffVideos(@RequestBody GetOffVideosParam getOffVideosParam) {
         return matchVideoService.getOffVideos(getOffVideosParam);
     }
 
     @PostMapping("/recallideos")
-    public String recallVideos(@RequestBody RecallVideosParam recallVideosParam) throws UnsupportedEncodingException {
+    public JSONObject recallVideos(@RequestBody RecallVideosParam recallVideosParam) throws UnsupportedEncodingException {
         return matchVideoService.recallVideos(recallVideosParam);
     }