237 lines
8.8 KiB
Java
237 lines
8.8 KiB
Java
package com.whdc.aicc.utils;
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.TypeReference;
|
|
import com.whdc.aicc.model.aicc.*;
|
|
import com.whdc.common.utils.AESpkcs7paddingUtil;
|
|
import lombok.Setter;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.http.HttpEntity;
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
import org.apache.http.client.methods.HttpPost;
|
|
import org.apache.http.entity.StringEntity;
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
import org.apache.http.impl.client.HttpClients;
|
|
import org.apache.http.util.EntityUtils;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.io.IOException;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
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 AutoCallHelper {
|
|
@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;
|
|
|
|
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);
|
|
String resp = sendPost("https://aicc.cuopen.net:9801/aicc-api/ssb/callout/thirdParty/login", request);
|
|
|
|
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) {
|
|
return apiUploadCallData(data, getToken());
|
|
}
|
|
|
|
public AICCCallRespWrapper<AICCCallRespTask> apiUploadCallData(AICCUploadTask data, String token) {
|
|
String e;
|
|
try {
|
|
e = AESpkcs7paddingUtil.encrypt(JSON.toJSONString(data), secret);
|
|
} catch (Exception ex) {
|
|
throw new RuntimeException(ex);
|
|
}
|
|
JSONObject request = new JSONObject();
|
|
request.put("request", e);
|
|
String resp = sendPost("https://aicc.cuopen.net:9801/aicc-api/ssb/callout/thirdParty/task/uploadCallData", request, token);
|
|
|
|
TypeReference<AICCCallRespWrapper<AICCCallRespTask>> type = new TypeReference<AICCCallRespWrapper<AICCCallRespTask>>() {
|
|
};
|
|
AICCCallRespWrapper<AICCCallRespTask> AICCCallRespWrapper = JSON.parseObject(resp, type);
|
|
if (AICCCallRespWrapper == null || !AICCCallRespWrapper.isSuccess()) {
|
|
initToken();
|
|
resp = sendPost("https://aicc.cuopen.net:9801/aicc-api/ssb/callout/thirdParty/task/uploadCallData", request, token);
|
|
AICCCallRespWrapper = JSON.parseObject(resp, type);
|
|
}
|
|
|
|
if (AICCCallRespWrapper == null || !AICCCallRespWrapper.isSuccess()) {
|
|
return null;
|
|
}
|
|
return AICCCallRespWrapper;
|
|
}
|
|
|
|
public AICCCallRespWrapper<AICCCallRespDetail> apiGetTaskCallDetail(String requestId, String custId) {
|
|
JSONObject request = new JSONObject();
|
|
request.put("requestId", requestId);
|
|
request.put("custId", custId);
|
|
String resp = sendPost("https://aicc.cuopen.net:9801/aicc-api/ssb/callout/thirdParty/task/getTaskCallDetail", request);
|
|
|
|
TypeReference<AICCCallRespWrapper<AICCCallRespDetail>> type = new TypeReference<AICCCallRespWrapper<AICCCallRespDetail>>() {
|
|
};
|
|
AICCCallRespWrapper<AICCCallRespDetail> AICCCallRespWrapper = JSON.parseObject(resp, type);
|
|
if (AICCCallRespWrapper == null || !AICCCallRespWrapper.isSuccess()) {
|
|
initToken();
|
|
resp = sendPost("https://aicc.cuopen.net:9801/aicc-api/ssb/callout/thirdParty/task/getTaskCallDetail", request);
|
|
AICCCallRespWrapper = JSON.parseObject(resp, type);
|
|
}
|
|
|
|
if (AICCCallRespWrapper == null || !AICCCallRespWrapper.isSuccess()) {
|
|
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;
|
|
}
|
|
}
|