AigcApiService.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. package com.tzld.piaoquan.api.component;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.tzld.piaoquan.api.common.exception.CommonException;
  4. import com.tzld.piaoquan.api.model.bo.GoogleLLMResult;
  5. import com.tzld.piaoquan.api.model.vo.WxAccountDatastatVO;
  6. import com.tzld.piaoquan.growth.common.component.HttpPoolClient;
  7. import com.tzld.piaoquan.growth.common.utils.DateUtil;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.apache.commons.collections.CollectionUtils;
  10. import org.apache.commons.lang3.StringUtils;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.beans.factory.annotation.Value;
  13. import org.springframework.stereotype.Component;
  14. import java.util.List;
  15. import java.util.Objects;
  16. @Slf4j
  17. @Component
  18. public class AigcApiService {
  19. @Autowired
  20. private HttpPoolClient httpPoolClient;
  21. @Value("${aigc.api.host:http://aigc-testapi.cybertogether.net/aigc}")
  22. private String aigcApiHost;
  23. @Value("${aigc.api.token:9ebfcb397e954c41986971f183eb1707}")
  24. private String aigcApiToken;
  25. @Value("${spring.profiles.active:dev}")
  26. private String env;
  27. private String getAigcPostParam(JSONObject params) {
  28. JSONObject baseInfo = JSONObject.parseObject("{\n" +
  29. " \"token\": \"" + aigcApiToken + "\",\n" +
  30. " \"appType\": 9,\n" +
  31. " \"platform\": \"pc\",\n" +
  32. " \"appVersionCode\": 1000,\n" +
  33. " \"clientTimestamp\": 1,\n" +
  34. " \"fid\": 1,\n" +
  35. " \"loginUid\": 1,\n" +
  36. " \"pageSource\": 1,\n" +
  37. " \"requestId\": 1,\n" +
  38. " \"rid\": 1,\n" +
  39. " \"uid\": 1\n" +
  40. "}");
  41. JSONObject param = new JSONObject();
  42. param.put("params", params);
  43. param.put("baseInfo", baseInfo);
  44. return param.toJSONString();
  45. }
  46. public String generateQrcode(String uuid) {
  47. String url = aigcApiHost + "/publish/account/generateQrcode";
  48. JSONObject params = new JSONObject();
  49. params.put("uuid", uuid);
  50. try {
  51. String post = httpPoolClient.post(url, getAigcPostParam(params));
  52. JSONObject res = JSONObject.parseObject(post);
  53. JSONObject data = res.getJSONObject("data");
  54. return data.getString("qrcodeStr");
  55. } catch (Exception e) {
  56. log.error("generateQrcode error", e);
  57. }
  58. return null;
  59. }
  60. public String getAuthResult(String uuid) {
  61. String url = aigcApiHost + "/publish/account/authResult";
  62. JSONObject params = new JSONObject();
  63. params.put("uuid", uuid);
  64. try {
  65. String post = httpPoolClient.post(url, getAigcPostParam(params));
  66. JSONObject res = JSONObject.parseObject(post);
  67. if (Objects.isNull(res) || res.getInteger("code") != 0) {
  68. return null;
  69. }
  70. JSONObject data = res.getJSONObject("data");
  71. return data.getString("id");
  72. } catch (Exception e) {
  73. log.error("getAuthResult error", e);
  74. }
  75. return null;
  76. }
  77. public JSONObject getAccountDetail(String publishAccountId) {
  78. String url = aigcApiHost + "/publish/account/detail";
  79. JSONObject params = new JSONObject();
  80. params.put("id", publishAccountId);
  81. try {
  82. String post = httpPoolClient.post(url, getAigcPostParam(params));
  83. JSONObject res = JSONObject.parseObject(post);
  84. return res.getJSONObject("data");
  85. } catch (Exception e) {
  86. log.error("getAccountDetail error", e);
  87. }
  88. return null;
  89. }
  90. public String getDetailByGhId(String ghId) {
  91. String url = aigcApiHost + "/publish/account/getDetailByGhId";
  92. JSONObject params = new JSONObject();
  93. params.put("id", ghId);
  94. try {
  95. String post = httpPoolClient.post(url, getAigcPostParam(params));
  96. JSONObject res = JSONObject.parseObject(post);
  97. JSONObject data = res.getJSONObject("data");
  98. if (Objects.nonNull(data)) {
  99. return data.getString("id");
  100. }
  101. } catch (Exception e) {
  102. log.error("getDetailByGhId error", e);
  103. }
  104. return null;
  105. }
  106. public String createPublishPlan(String publishAccountId, String name, String channel) {
  107. String url = aigcApiHost + "/publish/plan/save";
  108. JSONObject params = getPublishPlanAddParam(publishAccountId, name, channel);
  109. JSONObject res = null;
  110. try {
  111. String post = httpPoolClient.post(url, getAigcPostParam(params));
  112. res = JSONObject.parseObject(post);
  113. } catch (Exception e) {
  114. log.error("createPublishPlan error", e);
  115. }
  116. if (Objects.isNull(res)) {
  117. throw new CommonException(3000, "创建计划失败,请稍后重试");
  118. }
  119. Integer code = res.getInteger("code");
  120. if (code != 0) {
  121. throw new CommonException(3000, res.getString("msg"));
  122. }
  123. JSONObject data = res.getJSONObject("data");
  124. return data.getString("id");
  125. }
  126. private JSONObject getPublishPlanAddParam(String publishAccountId, String name, String channel) {
  127. String dateStr = DateUtil.getCurrentDateStr("yyyyMMdd");
  128. JSONObject params = JSONObject.parseObject("{\n" +
  129. " \"accountIds\": [\n" +
  130. " \"" + publishAccountId + "\"\n" +
  131. " ],\n" +
  132. " \"activeManualReview\": 0,\n" +
  133. " \"channel\": 5,\n" +
  134. " \"contentAllocationRules\": 0,\n" +
  135. " \"contentModal\": 7,\n" +
  136. " \"douyinPublishAccoutSetting\": [],\n" +
  137. " \"filterMatchMode\": 1,\n" +
  138. " \"name\": \"自动回复-" + dateStr + "-" + name + "-" + channel + "\",\n" +
  139. " \"publishAccoutJson\": \"\",\n" +
  140. " \"publishBgmType\": 0,\n" +
  141. " \"publishDate\": \"\",\n" +
  142. " \"publishLocation\": [],\n" +
  143. " \"publishNum\": \"\",\n" +
  144. " \"publishPushTime\": \"\",\n" +
  145. " \"publishRate\": 0,\n" +
  146. " \"publishTimeInterval\": 240,\n" +
  147. " \"wxContentInsert\": [\n" +
  148. " {\n" +
  149. " \"accountId\": \"" + publishAccountId + "\",\n" +
  150. " \"name\": \"" + name + "\",\n" +
  151. " \"bottomInsertFlag\": 0,\n" +
  152. " \"topInsertFlag\": 0,\n" +
  153. " \"miniprogramInsertFlag\": 0,\n" +
  154. " \"gzhArticleSortFlag\": 0,\n" +
  155. " \"publishOpenFlag\": 1\n" +
  156. " }\n" +
  157. " ],\n" +
  158. " \"wxVideoPublishAccountSetting\": [],\n" +
  159. " \"scoreJudgeFlag\": 0,\n" +
  160. " \"scoreJudgeTasks\": [],\n" +
  161. " \"inputGroups\": [],\n" +
  162. " \"machineReviewMatchMode\": 1,\n" +
  163. " \"gzhOnlyMiniprogramFlag\": 0,\n" +
  164. " \"planType\": 1,\n" +
  165. " \"pushType\": 6,\n" +
  166. " \"inputFilters\": [],\n" +
  167. " \"inputSources\": [],\n" +
  168. " \"triggerEvent\": 1,\n" +
  169. " \"gzhTriggerSyncFrequency\": 1,\n" +
  170. " \"gzhTriggerSendContentType\": 1,\n" +
  171. " \"gzhAutoReplyDelayFlag\": 1,\n" +
  172. " \"gzhAutoReplyDelayMillisecond\": 1000,\n" +
  173. " \"pushContentSortingRules\": 1\n" +
  174. "}");
  175. return params;
  176. }
  177. public void changePublishPlanStatus(String externalId, Integer status) {
  178. String url = aigcApiHost + "/publish/plan/updatePlanStatus";
  179. JSONObject params = new JSONObject();
  180. params.put("id", externalId);
  181. params.put("status", status);
  182. try {
  183. String post = httpPoolClient.post(url, getAigcPostParam(params));
  184. JSONObject res = JSONObject.parseObject(post);
  185. } catch (Exception e) {
  186. log.error("changePublishPlanStatus error", e);
  187. }
  188. }
  189. public void changePublishPlanAccountStatus(String planId, String accountId, Integer status) {
  190. String url = aigcApiHost + "/publish/plan/updatePlanAccountStatus";
  191. JSONObject params = new JSONObject();
  192. params.put("planId", planId);
  193. params.put("accountId", accountId);
  194. params.put("status", status);
  195. try {
  196. String post = httpPoolClient.post(url, getAigcPostParam(params));
  197. JSONObject res = JSONObject.parseObject(post);
  198. } catch (Exception e) {
  199. log.error("changePublishPlanAccountStatus error", e);
  200. }
  201. }
  202. public void refreshGzhAutoReplyMsgData(String ghId) {
  203. if (!"prod".equals(env)) {
  204. return;
  205. }
  206. String url = aigcApiHost + "/publish/api/refreshGzhAutoReplyMsgData?ghId=" + ghId;
  207. try {
  208. httpPoolClient.get(url);
  209. } catch (Exception e) {
  210. log.error("refreshGzhAutoReplyMsgData error", e);
  211. }
  212. }
  213. public List<WxAccountDatastatVO> getWxAccountDatastat(String dateStr, List<String> accountIds) {
  214. String url = aigcApiHost + "/dataStat/getWxAccountDatastat";
  215. JSONObject params = new JSONObject();
  216. params.put("dateStr", dateStr);
  217. params.put("accountIds", accountIds);
  218. try {
  219. String post = httpPoolClient.post(url, getAigcPostParam(params));
  220. JSONObject res = JSONObject.parseObject(post);
  221. return res.getJSONArray("data").toJavaList(WxAccountDatastatVO.class);
  222. } catch (Exception e) {
  223. log.error("getWxAccountDatastat error", e);
  224. }
  225. return null;
  226. }
  227. public void closeAccountMessagePublishPlan(String publishAccountId) {
  228. String url = aigcApiHost + "/publish/plan/closeAccountMessagePublishPlan";
  229. JSONObject params = new JSONObject();
  230. params.put("id", publishAccountId);
  231. try {
  232. String post = httpPoolClient.post(url, getAigcPostParam(params));
  233. JSONObject res = JSONObject.parseObject(post);
  234. } catch (Exception e) {
  235. log.error("closeAccountMessagePublishPlan error", e);
  236. }
  237. }
  238. public boolean checkContainMessagePlan(String publishAccountId) {
  239. String url = aigcApiHost + "/publish/plan/list";
  240. JSONObject params = JSONObject.parseObject("{\n" +
  241. " \"filterItems\": [\n" +
  242. " {\n" +
  243. " \"itemName\": \"planType\",\n" +
  244. " \"selectValues\": [\n" +
  245. " 1\n" +
  246. " ]\n" +
  247. " },\n" +
  248. " {\n" +
  249. " \"itemName\": \"publishAccounts\",\n" +
  250. " \"selectValues\": [\n" +
  251. " \"" + publishAccountId + "\"\n" +
  252. " ]\n" +
  253. " },\n" +
  254. " {\n" +
  255. " \"itemName\": \"planStatus\",\n" +
  256. " \"selectValues\": [\n" +
  257. " 1\n" +
  258. " ]\n" +
  259. " }\n" +
  260. " ],\n" +
  261. " \"listFieldFormula\": [],\n" +
  262. " \"pageNum\": 1,\n" +
  263. " \"pageSize\": 50,\n" +
  264. " \"channel\": 5,\n" +
  265. " \"contentModal\": 7\n" +
  266. "}");
  267. try {
  268. String post = httpPoolClient.post(url, getAigcPostParam(params));
  269. JSONObject res = JSONObject.parseObject(post);
  270. Integer totalCount = res.getJSONObject("data").getInteger("totalCount");
  271. return totalCount > 0;
  272. } catch (Exception e) {
  273. log.error("closeAccountMessagePublishPlan error", e);
  274. }
  275. return false;
  276. }
  277. public String gpt(String model, String prompt, JSONObject responseFormat, List<String> imageList) {
  278. if (StringUtils.isEmpty(model) || StringUtils.isEmpty(prompt)) {
  279. return null;
  280. }
  281. String url = "http://aigc-api.cybertogether.net/aigc" + "/dev/test/gpt";
  282. JSONObject params = new JSONObject();
  283. params.put("model", model);
  284. params.put("prompt", prompt);
  285. if (Objects.nonNull(responseFormat)) {
  286. params.put("responseFormat", responseFormat);
  287. }
  288. if (CollectionUtils.isNotEmpty(imageList)) {
  289. params.put("imageList", imageList);
  290. }
  291. try {
  292. String post = httpPoolClient.post(url, params.toJSONString());
  293. JSONObject res = JSONObject.parseObject(post);
  294. return res.getString("data");
  295. } catch (Exception e) {
  296. log.error("gpt error", e);
  297. }
  298. return null;
  299. }
  300. public GoogleLLMResult gemini(String model, String prompt, List<String> imageList) {
  301. if (StringUtils.isEmpty(model) || StringUtils.isEmpty(prompt)) {
  302. return null;
  303. }
  304. String url = "http://aigc-api.cybertogether.net/aigc" + "/infrastructure/gemini/requestWithMedia?model=" + model;
  305. JSONObject params = new JSONObject();
  306. params.put("prompt", prompt);
  307. if (CollectionUtils.isNotEmpty(imageList)) {
  308. params.put("mediaList", imageList);
  309. }
  310. try {
  311. String post = httpPoolClient.post(url, params.toJSONString());
  312. return JSONObject.parseObject(post, GoogleLLMResult.class);
  313. } catch (Exception e) {
  314. log.error("gemini error", e);
  315. }
  316. return null;
  317. }
  318. }