Sfoglia il codice sorgente

Merge branch 'claude_limit_up'

chenbiao 1 anno fa
parent
commit
d94883cf7e

+ 2 - 0
netflix-dao/src/main/java/com/cyksj/model/views/ClaudeCarInfoView.java

@@ -65,6 +65,8 @@ public class ClaudeCarInfoView {
     @DbField("cs.sort")
     private Integer sort;
 
+    @DbField("clears_in")
+    private Long clearsIn;
 
     @Override
     public boolean equals(Object o) {

+ 15 - 10
netflix-service/src/main/java/com/cyksj/service/claude/impl/ClaudeServiceImpl.java

@@ -4,6 +4,8 @@ import cn.hutool.core.date.DateTime;
 import cn.hutool.core.lang.UUID;
 import cn.hutool.core.util.RandomUtil;
 import cn.hutool.core.util.StrUtil;
+import cn.hutool.http.HttpUtil;
+import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.cyksj.common.exception.BusinessRuntimeException;
@@ -23,6 +25,8 @@ import org.apache.commons.lang3.StringUtils;
 import org.springframework.dao.DuplicateKeyException;
 import org.springframework.stereotype.Service;
 
+import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.time.LocalDateTime;
 import java.util.List;
 import java.util.Optional;
@@ -217,16 +221,9 @@ public class ClaudeServiceImpl implements ClaudeService {
      */
     @Override
     public Long getConversationCount(String userToken, Long limitTime) {
-        String key = RedisService.key.CLAUDE_CONVERSATION_LIMIT.getName() + ":" + userToken;
-        long currentTimeMillis = System.currentTimeMillis();
-        long windowStartMillis = currentTimeMillis - (limitTime * 60 * 60) * 1000;
-
-        // 清除时间窗口之前的请求记录(可选,根据需要决定是否在此处清理过期记录)
-        redisService.zRemoveRangeByScore(key, 0, windowStartMillis);
-
-        // 获取当前窗口内的请求次数
-        Long currentSize = redisService.zCard(key);
-        return currentSize != null ? currentSize : 0L;
+        JSONObject conversationCount = getConversationCount(userToken);
+        Long count = conversationCount.getLong("count");
+        return new BigDecimal(count).divide(BigDecimal.valueOf(4000),0, RoundingMode.HALF_UP).longValue();
     }
 
     @Override
@@ -378,5 +375,13 @@ public class ClaudeServiceImpl implements ClaudeService {
         redisService.zAdd(RedisService.key.CLAUDE_CAR_SCORES.getName(), highExperienceScore, carId);
     }
 
+    public JSONObject getConversationCount(String userToken) {
+        try {
+            String res = HttpUtil.get("http://154.198.213.52:9611/claude/getConversationCount?userToken=" + userToken);
+            return new JSONObject(res);
+        }catch (Exception e){
+            return new JSONObject("{\"count\": 0}");
+        }
+    }
 
 }

+ 2 - 103
netflix-web/src/main/java/com/cyksj/web/controller/mirror/MirrorController.java

@@ -371,78 +371,6 @@ public class MirrorController {
         }
     }
 
-    /**
-     *  Claude会话限制
-     */
-    @RequestMapping("/claude/conversation/limit")
-    public ClaudeConversionLimitView claudeConversationLimit() {
-        //1.从request获取请求头Authorization 并取 值内 'Bearer ' 后的值为usertoken
-        String authorization = request.getHeader("Authorization");
-        String carid = request.getHeader("Carid");
-        String userToken = authorization.substring(7);
-        //2.根据usertoken 查询 ClaudeUser  表中的记录
-        ClaudeUser user = claudeService.findByUserTokenAndExpireTimeAfter(userToken, LocalDateTime.now());
-        //3.如果记录不存在,返回状态码 401
-        if (user == null) {
-            response.setStatus(400);
-            ClaudeConversionLimitView claudeConversionLimitView = new ClaudeConversionLimitView();
-            ClaudeConversionLimitView.Error error = new ClaudeConversionLimitView.Error();
-            error.setMessage("看来您还没车票呢,是不是还没登录呀,赶紧去车票内重新选择上车 \uD83D\uDE97 [点击直达](https://nf.video/ticket)");
-            claudeConversionLimitView.setError(error);
-            return claudeConversionLimitView;
-        }else {
-            if (user.getIsBlock() != null && user.getIsBlock()) {
-                response.setStatus(400);
-                ClaudeConversionLimitView chatgptConversionLimitView = new ClaudeConversionLimitView();
-                ClaudeConversionLimitView.Error error = new ClaudeConversionLimitView.Error();
-                error.setMessage("抱歉,您因违反银河录像局使用条款已被封禁.");
-                chatgptConversionLimitView.setError(error);
-                return chatgptConversionLimitView;
-            }
-
-                ConversationLimitResponse conversationLimitResponse = claudeService.conversationLimit(userToken, user);
-
-                if (conversationLimitResponse.isLimited()) {
-                    response.setStatus(429);
-                    ClaudeConversionLimitView claudeConversionLimitView = new ClaudeConversionLimitView();
-                    ClaudeConversionLimitView.Error error = new ClaudeConversionLimitView.Error();
-                    //String url = "[点击此处升级](https://nf.video/ticket?relationId=%d&pType=pay)";
-                    //
-                    //String format = String.format(url, user.getRelationId());
-                    error.setMessage("您目前的套餐已达到使用上限,预计 " + DateUtil.format(new Date(conversationLimitResponse.getNextAvailableTime()), "yyyy-MM-dd HH:mm:ss") + " 恢复");
-                    claudeConversionLimitView.setError(error);
-                    return claudeConversionLimitView;
-                } else {
-                    response.setStatus(200);
-                }
-        }
-
-        return new ClaudeConversionLimitView();
-    }
-
-
-    /**
-     *  Claude触发车队限制
-     */
-    @GetMapping("/claude/car/limited")
-    public Result<String> claudeCarLimited(String carId, Long expTime) {
-        String authorization = request.getHeader("Authorization");
-        String userToken = "";
-        if(StringUtils.isNotBlank(authorization)){
-            try {
-                userToken = authorization.substring(7);
-            }catch (Exception e){
-                log.error("获取用户token失败",e);
-                return GatewayResponse.SUCCESS.newBuilder().toResult();
-            }
-        }
-        String finalUserToken = userToken;
-        TASK_EXECUTOR.execute(()->{
-            claudeService.carLimited(carId, finalUserToken, expTime);
-        });
-        return GatewayResponse.SUCCESS.newBuilder().toResult();
-    }
-
 
     /**
      *  Claude获取车队列表
@@ -462,10 +390,9 @@ public class MirrorController {
         }
         SearchResult<ClaudeCarInfoView> carInfoList = beanSearcher.search(ClaudeCarInfoView.class, MapUtils.flatBuilder(request.getParameterMap()).field(ClaudeCarInfoView::getCsStatus, true).orderBy(ClaudeCarInfoView::getSort).desc().orderBy(ClaudeCarInfoView::getScore).asc().build());
         carInfoList.getDataList().forEach((claudeCarInfoView)->{
-            Long aLong = claudeService.carLimit(claudeCarInfoView.getCarId());
-            if (aLong != 0L) {
+            if (claudeCarInfoView.getClearsIn() > 0) {
                 claudeCarInfoView.setStatus("停运");
-                claudeCarInfoView.setExpTime(new DateTime(System.currentTimeMillis() + aLong * 1000));
+                claudeCarInfoView.setExpTime(new DateTime(System.currentTimeMillis() + claudeCarInfoView.getClearsIn() * 1000));
             }else {
                 claudeCarInfoView.setStatus(claudeCarInfoView.getScore() > 50 ? "繁忙":"空闲");
             }
@@ -474,34 +401,6 @@ public class MirrorController {
     }
 
 
-    /**
-     *  Claude对话成功后记录次数
-     */
-    @RequestMapping("/claude/conversation/notifyUrl")
-    public void claudeConversationNotifyUrl() {
-        //1.从request获取请求头Authorization 并取 值内 'Bearer ' 后的值为usertoken
-        String authorization = request.getHeader("Authorization");
-        String carid = request.getHeader("Carid");
-        String model = request.getHeader("Model");
-        String conversation_id = request.getHeader("Convid");
-        ConversationRequest conversationRequest = new ConversationRequest();
-        conversationRequest.setCarId(carid);
-        conversationRequest.setModel(model);
-        conversationRequest.setConversation_id(conversation_id);
-        String userToken = authorization.substring(7);
-        //2.根据usertoken 查询 chatgpt_user 表中的记录
-        ClaudeUser user = claudeService.findByUserTokenAndExpireTimeAfter(userToken, LocalDateTime.now());
-        //3.如果记录不存在,返回状态码 401
-        if (user == null) {
-            return;
-        }
-        if(StringUtils.isNotBlank(conversationRequest.getCarId())){
-            TASK_EXECUTOR.execute(() -> {
-                claudeService.saveConversationRecord(userToken, conversationRequest);
-            });
-        }
-    }
-
 
     /**
      * luma车票跳转登录