|
|
@@ -1,12 +1,16 @@
|
|
|
package com.cyksj.service.chatgpt.impl;
|
|
|
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
import cn.hutool.core.lang.UUID;
|
|
|
import cn.hutool.http.HttpRequest;
|
|
|
import cn.hutool.http.HttpResponse;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
import cn.hutool.http.Method;
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.cyksj.common.exception.BusinessRuntimeException;
|
|
|
+import com.cyksj.common.util.J11HttpC;
|
|
|
import com.cyksj.common.util.StringUtil;
|
|
|
import com.cyksj.mapper.*;
|
|
|
import com.cyksj.model.entity.*;
|
|
|
@@ -16,9 +20,15 @@ import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.core.io.ByteArrayResource;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import javax.imageio.stream.FileImageOutputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* @author chan
|
|
|
@@ -115,8 +125,8 @@ public class ChatGptAccountServiceImpl implements ChatGptAccountService{
|
|
|
GoodsDonSku goodsDonSku = skuMapper.selectById(groupsTrips.getSkuId());
|
|
|
if (goodsDonSku != null && goodsDonSku.getIsMirror()) {
|
|
|
ChatgptUser chatgptUser = chatgptUserMapper.selectOne(Wrappers.lambdaQuery(ChatgptUser.class).eq(ChatgptUser::getRelationId, relationId));
|
|
|
+ User user = userMapper.selectById(userId);
|
|
|
if (chatgptUser == null) {
|
|
|
- User user = userMapper.selectById(userId);
|
|
|
ChatgptSession chatgptSession = chatgptSessionMapper.selectOne(Wrappers.lambdaQuery(ChatgptSession.class).eq(ChatgptSession::getAccountId, groupsTrips.getAccountId()));
|
|
|
//第一次访问
|
|
|
chatgptUser = new ChatgptUser();
|
|
|
@@ -124,10 +134,16 @@ public class ChatGptAccountServiceImpl implements ChatGptAccountService{
|
|
|
chatgptUser.setIsPlus(1);
|
|
|
chatgptUser.setSessionId(chatgptSession.getId());
|
|
|
chatgptUser.setName(user.getNickname());
|
|
|
- chatgptUser.setImg(user.getHeadimgurl());
|
|
|
+
|
|
|
+ chatgptUser.setImg(getWxImg(user.getHeadimgurl(), user));
|
|
|
chatgptUser.setRelationId(groupsRelation.getId());
|
|
|
chatgptUser.setUserToken(UUID.randomUUID().toString());
|
|
|
chatgptUserMapper.insert(chatgptUser);
|
|
|
+ }else {
|
|
|
+ chatgptUser.setName(user.getNickname());
|
|
|
+ chatgptUser.setImg(getWxImg(user.getHeadimgurl(), user));
|
|
|
+ chatgptUser.setExpireTime(groupsRelation.getExpiryTime());
|
|
|
+ chatgptUserMapper.updateById(chatgptUser);
|
|
|
}
|
|
|
|
|
|
return GPT_DOMAIN + "/login_token?access_token=" + chatgptUser.getUserToken();
|
|
|
@@ -136,5 +152,37 @@ public class ChatGptAccountServiceImpl implements ChatGptAccountService{
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 更换wx头像至oss
|
|
|
+ *
|
|
|
+ */
|
|
|
+ private String getWxImg(String headimgurl, User user) {
|
|
|
+ //2023-03-24 先此方案处理。因会占用不少io资源,需修改
|
|
|
+ if ("wx".equals(user.getRegisterEnv()) && headimgurl.contains("thirdwx.qlogo.cn")) {
|
|
|
+ File file = null;
|
|
|
+ try {
|
|
|
+ byte[] body = HttpUtil.downloadBytes(headimgurl);
|
|
|
+ file = File.createTempFile("wxheadimg-" + user.getId(), ".jpeg"); // 创建临时文件
|
|
|
+ try (FileImageOutputStream imageOutput = new FileImageOutputStream(file)) {
|
|
|
+ imageOutput.write(body, 0, body.length);
|
|
|
+ } // 自动关闭FileImageOutputStream
|
|
|
+
|
|
|
+ Map<String, Object> paramMap = new HashMap<>();
|
|
|
+ paramMap.put("file", FileUtil.file(file.getAbsolutePath()));
|
|
|
+ String result = HttpUtil.post("https://files.liuliangbang.vip/pic/ups", paramMap);
|
|
|
+ return JSONUtil.parseObj(result).getJSONObject("value").getJSONArray("saved").getJSONObject(0).getJSONObject("info").getStr("cdnUrl");
|
|
|
+ } catch (Exception ex) {
|
|
|
+ log.error("上传微信头像错误!msg:{}", StringUtil.getErrorText(ex));
|
|
|
+ return "./avatars.png";
|
|
|
+ } finally {
|
|
|
+ if (file != null && file.exists()) {
|
|
|
+ FileUtil.del(file); // 确保最终文件被删除,避免产生垃圾文件
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return user.getHeadimgurl();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|