|
|
@@ -1,5 +1,6 @@
|
|
|
package com.cyksj.service.chatgpt.impl;
|
|
|
|
|
|
+import cn.hutool.core.date.DateField;
|
|
|
import cn.hutool.core.date.DateTime;
|
|
|
import cn.hutool.core.date.DateUnit;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
@@ -16,10 +17,14 @@ import cn.hutool.json.JSONUtil;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.cyksj.common.constant.Constant;
|
|
|
import com.cyksj.common.exception.BusinessRuntimeException;
|
|
|
+import com.cyksj.common.snowflake.Sequence;
|
|
|
import com.cyksj.common.util.Jsons;
|
|
|
+import com.cyksj.common.util.SmsUtil;
|
|
|
import com.cyksj.common.util.StringUtil;
|
|
|
+import com.cyksj.dto.RedisKey;
|
|
|
import com.cyksj.mapper.*;
|
|
|
import com.cyksj.mapper.station.CollegeStudentStationUseTicketRecordMapper;
|
|
|
+import com.cyksj.model.dto.ChatgptUserVerifyDto;
|
|
|
import com.cyksj.model.entity.*;
|
|
|
import com.cyksj.model.views.ChatGptUserView;
|
|
|
import com.cyksj.model.views.ChatgptCarInfoView;
|
|
|
@@ -64,6 +69,9 @@ public class ChatGptAccountServiceImpl implements ChatGptAccountService {
|
|
|
|
|
|
private static final String GPT_PROXY = "https://chat-chan-87jztgkf257d.xyhelper.org";
|
|
|
|
|
|
+ private static final String RESTRICTION_URL = "https://nf.video/bind/web/validating";
|
|
|
+
|
|
|
+ private final static Sequence SEQUENCE = new Sequence(0);
|
|
|
|
|
|
private final RedisService redisService;
|
|
|
|
|
|
@@ -99,6 +107,8 @@ public class ChatGptAccountServiceImpl implements ChatGptAccountService {
|
|
|
|
|
|
private final ChatgptTrialRecordMapper chatgptTrialRecordMapper;
|
|
|
|
|
|
+ private final ChatgptUserRestrictionMapper chatgptUserRestrictionMapper;
|
|
|
+
|
|
|
@Override
|
|
|
public String getGptSession(String account, String password) {
|
|
|
String officialSession = "";
|
|
|
@@ -665,6 +675,86 @@ public class ChatGptAccountServiceImpl implements ChatGptAccountService {
|
|
|
return DOMAIN;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public String isRestrictions(String userToken, String referer, String userAgent) {
|
|
|
+ //从redis 中查询该usertoken 当日使用次数
|
|
|
+ DateTime now = DateTime.now();
|
|
|
+ DateTime dateTime = DateUtil.beginOfDay(now.toJdkDate());
|
|
|
+ DateTime endOfDayTime = DateUtil.endOfDay(now.toJdkDate());
|
|
|
+ DateTime twelveHoursAgo = DateUtil.offset(now, DateField.HOUR, -6);
|
|
|
+ ChatgptUserRestriction chatgptUserRestriction = chatgptUserRestrictionMapper.selectOne(Wrappers.lambdaQuery(ChatgptUserRestriction.class)
|
|
|
+ .eq(ChatgptUserRestriction::getUserToken, userToken)
|
|
|
+ .ge(ChatgptUserRestriction::getCreatedTime, twelveHoursAgo.toString()).eq(ChatgptUserRestriction::getSuccess, false).last(" limit 1"));
|
|
|
+ if (chatgptUserRestriction != null) {
|
|
|
+ JSONObject result = new JSONObject();
|
|
|
+ result.putOpt("url",chatgptUserRestriction.getUrl());
|
|
|
+ result.putOpt("code",1);
|
|
|
+ return result.toString();
|
|
|
+ }
|
|
|
+ String numStr = redisService.getStr(RedisKey.CHATGPT_USER_RESTRICTION + userToken);
|
|
|
+ if (StringUtils.isNotBlank(numStr) && StringUtils.isNumeric(numStr)) {
|
|
|
+ long num = Long.parseLong(numStr);
|
|
|
+ if (num >= 150) {
|
|
|
+ //触发限制,需要人机验证
|
|
|
+ Integer count = chatgptUserRestrictionMapper.selectCount(Wrappers.lambdaQuery(ChatgptUserRestriction.class).eq(ChatgptUserRestriction::getUserToken, userToken)
|
|
|
+ .ge(ChatgptUserRestriction::getCreatedTime, dateTime.toString()));
|
|
|
+ if (count > 10) {
|
|
|
+ chatgptUserMapper.update(null, Wrappers.lambdaUpdate(ChatgptUser.class).eq(ChatgptUser::getUserToken, userToken).set(ChatgptUser::getIsBlock, 1));
|
|
|
+ }
|
|
|
+ ChatgptUserRestriction restriction = new ChatgptUserRestriction();
|
|
|
+
|
|
|
+ restriction.setRestrictionId(SEQUENCE.nextId().toString());
|
|
|
+ restriction.setUrl(RESTRICTION_URL + "?restrictionId=" + restriction.getRestrictionId() + "&userToken=" + userToken);
|
|
|
+ restriction.setUserToken(userToken);
|
|
|
+ restriction.setUserAgent(userAgent);
|
|
|
+ restriction.setReferer(referer);
|
|
|
+ chatgptUserRestrictionMapper.insert(restriction);
|
|
|
+ JSONObject result = new JSONObject();
|
|
|
+ result.putOpt("url",restriction.getUrl());
|
|
|
+ result.putOpt("code",1);
|
|
|
+ return result.toString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ JSONObject result = new JSONObject();
|
|
|
+ result.putOpt("code",0);
|
|
|
+ redisService.incr(RedisKey.CHATGPT_USER_RESTRICTION + userToken, 1L);
|
|
|
+ long between = DateUtil.between(now, endOfDayTime, DateUnit.SECOND, true);
|
|
|
+ redisService.expire(RedisKey.CHATGPT_USER_RESTRICTION + userToken, between);
|
|
|
+ return result.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean gptRestrictionStatus(String userToken, String restrictionId) {
|
|
|
+ ChatgptUserRestriction chatgptUserRestriction = chatgptUserRestrictionMapper.selectOne(Wrappers.lambdaQuery(ChatgptUserRestriction.class)
|
|
|
+ .eq(ChatgptUserRestriction::getUserToken, userToken)
|
|
|
+ .eq(ChatgptUserRestriction::getRestrictionId, restrictionId)
|
|
|
+ .last(" limit 1"));
|
|
|
+ if (chatgptUserRestriction == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("该验证异常.");
|
|
|
+ }else {
|
|
|
+ return chatgptUserRestriction.getSuccess();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean verifyRestriction(ChatgptUserVerifyDto chatgptUserVerifyDto) {
|
|
|
+ SmsUtil.checkTencentCaptcha(chatgptUserVerifyDto.getTicket(), chatgptUserVerifyDto.getIp(), chatgptUserVerifyDto.getRandStr());
|
|
|
+ // 清除一定次数,以及修改验证状态
|
|
|
+ ChatgptUserRestriction chatgptUserRestriction = chatgptUserRestrictionMapper.selectOne(Wrappers.lambdaQuery(ChatgptUserRestriction.class)
|
|
|
+ .eq(ChatgptUserRestriction::getUserToken, chatgptUserVerifyDto.getUserToken())
|
|
|
+ .eq(ChatgptUserRestriction::getRestrictionId, chatgptUserVerifyDto.getRestrictionId())
|
|
|
+ .eq(ChatgptUserRestriction::getSuccess, false)
|
|
|
+ .last(" limit 1"));
|
|
|
+ if (chatgptUserRestriction != null) {
|
|
|
+ chatgptUserRestriction.setSuccess(true);
|
|
|
+ chatgptUserRestriction.setVerifyUserAgent(chatgptUserVerifyDto.getUserAgent());
|
|
|
+ chatgptUserRestriction.setSuccessTime(new Date());
|
|
|
+ chatgptUserRestrictionMapper.updateById(chatgptUserRestriction);
|
|
|
+ redisService.decr(RedisKey.CHATGPT_USER_RESTRICTION + chatgptUserVerifyDto.getUserToken(), 50L);
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public String trialGpt(String clientIP, String trialToken, String userAgent) {
|