Bläddra i källkod

GPT session账号导入

zoujiajian 1 år sedan
förälder
incheckning
b291d6e3c2

+ 10 - 0
netflix-dao/src/main/java/com/cyksj/model/entity/ChatgptSession.java

@@ -89,4 +89,14 @@ public class ChatgptSession extends BaseEntity {
     private String relateEmail;
 
 
+    /**
+     * 账号信息
+     */
+    private String accountInfo;
+
+    /**
+     * 会员类型
+     * 普通、Plus、Team、Pro
+     */
+    private String type;
 }

+ 66 - 0
netflix-dao/src/main/java/com/cyksj/model/excel/ExcelGptSessionAccountData.java

@@ -0,0 +1,66 @@
+package com.cyksj.model.excel;
+
+import com.alibaba.excel.annotation.ExcelProperty;
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * 项目名: yhlxj
+ * 文件名: ExcelGptSessionAccountData
+ * 创建者: JavaZou
+ * 创建时间:2024/12/12 16:21
+ */
+@Getter
+@Setter
+public class ExcelGptSessionAccountData {
+	@ExcelProperty("账号")
+	private String email;
+
+	/**
+	 * 密码
+	 */
+	@ExcelProperty("密码")
+	private String password;
+
+
+	/**
+	 * 状态
+	 */
+	@ExcelProperty("状态")
+	private String statusStr;
+
+
+	/**
+	 * 账号类型
+	 *(单选:普通、Plus、Team、Pro)
+	 */
+	@ExcelProperty("账号类型(单选:普通、Plus、Team、Pro)")
+	private String type;
+
+	/**
+	 * 双重验证
+	 */
+	@ExcelProperty("双重验证")
+	private String apiSecret;
+
+	/**
+	 * 备用安全码
+	 */
+	@ExcelProperty("备用安全码")
+	private String verifyCodes;
+
+	/**
+	 * 邮箱密码
+	 */
+	@ExcelProperty("邮箱密码")
+	private String emailPassword;
+
+	@ExcelProperty("关联邮箱")
+	private String relateEmail;
+
+	@ExcelProperty("备注")
+	private String remark;
+
+	@ExcelProperty("officialSession")
+	private String officialSession;
+}

+ 1 - 1
netflix-dao/src/main/java/com/cyksj/model/excel/ExcelPrepareAccountData.java

@@ -13,7 +13,7 @@ public class ExcelPrepareAccountData {
     private String account;
 
     /**
-     * 省份
+     * 密码
      */
     @ExcelProperty("密码")
     private String password;

+ 6 - 0
netflix-dao/src/main/java/com/cyksj/model/views/ChatgptCarSessionView.java

@@ -95,6 +95,12 @@ public class ChatgptCarSessionView {
 	@DbField("cs.relate_email")
 	private String relateEmail;
 
+	/**
+	 * 会员类型
+	 */
+	@DbField("cs.type")
+	private String type;
+
 	@DbField("cs.created_time")
 	private Date createdTime;
 

+ 108 - 2
netflix-web/src/main/java/com/cyksj/web/controller/manage/account/CmsGptCarController.java

@@ -1,13 +1,18 @@
 package com.cyksj.web.controller.manage.account;
 
+import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.RandomUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.http.HttpRequest;
+import cn.hutool.http.HttpResponse;
 import cn.hutool.http.HttpUtil;
 import cn.hutool.json.JSONArray;
 import cn.hutool.json.JSONObject;
+import com.alibaba.excel.EasyExcel;
+import com.alibaba.excel.context.AnalysisContext;
+import com.alibaba.excel.event.AnalysisEventListener;
 import com.alibaba.excel.support.ExcelTypeEnum;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.cyksj.common.exception.BusinessRuntimeException;
@@ -21,6 +26,7 @@ import com.cyksj.model.dto.ChatgptChangeNodesDto;
 import com.cyksj.model.entity.ChatgptSession;
 import com.cyksj.model.entity.SysConfig;
 import com.cyksj.model.excel.ExcelGptMirrorAccountData;
+import com.cyksj.model.excel.ExcelGptSessionAccountData;
 import com.cyksj.model.views.ChatgptCarSessionView;
 import com.cyksj.redis.RedisService;
 import com.cyksj.service.chatgpt.ChatGptAccountService;
@@ -34,9 +40,11 @@ import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
 import java.util.List;
 import java.util.Optional;
 
@@ -128,9 +136,9 @@ public class CmsGptCarController {
 	 * 新增镜像车队账号
 	 */
 	@PostMapping("/post/mirror/car/session")
-	public Result<String> postMirrorCarSession(@RequestBody ChatgptSession chatgptSession) {
+	public Result<String> postMirrorCarSession(@RequestBody ChatgptSession chatgptSession) throws Exception {
 		Integer selectCount = chatgptSessionMapper.selectCount(Wrappers.lambdaQuery(ChatgptSession.class)
-				.eq(ChatgptSession::getCarId, chatgptSession.getCarId()).or().eq(ChatgptSession::getEmail, chatgptSession.getEmail()));
+				.eq(chatgptSession.getCarId() != null, ChatgptSession::getCarId, chatgptSession.getCarId()).or().eq(ChatgptSession::getEmail, chatgptSession.getEmail()));
 		if (selectCount > 0) {
 			throw BusinessRuntimeException.getInstance("请检验车队ID或账号是否已存在");
 		}
@@ -164,6 +172,15 @@ public class CmsGptCarController {
 		refresh(chatgptSession.getId());
 		chatgptSessionMapper.updateById(chatgptSession);
 
+		//同步session的会员类型
+		if (StrUtil.isNotEmpty(chatgptSession.getOfficialSession())) {
+			String officialSession = chatgptSession.getOfficialSession();
+			JSONObject session = Jsons.parseObject(officialSession, JSONObject.class);
+			String accessToken = session.getStr("accessToken");
+			if (StrUtil.isNotEmpty(accessToken)) {
+				getGptAccountInfo(request, chatgptSession.getId(), accessToken);
+			}
+		}
 		return GatewayResponse.SUCCESS.newBuilder().toResult();
 	}
 
@@ -301,4 +318,93 @@ public class CmsGptCarController {
 		}
 	}
 
+	/**
+	 * 获取 gpt用户信息
+	 */
+	@GetMapping("/getGptAccountInfo")
+	public Result<String> getGptAccountInfo(HttpServletRequest request, Long id, String accessToken) {
+		if (StringUtils.isBlank(accessToken)) {
+			ChatgptSession chatgptSession = chatgptSessionMapper.selectById(id);
+			String officialSession = chatgptSession.getOfficialSession();
+			JSONObject jsonObject = new JSONObject(officialSession);
+			String accessTokenJson = jsonObject.getStr("accessToken");
+			if (StringUtils.isBlank(accessTokenJson)) {
+				throw BusinessRuntimeException.getInstance("缺少accessToken");
+			} else {
+				accessToken = accessTokenJson;
+			}
+		}
+		log.info("accessToken:{}", accessToken);
+		HttpRequest httpRequest = new HttpRequest("https://chat-chan-87jztgkf257d.xyhelper.org/backend-api/accounts/check/v4-2023-04-27?timezone_offset_min=0");
+		httpRequest.header("User-Agent", request.getHeader("User-Agent"));
+		httpRequest.header("Authorization", accessToken);
+		try (HttpResponse execute = httpRequest.execute()) {
+			if (execute.getStatus() == 200) {
+				String result = execute.body();
+				chatgptSessionMapper.update(null, Wrappers.lambdaUpdate(ChatgptSession.class).eq(ChatgptSession::getId, id).set(ChatgptSession::getAccountInfo, result));
+				JSONObject resObj = new JSONObject(result);
+				JSONObject entitlement = resObj.getJSONObject("accounts").getJSONObject("default").getJSONObject("entitlement");
+				String subscriptionPlan = entitlement.getStr("subscription_plan");
+				if (StrUtil.isNotEmpty(subscriptionPlan)) {
+					if (subscriptionPlan.contains("chatgptpro")) {
+						chatgptSessionMapper.update(null, Wrappers.lambdaUpdate(ChatgptSession.class).eq(ChatgptSession::getId, id).set(ChatgptSession::getType, "Pro"));
+					}
+					if (subscriptionPlan.contains("chatgptplusplan")) {
+						chatgptSessionMapper.update(null, Wrappers.lambdaUpdate(ChatgptSession.class).eq(ChatgptSession::getId, id).set(ChatgptSession::getType, "Plus"));
+					}
+				} else {
+					chatgptSessionMapper.update(null, Wrappers.lambdaUpdate(ChatgptSession.class).eq(ChatgptSession::getId, id).set(ChatgptSession::getType, "普通"));
+				}
+				JSONArray accountOrdering = resObj.getJSONArray("account_ordering");
+				if (accountOrdering.size() > 1) {
+					chatgptSessionMapper.update(null, Wrappers.lambdaUpdate(ChatgptSession.class).eq(ChatgptSession::getId, id).set(ChatgptSession::getType, "Team"));
+				}
+				return GatewayResponse.SUCCESS.newBuilder().toResult(entitlement.getStr("expires_at"));
+			} else {
+				if (execute.getStatus() == 401) {
+					throw BusinessRuntimeException.getInstance("token失效");
+				}
+				throw BusinessRuntimeException.getInstance(execute.body());
+			}
+		}
+
+	}
+
+	/**
+	 * 下载GPT镜像车队模板
+	 */
+	@GetMapping("/export/gpt/session/template")
+	public void getGptSessionTemplate(HttpServletResponse response) {
+		EasyExcelUtils.createTemplateExcel(response, ExcelGptSessionAccountData.class, "导入GPT镜像车队账号模板", "导入GPT镜像车队账号模板", ExcelTypeEnum.XLSX, null);
+	}
+
+	/**
+	 * 上传GPT镜像车队
+	 */
+	@PostMapping("/import/gpt/session")
+	public Result<String> importGptSession(MultipartFile file) throws IOException {
+		EasyExcel.read(file.getInputStream(), ExcelGptSessionAccountData.class, new AnalysisEventListener<ExcelGptSessionAccountData>() {
+			@Override
+			public void invoke(ExcelGptSessionAccountData data, AnalysisContext analysisContext) {
+				ChatgptSession session = new ChatgptSession();
+				BeanUtil.copyProperties(data, session);
+				session.setStatus(0);
+				if ("有效".equals(data.getStatusStr())) {
+					session.setStatus(1);
+				}
+				//生成gptSession
+				try {
+					postMirrorCarSession(session);
+				} catch (Exception e) {
+
+				}
+			}
+			@Override
+			public void doAfterAllAnalysed(AnalysisContext analysisContext) {
+
+			}
+		}).sheet().doRead();
+		return GatewayResponse.SUCCESS.newBuilder().toResult();
+	}
+
 }