package com.whdc.utils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.TypeReference; import com.whdc.model.autocall.aicc.*; import lombok.Setter; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.io.UnsupportedEncodingException; import java.security.GeneralSecurityException; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; /** * @author lyf * @since 2025-06-19 */ @Component @Slf4j public class AICCHelper { @Setter @Value("${autocall.processId}") private String processId; @Setter @Value("${autocall.callerGroup}") private String callerGroup; @Setter @Value("${autocall.secret}") private String secret; @Setter @Value("${autocall.sysUserId}") private String sysUserId; @Autowired private HttpHelper httpHelper = new HttpHelper(); private static final AtomicBoolean isAcquiringToken = new AtomicBoolean(false); private static volatile CountDownLatch latch = new CountDownLatch(0); private static final AtomicReference globalToken = new AtomicReference<>(); public String getToken() { if (globalToken.get() == null) { initToken(); } return globalToken.get(); } public void initToken() throws RuntimeException { if (isAcquiringToken.compareAndSet(false, true)) { latch = new CountDownLatch(1); globalToken.set(null); try { String data = "{\"sysUserId\":\"" + sysUserId + "\",\"expire\":1000000}"; String encrypt; try { encrypt = AESpkcs7paddingUtil.encrypt(data, secret); } catch (Exception e) { throw new RuntimeException(e); } JSONObject request = new JSONObject(); request.put("request", encrypt); Map headers = new HashMap<>(); // headers.put("X-Access-Token", getToken()); String resp = httpHelper.postJsonString("https://aicc.cuopen.net:9801/aicc-api/ssb/callout/thirdParty/login", request.toJSONString(), headers); TypeReference> type = new TypeReference>() { }; AICCCallRespWrapper AICCCallRespWrapper = JSON.parseObject(resp, type); if (AICCCallRespWrapper == null || !AICCCallRespWrapper.isSuccess() || AICCCallRespWrapper.getResult() == null) { log.warn("获取外呼系统token失败"); return; } AICCLogin result = AICCCallRespWrapper.getResult(); String token = result.getToken(); // if (token != null && !token.isEmpty()) { globalToken.set(token); // } } finally { isAcquiringToken.set(false); latch.countDown(); } } else { try { latch.await(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } public AICCCallRespWrapper apiUploadCallData(AICCUploadTask data) throws GeneralSecurityException, UnsupportedEncodingException { return apiUploadCallData(data, getToken()); } public AICCCallRespWrapper apiUploadCallData(AICCUploadTask data, String token) throws GeneralSecurityException, UnsupportedEncodingException { String e; try { e = AESpkcs7paddingUtil.encrypt(JSON.toJSONString(data), secret); } catch (GeneralSecurityException | UnsupportedEncodingException ex) { throw ex; } catch (Exception ex) { throw new RuntimeException(ex); } JSONObject request = new JSONObject(); request.put("request", e); Map headers = new HashMap<>(); headers.put("X-Access-Token", token); String resp = httpHelper.postJsonString("https://aicc.cuopen.net:9801/aicc-api/ssb/callout/thirdParty/task/uploadCallData", request.toJSONString(), headers); log.info("apiUploadCallData: {}", resp); TypeReference> type = new TypeReference>() { }; AICCCallRespWrapper AICCCallRespWrapper = null; if (!resp.contains("请求失败") && !resp.contains("网络异常")) { try { AICCCallRespWrapper = JSON.parseObject(resp, type); } catch (Exception ex) { log.error("apiUploadCallData first time: {}", resp); log.error("error: ", ex); } } else { log.info("apiUploadCallData first time: {}", resp); } if (AICCCallRespWrapper == null || !AICCCallRespWrapper.isSuccess()) { initToken(); headers.put("X-Access-Token", getToken()); resp = httpHelper.postJsonString("https://aicc.cuopen.net:9801/aicc-api/ssb/callout/thirdParty/task/uploadCallData", request.toJSONString(), headers); if (!resp.contains("请求失败") && !resp.contains("网络异常")) { try { AICCCallRespWrapper = JSON.parseObject(resp, type); } catch (Exception ex) { log.error("apiUploadCallData second time: {}", resp); log.error("error: ", ex); } } else { log.info("apiUploadCallData second time: {}", resp); } } if (AICCCallRespWrapper == null || !AICCCallRespWrapper.isSuccess()) { return null; } return AICCCallRespWrapper; } public AICCCallRespWrapper apiGetTaskCallDetail(String requestId, String custId) { return apiGetTaskCallDetail(requestId, custId, getToken()); } public AICCCallRespWrapper apiGetTaskCallDetail(String requestId, String custId, String token) { JSONObject request = new JSONObject(); request.put("requestId", requestId); request.put("custId", custId); Map headers = new HashMap<>(); headers.put("X-Access-Token", token); String resp = httpHelper.postJsonString("https://aicc.cuopen.net:9801/aicc-api/ssb/callout/thirdParty/task/getTaskCallDetail", request.toJSONString(),headers); log.info("apiGetTaskCallDetail: {}", resp); TypeReference> type = new TypeReference>() { }; AICCCallRespWrapper aICCCallRespWrapper = null; try { aICCCallRespWrapper = JSON.parseObject(resp, type); } catch (Exception e) { log.error("apiGetTaskCallDetail first time: {}", resp); log.error("error: ", e); } if (aICCCallRespWrapper == null || !aICCCallRespWrapper.isSuccess()) { initToken(); headers.put("X-Access-Token", getToken()); resp = httpHelper.postJsonString("https://aicc.cuopen.net:9801/aicc-api/ssb/callout/thirdParty/task/getTaskCallDetail", request.toJSONString(), headers); try { aICCCallRespWrapper = JSON.parseObject(resp, type); } catch (Exception e) { log.error("apiGetTaskCallDetail second time: {}", resp); log.error("error: ", e); } } if (aICCCallRespWrapper == null || !aICCCallRespWrapper.isSuccess()) { log.info("获取任务详情失败:{}", resp); return null; } return aICCCallRespWrapper; } public AICCUploadTask newTask(String requestId, String custId, String custName, String content, List numbers) { AICCUploadTask task = new AICCUploadTask(); task.setTaskName(requestId); task.setProcessId(processId); task.setCallerGroup(callerGroup); task.setRequestId(requestId); task.genMutiTimeRange(); AICCUploadTask.Cust param = AICCUploadTask.Cust.builder() .setCustName(custName) .setCustId(custId) .setContent(content) .set_numbers(numbers) .build(); task.setParam(Collections.singletonList(param)); return task; } // private String sendPost(String url, JSONObject jsonData) { // String resp = sendPost(url, jsonData.toJSONString()); // if (resp == null) { // resp = sendPost(url, jsonData.toJSONString()); // } // return resp; // } // // private String sendPost(String url, JSONObject jsonData, String token) { // String resp = sendPost(url, jsonData.toJSONString(), token); // if (resp == null) { // resp = sendPost(url, jsonData.toJSONString(), token); // } // return resp; // } // // private String sendPost(String url, String jsonData) { // return sendPost(url, jsonData, getToken()); // } // // private String sendPost(String url, String jsonData, String token) { // CloseableHttpResponse response = null; // CloseableHttpClient httpClient = null; // String responseContent = null; // try { // httpClient = HttpClients.createDefault(); // HttpPost httpPost = new HttpPost(url); // httpPost.addHeader("Content-Type", "application/json"); // httpPost.addHeader("X-Access-Token", token); // // if (StringUtils.isNotBlank(jsonData)) { // httpPost.setEntity(new StringEntity(jsonData, "UTF-8")); // } // // log.info("请求地址: " + url); // log.info("token: " + getToken()); // log.info("请求参数: " + jsonData); // // response = httpClient.execute(httpPost); // HttpEntity entity = response.getEntity(); // responseContent = EntityUtils.toString(entity, "UTF-8"); // responseContent = jsonFormat(responseContent); // if (responseContent.length() < 200) { // log.info("响应参数: " + responseContent); // } // } catch (Exception e) { // log.error("发送请求异常", e); // return null; // } finally { // try { // if (null != response) { // response.close(); // } // httpClient.close(); // } catch (IOException e) { // e.printStackTrace(); // } // } // return responseContent; // } private static String jsonFormat(String str) { if (JSON.isValidObject(str)) { str = JSON.parseObject(str).toJSONString(); } return str; } }