|
|
@@ -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();
|
|
|
+ }
|
|
|
+
|
|
|
}
|