RecommendApiService.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package com.tzld.piaoquan.api.component;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.tzld.piaoquan.growth.common.component.HttpPoolClient;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.stereotype.Component;
  8. /**
  9. * 推荐API服务
  10. */
  11. @Component
  12. @Slf4j
  13. public class RecommendApiService {
  14. @Autowired
  15. private HttpPoolClient httpPoolClient;
  16. @Value("${recommend.api.host:http://101.37.174.139:80}")
  17. private String recommendApiHost;
  18. /**
  19. * 根据rootSourceId获取文章信息
  20. */
  21. public JSONObject getArticleByRootSourceId(String rootSourceId) {
  22. try {
  23. String url = recommendApiHost + "/article/getArticleByRootSourceId?rootSourceId=" + rootSourceId;
  24. String response = httpPoolClient.get(url);
  25. JSONObject res = JSONObject.parseObject(response);
  26. if (res.getInteger("code") == 0 && res.getJSONObject("data") != null) {
  27. return res.getJSONObject("data");
  28. }
  29. } catch (Exception e) {
  30. log.error("获取文章信息失败, rootSourceId={}", rootSourceId, e);
  31. }
  32. return null;
  33. }
  34. /**
  35. * 根据rootSourceId获取合作方文章信息
  36. */
  37. public String getCooperateArticleIdByRootSourceId(String rootSourceId) {
  38. try {
  39. String url = recommendApiHost + "/article/cooperate/getArticleIdByRootSourceId?rootSourceId=" + rootSourceId;
  40. String response = httpPoolClient.get(url);
  41. JSONObject res = JSONObject.parseObject(response);
  42. if (res.getInteger("code") == 0 && res.getString("data") != null) {
  43. return res.getString("data");
  44. }
  45. } catch (Exception e) {
  46. log.error("获取文章信息失败, rootSourceId={}", rootSourceId, e);
  47. }
  48. return null;
  49. }
  50. /**
  51. * 检查rootSourceId是否存在
  52. */
  53. public Boolean checkExistRootSourceId(String rootSourceId) {
  54. try {
  55. String url = recommendApiHost + "/api/checkExistRootSourceId?rootSourceId=" + rootSourceId;
  56. String response = httpPoolClient.get(url);
  57. JSONObject res = JSONObject.parseObject(response);
  58. if (res != null && res.getInteger("code") == 0) {
  59. return res.getBoolean("data");
  60. }
  61. } catch (Exception e) {
  62. log.error("检查rootSourceId是否存在失败, rootSourceId={}", rootSourceId, e);
  63. }
  64. return false;
  65. }
  66. }