|
|
@@ -24,6 +24,7 @@ import com.cyksj.server.recharge.dto.TaskSubmitResult;
|
|
|
import com.cyksj.server.recharge.dto.TokenParseResult;
|
|
|
import com.cyksj.service.chatgpt.GptUserRechargeRecordService;
|
|
|
import com.cyksj.service.recharge.GptRechargeCardKeyChannelService;
|
|
|
+import com.cyksj.service.recharge.OwGptRechargeService;
|
|
|
import com.cyksj.service.user.UserBindRelationService;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
@@ -61,6 +62,8 @@ public class GptProxyRechargeServiceImpl implements GptProxyRechargeService {
|
|
|
|
|
|
private final GptRechargeCardKeyChannelService gptRechargeCardKeyChannelService;
|
|
|
|
|
|
+ private final OwGptRechargeService owGptRechargeService;
|
|
|
+
|
|
|
private static final GlobalThreadPoolTaskExecutor TASK_EXECUTOR = GlobalThreadPoolTaskExecutor.getInstance();
|
|
|
|
|
|
private HttpRequest createBaseRequest(String url) {
|
|
|
@@ -72,10 +75,13 @@ public class GptProxyRechargeServiceImpl implements GptProxyRechargeService {
|
|
|
.header("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36");
|
|
|
}
|
|
|
|
|
|
- public CardKeyValidationResult validateCardKey(String cardKey) {
|
|
|
+ public Boolean validateCardKey(String cardKey) {
|
|
|
log.info("开始验证卡密: {}", cardKey);
|
|
|
try {
|
|
|
String BASE_URL = gptRechargeCardKeyChannelService.getCardKeyDomain(cardKey);
|
|
|
+ if (BASE_URL.contains(Constant.SC_OW_DOMAIN_STR)) {
|
|
|
+ return owGptRechargeService.validateCardKey(BASE_URL, cardKey);
|
|
|
+ }
|
|
|
HttpResponse response = createBaseRequest(BASE_URL + "/card-keys/" + cardKey)
|
|
|
.execute();
|
|
|
String body = response.body();
|
|
|
@@ -87,7 +93,7 @@ public class GptProxyRechargeServiceImpl implements GptProxyRechargeService {
|
|
|
);
|
|
|
//{"available":false,"error":"卡密已被使用"}
|
|
|
log.info("卡密验证结果: {}", result);
|
|
|
- return result;
|
|
|
+ return result.getAvailable();
|
|
|
} catch (Exception e) {
|
|
|
log.error("验证卡密失败: {}", e.getMessage(), e);
|
|
|
throw new RuntimeException("验证卡密失败: " + e.getMessage(), e);
|
|
|
@@ -127,76 +133,85 @@ public class GptProxyRechargeServiceImpl implements GptProxyRechargeService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public TaskSubmitResult submitTask(String cardKey, String accessToken, String idp) {
|
|
|
- log.info("开始提交任务 - 卡密: {}, idp: {}", cardKey, idp);
|
|
|
- try {
|
|
|
- String BASE_URL = gptRechargeCardKeyChannelService.getCardKeyDomain(cardKey);
|
|
|
- String url = BASE_URL + "/tasks";
|
|
|
- JSONObject requestBody = new JSONObject();
|
|
|
- requestBody.put("card_key", cardKey);
|
|
|
- requestBody.put("access_token", accessToken);
|
|
|
- requestBody.put("idp", idp);
|
|
|
-
|
|
|
- HttpResponse response = HttpRequest.post(url)
|
|
|
- .setConnectionTimeout(60000)
|
|
|
- .header("accept", "application/json, text/plain, */*")
|
|
|
- .header("accept-language", "zh-CN,zh;q=0.9,en;q=0.8")
|
|
|
- .header("content-type", "application/json")
|
|
|
- .header("origin", "https://www.ow520.com")
|
|
|
- .header("referer", "https://www.ow520.com/")
|
|
|
- .header("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36")
|
|
|
- .body(requestBody.toString())
|
|
|
- .execute();
|
|
|
- String body = response.body();
|
|
|
- log.info("任务提交响应: {}", body);
|
|
|
-
|
|
|
- JSONObject jsonResponse = JSONUtil.parseObj(body);
|
|
|
- TaskSubmitResult result = new TaskSubmitResult(
|
|
|
- jsonResponse.getStr("task_id"),
|
|
|
- jsonResponse.getBool("success")
|
|
|
- );
|
|
|
- log.info("任务提交结果: {}", result);
|
|
|
- return result;
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("提交任务失败: {}", e.getMessage(), e);
|
|
|
- throw new RuntimeException("提交任务失败: " + e.getMessage(), e);
|
|
|
- }
|
|
|
- }
|
|
|
+ public TaskSubmitResult submitTask(String cardKey, String accountEmail, String fullToken, String accessToken, String idp) {
|
|
|
+ log.info("开始提交任务 - 卡密: {}, idp: {}", cardKey, idp);
|
|
|
+ try {
|
|
|
+ String BASE_URL = gptRechargeCardKeyChannelService.getCardKeyDomain(cardKey);
|
|
|
+ if (BASE_URL.contains(Constant.SC_OW_DOMAIN_STR)) {
|
|
|
+ return owGptRechargeService.submitTask(BASE_URL, cardKey, accountEmail, fullToken, accessToken);
|
|
|
+ }
|
|
|
+ String url = BASE_URL + "/tasks";
|
|
|
+ JSONObject requestBody = new JSONObject();
|
|
|
+ requestBody.put("card_key", cardKey);
|
|
|
+ requestBody.put("access_token", accessToken);
|
|
|
+ requestBody.put("idp", idp);
|
|
|
|
|
|
- public TaskResult getTaskResult(String taskId) {
|
|
|
- log.info("开始查询任务结果: {}", taskId);
|
|
|
- try {
|
|
|
- GroupsRelation relation = relationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class)
|
|
|
- .eq(GroupsRelation::getGptTaskId, taskId)
|
|
|
- .last("limit 1"));
|
|
|
- if (relation == null) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- String cardKey = relation.getCardKey();
|
|
|
- String BASE_URL = gptRechargeCardKeyChannelService.getCardKeyDomain(cardKey);
|
|
|
- String url = BASE_URL + "/tasks/" + taskId;
|
|
|
- HttpResponse response = createBaseRequest(url)
|
|
|
- .execute();
|
|
|
- String body = response.body();
|
|
|
- log.info("任务查询响应: {}", body);
|
|
|
+ HttpResponse response = HttpRequest.post(url)
|
|
|
+ .setConnectionTimeout(60000)
|
|
|
+ .header("accept", "application/json, text/plain, */*")
|
|
|
+ .header("accept-language", "zh-CN,zh;q=0.9,en;q=0.8")
|
|
|
+ .header("content-type", "application/json")
|
|
|
+ .header("origin", "https://www.ow520.com")
|
|
|
+ .header("referer", "https://www.ow520.com/")
|
|
|
+ .header("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36")
|
|
|
+ .body(requestBody.toString())
|
|
|
+ .execute();
|
|
|
+ String body = response.body();
|
|
|
+ log.info("任务提交响应: {}", body);
|
|
|
|
|
|
- JSONObject jsonResponse = JSONUtil.parseObj(body);
|
|
|
- TaskResult result = new TaskResult(
|
|
|
- jsonResponse.getStr("status"),
|
|
|
- jsonResponse.getStr("result"),
|
|
|
- jsonResponse.getStr("error")
|
|
|
- );
|
|
|
- log.info("任务查询结果: {}", result);
|
|
|
- String status = result.getStatus();
|
|
|
- String resultResult = Optional.ofNullable(result.getError()).orElse(result.getResult());
|
|
|
+ JSONObject jsonResponse = JSONUtil.parseObj(body);
|
|
|
+ TaskSubmitResult result = new TaskSubmitResult(
|
|
|
+ jsonResponse.getStr("task_id"),
|
|
|
+ jsonResponse.getBool("success")
|
|
|
+ );
|
|
|
+ log.info("任务提交结果: {}", result);
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("提交任务失败: {}", e.getMessage(), e);
|
|
|
+ throw new RuntimeException("提交任务失败: " + e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public TaskResult getTaskResult(String taskId) {
|
|
|
+ log.info("开始查询任务结果: {}", taskId);
|
|
|
+ try {
|
|
|
+ GroupsRelation relation = relationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class)
|
|
|
+ .eq(GroupsRelation::getGptTaskId, taskId)
|
|
|
+ .last("limit 1"));
|
|
|
+ if (relation == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String cardKey = relation.getCardKey();
|
|
|
+ String BASE_URL = gptRechargeCardKeyChannelService.getCardKeyDomain(cardKey);
|
|
|
+ TaskResult result;
|
|
|
+ String resultResult;
|
|
|
+ if (BASE_URL.contains(Constant.SC_OW_DOMAIN_STR)) {
|
|
|
+ result = owGptRechargeService.getTaskResult(BASE_URL, taskId);
|
|
|
+ log.info("ow代充任务查询结果: {}", result);
|
|
|
+ } else {
|
|
|
+ String url = BASE_URL + "/tasks/" + taskId;
|
|
|
+ HttpResponse response = createBaseRequest(url)
|
|
|
+ .execute();
|
|
|
+ String body = response.body();
|
|
|
+ log.info("任务查询响应: {}", body);
|
|
|
+
|
|
|
+ JSONObject jsonResponse = JSONUtil.parseObj(body);
|
|
|
+ result = new TaskResult(
|
|
|
+ jsonResponse.getStr("status"),
|
|
|
+ jsonResponse.getStr("result"),
|
|
|
+ jsonResponse.getStr("error")
|
|
|
+ );
|
|
|
+ log.info("任务查询结果: {}", result);
|
|
|
+ }
|
|
|
+ resultResult = Optional.ofNullable(result.getError()).orElse(result.getResult());
|
|
|
//更新代充状态
|
|
|
- resetGroupsRelationRechargeStatus(relation, status, resultResult);
|
|
|
- return result;
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("获取任务结果失败: {}", e.getMessage(), e);
|
|
|
- throw new RuntimeException("获取任务结果失败: " + e.getMessage(), e);
|
|
|
- }
|
|
|
- }
|
|
|
+ resetGroupsRelationRechargeStatus(relation, result.getStatus(), resultResult);
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取任务结果失败: {}", e.getMessage(), e);
|
|
|
+ throw new RuntimeException("获取任务结果失败: " + e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Throwable.class)
|
|
|
@@ -308,13 +323,13 @@ public class GptProxyRechargeServiceImpl implements GptProxyRechargeService {
|
|
|
}
|
|
|
String gptCardKey = gptRechargeCardKey.getCardKey();
|
|
|
//校验卡密是否可用
|
|
|
- CardKeyValidationResult cardKeyValidationResult = validateCardKey(gptCardKey);
|
|
|
- if (!cardKeyValidationResult.getAvailable()) {
|
|
|
+ Boolean isAvailable = validateCardKey(gptCardKey);
|
|
|
+ if (!isAvailable) {
|
|
|
setRechargeRelationStatusAutoError(relation);
|
|
|
throw BusinessRuntimeException.getInstance("充值失败,请联系客服开通");
|
|
|
}
|
|
|
//提交任务
|
|
|
- TaskSubmitResult result = submitTask(gptCardKey, accessToken, "auth0");
|
|
|
+ TaskSubmitResult result = submitTask(gptCardKey, rechargeAccount, saveTokenJson, accessToken, "auth0");
|
|
|
if (!result.getSuccess()) {
|
|
|
setRechargeRelationStatusAutoError(relation);
|
|
|
throw BusinessRuntimeException.getInstance("充值失败,请联系客服开通");
|