fxkh-txl-service/src/main/java/com/whdc/utils/AICCHelper.java

269 lines
11 KiB
Java
Raw Normal View History

2025-07-14 11:23:11 +08:00
package com.whdc.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
2025-08-08 14:02:39 +08:00
import com.whdc.model.autocall.aicc.*;
2025-07-14 11:23:11 +08:00
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<String> globalToken = new AtomicReference<>();
public String getToken() {
if (globalToken.get() == null) {
initToken();
}
return globalToken.get();
}
public void initToken() throws RuntimeException {
if (isAcquiringToken.compareAndSet(false, true)) {
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<String, String> 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<AICCCallRespWrapper<AICCLogin>> type = new TypeReference<AICCCallRespWrapper<AICCLogin>>() {
};
AICCCallRespWrapper<AICCLogin> 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);
}
} else {
try {
latch.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
public AICCCallRespWrapper<AICCCallRespTask> apiUploadCallData(AICCUploadTask data) throws GeneralSecurityException, UnsupportedEncodingException {
return apiUploadCallData(data, getToken());
}
public AICCCallRespWrapper<AICCCallRespTask> 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<String, String> 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<AICCCallRespWrapper<AICCCallRespTask>> type = new TypeReference<AICCCallRespWrapper<AICCCallRespTask>>() {
};
AICCCallRespWrapper<AICCCallRespTask> AICCCallRespWrapper = null;
2025-08-06 17:21:07 +08:00
if (!resp.contains("请求失败") || !resp.contains("网络异常")) {
try {
AICCCallRespWrapper = JSON.parseObject(resp, type);
} catch (Exception ex) {
log.error("apiUploadCallData first time: {}", resp);
log.error("error: ", ex);
}
2025-08-07 21:29:50 +08:00
} else {
log.info("apiUploadCallData first time: {}", resp);
}
2025-08-06 17:21:07 +08:00
2025-07-14 11:23:11 +08:00
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);
2025-08-06 17:21:07 +08:00
if (!resp.contains("请求失败") || !resp.contains("网络异常")) {
try {
AICCCallRespWrapper = JSON.parseObject(resp, type);
} catch (Exception ex) {
log.error("apiUploadCallData second time: {}", resp);
log.error("error: ", ex);
}
2025-08-07 21:29:50 +08:00
} else {
log.info("apiUploadCallData second time: {}", resp);
2025-08-06 17:21:07 +08:00
}
2025-07-14 11:23:11 +08:00
}
if (AICCCallRespWrapper == null || !AICCCallRespWrapper.isSuccess()) {
return null;
}
return AICCCallRespWrapper;
}
public AICCCallRespWrapper<AICCCallRespDetail> apiGetTaskCallDetail(String requestId, String custId) {
2025-08-06 17:21:07 +08:00
return apiGetTaskCallDetail(requestId, custId, getToken());
}
public AICCCallRespWrapper<AICCCallRespDetail> apiGetTaskCallDetail(String requestId, String custId, String token) {
2025-07-14 11:23:11 +08:00
JSONObject request = new JSONObject();
request.put("requestId", requestId);
request.put("custId", custId);
Map<String, String> headers = new HashMap<>();
2025-08-06 17:21:07 +08:00
headers.put("X-Access-Token", token);
2025-07-14 11:23:11 +08:00
String resp = httpHelper.postJsonString("https://aicc.cuopen.net:9801/aicc-api/ssb/callout/thirdParty/task/getTaskCallDetail", request.toJSONString(),headers);
TypeReference<AICCCallRespWrapper<AICCCallRespDetail>> type = new TypeReference<AICCCallRespWrapper<AICCCallRespDetail>>() {
};
AICCCallRespWrapper<AICCCallRespDetail> AICCCallRespWrapper = JSON.parseObject(resp, type);
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);
AICCCallRespWrapper = JSON.parseObject(resp, type);
}
if (AICCCallRespWrapper == null || !AICCCallRespWrapper.isSuccess()) {
2025-08-07 21:29:50 +08:00
log.info("获取任务详情失败:{}", resp);
2025-07-14 11:23:11 +08:00
return null;
}
return AICCCallRespWrapper;
}
public AICCUploadTask newTask(String requestId, String custId, String custName, String content, List<String> 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;
}
}