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)) {
|
2025-08-09 19:07:50 +08:00
|
|
|
latch = new CountDownLatch(1);
|
2025-07-14 11:23:11 +08:00
|
|
|
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<>();
|
2025-08-09 19:07:50 +08:00
|
|
|
// headers.put("X-Access-Token", getToken());
|
2025-07-14 11:23:11 +08:00
|
|
|
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();
|
2025-08-09 19:07:50 +08:00
|
|
|
// if (token != null && !token.isEmpty()) {
|
2025-07-14 11:23:11 +08:00
|
|
|
globalToken.set(token);
|
2025-08-09 19:07:50 +08:00
|
|
|
// }
|
2025-07-14 11:23:11 +08:00
|
|
|
} finally {
|
|
|
|
|
isAcquiringToken.set(false);
|
2025-08-09 19:07:50 +08:00
|
|
|
latch.countDown();
|
2025-07-14 11:23:11 +08:00
|
|
|
}
|
|
|
|
|
} 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>>() {
|
|
|
|
|
};
|
2025-08-04 15:33:08 +08:00
|
|
|
AICCCallRespWrapper<AICCCallRespTask> AICCCallRespWrapper = null;
|
2025-08-08 13:51:52 +08:00
|
|
|
if (!resp.contains("请求失败") && !resp.contains("网络异常")) {
|
2025-08-06 17:21:07 +08:00
|
|
|
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-04 15:33:08 +08:00
|
|
|
}
|
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-08 13:51:52 +08:00
|
|
|
if (!resp.contains("请求失败") && !resp.contains("网络异常")) {
|
2025-08-06 17:21:07 +08:00
|
|
|
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);
|
2025-08-09 19:07:50 +08:00
|
|
|
log.info("apiGetTaskCallDetail: {}", resp);
|
2025-07-14 11:23:11 +08:00
|
|
|
TypeReference<AICCCallRespWrapper<AICCCallRespDetail>> type = new TypeReference<AICCCallRespWrapper<AICCCallRespDetail>>() {
|
|
|
|
|
};
|
2025-08-09 19:07:50 +08:00
|
|
|
AICCCallRespWrapper<AICCCallRespDetail> 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()) {
|
2025-07-14 11:23:11 +08:00
|
|
|
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);
|
2025-08-09 19:07:50 +08:00
|
|
|
try {
|
|
|
|
|
aICCCallRespWrapper = JSON.parseObject(resp, type);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("apiGetTaskCallDetail second time: {}", resp);
|
|
|
|
|
log.error("error: ", e);
|
|
|
|
|
}
|
2025-07-14 11:23:11 +08:00
|
|
|
}
|
|
|
|
|
|
2025-08-09 19:07:50 +08:00
|
|
|
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;
|
|
|
|
|
}
|
2025-08-09 19:07:50 +08:00
|
|
|
return aICCCallRespWrapper;
|
2025-07-14 11:23:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|