|
|
@@ -2,10 +2,8 @@ package com.goafanti.common.utils;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.goafanti.baiduAI.bo.InputSendChat;
|
|
|
-import com.goafanti.baiduAI.bo.OutSendChatOK;
|
|
|
-import com.goafanti.baiduAI.bo.SseMap;
|
|
|
-import com.goafanti.baiduAI.bo.SseResult;
|
|
|
+import com.goafanti.baiduAI.BaiduChatErrorEnums;
|
|
|
+import com.goafanti.baiduAI.bo.*;
|
|
|
import com.goafanti.common.error.BusinessException;
|
|
|
import okhttp3.*;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
@@ -25,6 +23,7 @@ import java.net.URL;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.Calendar;
|
|
|
import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
@@ -107,6 +106,7 @@ public class BaiduChatUtils {
|
|
|
SseResult res = SseMap.sseEmitterMap.get(inputSendChat.getUserId());
|
|
|
while ((line = reader.readLine())!= null) {
|
|
|
// 每行数据中以 "data:" 开头的部分即为实际的响应数据
|
|
|
+ System.out.println("relut="+line+"。");
|
|
|
if (line.startsWith("data:")) {
|
|
|
String data = line.substring("data:".length()).trim();
|
|
|
JSONObject jsonObject = JSONObject.parseObject(data);
|
|
|
@@ -117,6 +117,10 @@ public class BaiduChatUtils {
|
|
|
// OutSendChatOK out =jsonObject.toJavaObject(OutSendChatOK.class);
|
|
|
// System.out.println(data);
|
|
|
res.sseEmitter.send(data);
|
|
|
+ }else if(line.startsWith("{")) {
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(line);
|
|
|
+ jsonObject.getString("error_code");
|
|
|
+ res.sseEmitter.send(line);
|
|
|
}
|
|
|
}
|
|
|
res.sseEmitter.complete();
|
|
|
@@ -130,6 +134,27 @@ public class BaiduChatUtils {
|
|
|
SseMap.sseEmitterMap.remove(inputSendChat.getUserId());
|
|
|
}
|
|
|
|
|
|
+ private OutSendChat pushResultToOutSendChat(String result) {
|
|
|
+
|
|
|
+ Map<String ,Object> resultMap=JSON.parseObject(result, Map.class);
|
|
|
+ Integer errorCode= (Integer) resultMap.get("error_code");
|
|
|
+ if (errorCode!=null){
|
|
|
+ OutChatER res=new OutChatER();
|
|
|
+ res.setErrorCode(errorCode);
|
|
|
+ if (errorCode.equals("336003")){
|
|
|
+ res.setErrorMsg(BaiduChatErrorEnums.BycodeGetMsg(errorCode)+resultMap.get("error_msg"));
|
|
|
+ }else {
|
|
|
+ res.setErrorMsg(BaiduChatErrorEnums.BycodeGetMsg(errorCode));
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+ }else {
|
|
|
+ OutSendChatOK res=new OutSendChatOK();
|
|
|
+ res=JSON.parseObject(result,OutSendChatOK.class);
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
private void sendJitaoBaiWen(String userId, String s) throws IOException {
|
|
|
SseResult res = SseMap.sseEmitterMap.get(userId);
|
|
|
res.sseEmitter.send(s);
|