Ver Fonte

gpt 登录

chenbiao há 2 anos atrás
pai
commit
5074c4948a

+ 1 - 1
netflix-service/src/main/java/com/cyksj/service/chatgpt/ChatGptAccountService.java

@@ -7,7 +7,7 @@ import com.cyksj.model.entity.Account;
  * @date 2024/3/19 11:01
  */
 public interface ChatGptAccountService {
-    void addAccount(Account account);
+    String addAccount(Account account);
 
     void upAccount(Account account);
 

+ 28 - 2
netflix-service/src/main/java/com/cyksj/service/chatgpt/impl/ChatGptAccountServiceImpl.java

@@ -1,8 +1,12 @@
 package com.cyksj.service.chatgpt.impl;
 
 import cn.hutool.core.lang.UUID;
+import cn.hutool.http.HttpRequest;
+import cn.hutool.http.HttpResponse;
+import cn.hutool.json.JSONObject;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.cyksj.common.exception.BusinessRuntimeException;
+import com.cyksj.common.util.StringUtil;
 import com.cyksj.mapper.*;
 import com.cyksj.model.entity.*;
 import com.cyksj.service.chatgpt.ChatGptAccountService;
@@ -24,6 +28,8 @@ public class ChatGptAccountServiceImpl implements ChatGptAccountService{
     @Value("${chatgpt.domain}")
     private String GPT_DOMAIN;
 
+    private static final String GPT_PROXY = "https://chat-chan-87jztgkf257d.xyhelper.net";
+
     private final ChatgptUserMapper chatgptUserMapper;
 
     private final ChatgptSessionMapper chatgptSessionMapper;
@@ -38,19 +44,39 @@ public class ChatGptAccountServiceImpl implements ChatGptAccountService{
 
 
     @Override
-    public void addAccount(Account account) {
+    public String addAccount(Account account) {
         Integer count = chatgptSessionMapper.selectCount(Wrappers.lambdaQuery(ChatgptSession.class).eq(ChatgptSession::getEmail, account.getAccount()));
         if(count > 0){
             throw BusinessRuntimeException.getInstance("镜像服务该账号已存在.");
         }
+        String refreshToken = "";
+        String officialSession = "";
         ChatgptSession chatgptSession = new ChatgptSession();
-        chatgptSession.setOfficialSession(account.getGptRefreshToken());
+       try {
+           HttpRequest request = new HttpRequest(GPT_PROXY + "/applelogin");
+           request.form("username", account.getAccount());
+           request.form("password", account.getPassword());
+           request.header("Content-Type","application/x-www-form-urlencoded");
+           HttpResponse execute = request.execute();
+           JSONObject loginResult = new JSONObject(execute.body());
+           //detail
+           if (StringUtils.isBlank(loginResult.getStr("accessToken"))) {
+               throw BusinessRuntimeException.getInstance(loginResult.getStr("detail"));
+           }
+           refreshToken = loginResult.getStr("refresh_token");
+           officialSession = execute.body();
+       }catch (Exception e){
+           throw BusinessRuntimeException.getInstance("登录获取token错误 error:" + StringUtil.getErrorText(e));
+       }
+        chatgptSession.setOfficialSession(officialSession);
         chatgptSession.setEmail(account.getAccount());
         chatgptSession.setPassword(account.getPassword());
         chatgptSession.setIsPlus(1);
         chatgptSession.setAccountId(account.getId());
         chatgptSession.setStatus(1);
         chatgptSessionMapper.insert(chatgptSession);
+
+        return refreshToken;
     }
 
     @Override