|
|
@@ -23,6 +23,7 @@ import com.cyksj.server.recharge.dto.TaskResult;
|
|
|
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.user.UserBindRelationService;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
@@ -42,8 +43,6 @@ import java.util.Optional;
|
|
|
@RequiredArgsConstructor
|
|
|
public class GptProxyRechargeServiceImpl implements GptProxyRechargeService {
|
|
|
|
|
|
- private static final String BASE_URL = "https://api.987ai.vip/api";
|
|
|
-
|
|
|
private final GptRechargeCardKeyMapper gptRechargeCardKeyMapper;
|
|
|
|
|
|
private final GroupsRelationMapper relationMapper;
|
|
|
@@ -60,10 +59,12 @@ public class GptProxyRechargeServiceImpl implements GptProxyRechargeService {
|
|
|
|
|
|
private final RedisService redisService;
|
|
|
|
|
|
+ private final GptRechargeCardKeyChannelService gptRechargeCardKeyChannelService;
|
|
|
+
|
|
|
private static final GlobalThreadPoolTaskExecutor TASK_EXECUTOR = GlobalThreadPoolTaskExecutor.getInstance();
|
|
|
|
|
|
- private HttpRequest createBaseRequest(String uri) {
|
|
|
- return HttpRequest.get(BASE_URL + uri)
|
|
|
+ private HttpRequest createBaseRequest(String url) {
|
|
|
+ return HttpRequest.get(url)
|
|
|
.header("accept", "application/json, text/plain, */*")
|
|
|
.header("accept-language", "zh-CN,zh;q=0.9,en;q=0.8")
|
|
|
.header("origin", "https://www.ow520.com")
|
|
|
@@ -74,8 +75,9 @@ public class GptProxyRechargeServiceImpl implements GptProxyRechargeService {
|
|
|
public CardKeyValidationResult validateCardKey(String cardKey) {
|
|
|
log.info("开始验证卡密: {}", cardKey);
|
|
|
try {
|
|
|
- HttpResponse response = createBaseRequest("/card-keys/" + cardKey)
|
|
|
- .execute();
|
|
|
+ String BASE_URL = gptRechargeCardKeyChannelService.getCardKeyDomain(cardKey);
|
|
|
+ HttpResponse response = createBaseRequest(BASE_URL + "/card-keys/" + cardKey)
|
|
|
+ .execute();
|
|
|
String body = response.body();
|
|
|
log.info("卡密验证响应: {}", body);
|
|
|
|
|
|
@@ -95,7 +97,8 @@ public class GptProxyRechargeServiceImpl implements GptProxyRechargeService {
|
|
|
public TokenParseResult parseToken(String accessToken) {
|
|
|
log.info("开始验证凭证: {}", accessToken.substring(0, Math.min(20, accessToken.length())) + "...");
|
|
|
try {
|
|
|
- String url = BASE_URL + "/parse-token";
|
|
|
+ String BASE_URL = gptRechargeCardKeyChannelService.getCardKeyDomain(null);
|
|
|
+ String url = BASE_URL + "/parse-token";
|
|
|
JSONObject requestBody = new JSONObject();
|
|
|
requestBody.put("access_token", accessToken);
|
|
|
|
|
|
@@ -127,7 +130,8 @@ public class GptProxyRechargeServiceImpl implements GptProxyRechargeService {
|
|
|
public TaskSubmitResult submitTask(String cardKey, String accessToken, String idp) {
|
|
|
log.info("开始提交任务 - 卡密: {}, idp: {}", cardKey, idp);
|
|
|
try {
|
|
|
- String url = BASE_URL + "/tasks";
|
|
|
+ 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);
|
|
|
@@ -162,8 +166,16 @@ public class GptProxyRechargeServiceImpl implements GptProxyRechargeService {
|
|
|
public TaskResult getTaskResult(String taskId) {
|
|
|
log.info("开始查询任务结果: {}", taskId);
|
|
|
try {
|
|
|
- String uri = "/tasks/" + taskId;
|
|
|
- HttpResponse response = createBaseRequest(uri)
|
|
|
+ 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);
|
|
|
@@ -178,7 +190,7 @@ public class GptProxyRechargeServiceImpl implements GptProxyRechargeService {
|
|
|
String status = result.getStatus();
|
|
|
String resultResult = Optional.ofNullable(result.getError()).orElse(result.getResult());
|
|
|
//更新代充状态
|
|
|
- resetGroupsRelationRechargeStatus(taskId, status, resultResult);
|
|
|
+ resetGroupsRelationRechargeStatus(relation, status, resultResult);
|
|
|
return result;
|
|
|
} catch (Exception e) {
|
|
|
log.error("获取任务结果失败: {}", e.getMessage(), e);
|
|
|
@@ -340,11 +352,9 @@ public class GptProxyRechargeServiceImpl implements GptProxyRechargeService {
|
|
|
/**
|
|
|
* 更新代充状态
|
|
|
*/
|
|
|
- private void resetGroupsRelationRechargeStatus(String taskId, String status, String result) {
|
|
|
+ private void resetGroupsRelationRechargeStatus(GroupsRelation relation, String status, String result) {
|
|
|
if ("completed".equals(status) || "failed".equals(status) || "unknown".equals(status)) {
|
|
|
- GroupsRelation relation = relationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class)
|
|
|
- .eq(GroupsRelation::getGptTaskId, taskId)
|
|
|
- .last("limit 1"));
|
|
|
+ String taskId = relation.getGptTaskId();
|
|
|
if (relation != null && relation.getRechargeStatus() == GroupsRelation.RechargeStatus.recharging) {
|
|
|
Integer rechargeRemainNum = relation.getRechargeRemainNum();
|
|
|
GroupsRelation.RechargeStatus rechargeStatus = "completed".equals(status) ? GroupsRelation.RechargeStatus.complete : GroupsRelation.RechargeStatus.recharge_error;
|