|
|
@@ -0,0 +1,140 @@
|
|
|
+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.http.Method;
|
|
|
+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;
|
|
|
+import com.cyksj.service.user.UserBindRelationService;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author chan
|
|
|
+ * @date 2024/3/19 11:01
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+@RequiredArgsConstructor
|
|
|
+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;
|
|
|
+
|
|
|
+ private final GroupsRelationMapper groupsRelationMapper;
|
|
|
+
|
|
|
+ private final GroupsMapper groupsMapper;
|
|
|
+
|
|
|
+ private final GoodsDonSkuMapper skuMapper;
|
|
|
+
|
|
|
+ private final UserMapper userMapper;
|
|
|
+
|
|
|
+ private final UserBindRelationService userBindRelationService;
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ 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();
|
|
|
+ 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.method(Method.POST).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:" + e.getMessage());
|
|
|
+ }
|
|
|
+ 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
|
|
|
+ public void upAccount(Account account) {
|
|
|
+ ChatgptSession chatgptSession = chatgptSessionMapper.selectOne(Wrappers.lambdaQuery(ChatgptSession.class).eq(ChatgptSession::getEmail, account.getAccount()));
|
|
|
+ if(chatgptSession != null){
|
|
|
+ if(StringUtils.isNotBlank(account.getGptRefreshToken())){
|
|
|
+ chatgptSession.setOfficialSession(account.getGptRefreshToken());
|
|
|
+ }
|
|
|
+ chatgptSession.setEmail(account.getAccount());
|
|
|
+ chatgptSession.setPassword(account.getPassword());
|
|
|
+ chatgptSession.setIsPlus(1);
|
|
|
+ chatgptSession.setAccountId(account.getId());
|
|
|
+ chatgptSession.setStatus(1);
|
|
|
+ chatgptSessionMapper.updateById(chatgptSession);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getLoginUrl(Long userId, Long relationId) {
|
|
|
+ List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
|
|
|
+ GroupsRelation groupsRelation = groupsRelationMapper.selectOne(Wrappers.lambdaQuery(GroupsRelation.class).in(GroupsRelation::getUserId, userIdList).eq(GroupsRelation::getId, relationId));
|
|
|
+ if (groupsRelation == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("车票不存在");
|
|
|
+ }
|
|
|
+ GroupsTrips groupsTrips = groupsMapper.selectById(groupsRelation.getGroupsId());
|
|
|
+ if (groupsTrips == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("车队异常");
|
|
|
+ }
|
|
|
+ GoodsDonSku goodsDonSku = skuMapper.selectById(groupsTrips.getSkuId());
|
|
|
+ if (goodsDonSku != null && goodsDonSku.getIsMirror()) {
|
|
|
+ ChatgptUser chatgptUser = chatgptUserMapper.selectOne(Wrappers.lambdaQuery(ChatgptUser.class).eq(ChatgptUser::getRelationId, relationId));
|
|
|
+ if (chatgptUser == null) {
|
|
|
+ User user = userMapper.selectById(userId);
|
|
|
+ ChatgptSession chatgptSession = chatgptSessionMapper.selectOne(Wrappers.lambdaQuery(ChatgptSession.class).eq(ChatgptSession::getAccountId, groupsTrips.getAccountId()));
|
|
|
+ //第一次访问
|
|
|
+ chatgptUser = new ChatgptUser();
|
|
|
+ chatgptUser.setExpireTime(groupsRelation.getExpiryTime());
|
|
|
+ chatgptUser.setIsPlus(1);
|
|
|
+ chatgptUser.setSessionId(chatgptSession.getId());
|
|
|
+ chatgptUser.setName(user.getNickname());
|
|
|
+ chatgptUser.setImg(user.getHeadimgurl());
|
|
|
+ chatgptUser.setRelationId(groupsRelation.getId());
|
|
|
+ chatgptUser.setUserToken(UUID.randomUUID().toString());
|
|
|
+ chatgptUserMapper.insert(chatgptUser);
|
|
|
+ }
|
|
|
+
|
|
|
+ return GPT_DOMAIN + "/login_token?access_token=" + chatgptUser.getUserToken();
|
|
|
+ }else {
|
|
|
+ throw BusinessRuntimeException.getInstance("服务器出了点问题");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|