|
@@ -6,18 +6,11 @@ import com.goafanti.baiduAI.BaiduChatErrorEnums;
|
|
|
import com.goafanti.baiduAI.bo.*;
|
|
import com.goafanti.baiduAI.bo.*;
|
|
|
import com.goafanti.common.error.BusinessException;
|
|
import com.goafanti.common.error.BusinessException;
|
|
|
import okhttp3.*;
|
|
import okhttp3.*;
|
|
|
-import org.apache.http.client.ClientProtocolException;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
|
|
-import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
|
|
|
-import retrofit2.http.GET;
|
|
|
|
|
-
|
|
|
|
|
-import java.io.BufferedReader;
|
|
|
|
|
-import java.io.IOException;
|
|
|
|
|
-import java.io.InputStream;
|
|
|
|
|
-import java.io.InputStreamReader;
|
|
|
|
|
|
|
+
|
|
|
|
|
+import java.io.*;
|
|
|
import java.net.HttpURLConnection;
|
|
import java.net.HttpURLConnection;
|
|
|
import java.net.MalformedURLException;
|
|
import java.net.MalformedURLException;
|
|
|
import java.net.ProtocolException;
|
|
import java.net.ProtocolException;
|
|
@@ -25,7 +18,6 @@ import java.net.URL;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.Calendar;
|
|
import java.util.Calendar;
|
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
|
|
@@ -52,45 +44,12 @@ public class BaiduChatUtils {
|
|
|
.build();
|
|
.build();
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
|
- public String getBaiduAccessToken() throws IOException {
|
|
|
|
|
- MediaType mediaType = MediaType.parse("application/json");
|
|
|
|
|
- RequestBody body = RequestBody.create(mediaType, "");
|
|
|
|
|
- StringBuffer url= new StringBuffer(BAIDU_ACCESSTOKEN_URL)
|
|
|
|
|
- .append("client_id=").append(baiduApiKey).append("&client_secret=").append(baiduSecretKey);
|
|
|
|
|
- Request request = new Request.Builder()
|
|
|
|
|
- .url(url.toString())
|
|
|
|
|
- .method("POST", body)
|
|
|
|
|
- .addHeader("Content-Type", "application/json")
|
|
|
|
|
- .addHeader("Accept", "application/json")
|
|
|
|
|
- .build();
|
|
|
|
|
- Response response = HTTP_CLIENT.newCall(request).execute();
|
|
|
|
|
- String result=response.body().string();
|
|
|
|
|
- HashMap<String,Object> map = JSON.parseObject(result, HashMap.class);
|
|
|
|
|
- String accessToken = map.get("access_token").toString();
|
|
|
|
|
- LoggerUtils.debug(getClass(),"获取accessToken="+accessToken);
|
|
|
|
|
- return accessToken;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public String sendBaiduAI(InputSendChat in) throws IOException{
|
|
|
|
|
- String accessToken = getRedisBaiduAccessToken();
|
|
|
|
|
- MediaType mediaType = MediaType.parse("application/json");
|
|
|
|
|
- RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(in));
|
|
|
|
|
- Request request = new Request.Builder()
|
|
|
|
|
- .url(BAIDU_CHAT_WXYY_URL + accessToken)
|
|
|
|
|
- .method("POST", body)
|
|
|
|
|
- .addHeader("Content-Type", "application/json")
|
|
|
|
|
- .build();
|
|
|
|
|
- Response response = HTTP_CLIENT.newCall(request).execute();
|
|
|
|
|
- String result=response.body().string();
|
|
|
|
|
- return result;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
@Async
|
|
@Async
|
|
|
public void sendBaiduAiStream(InputSendChat inputSendChat) throws IOException {
|
|
public void sendBaiduAiStream(InputSendChat inputSendChat) throws IOException {
|
|
|
SseResult res = null;
|
|
SseResult res = null;
|
|
|
BufferedReader reader=null;
|
|
BufferedReader reader=null;
|
|
|
InputStreamReader inputStreamReader=null;
|
|
InputStreamReader inputStreamReader=null;
|
|
|
|
|
+ OutputStream outputStream=null;
|
|
|
try {
|
|
try {
|
|
|
String baidu_url=BAIDU_CHAT_WXYY_URL+getRedisBaiduAccessToken();
|
|
String baidu_url=BAIDU_CHAT_WXYY_URL+getRedisBaiduAccessToken();
|
|
|
URL url = new URL(baidu_url);
|
|
URL url = new URL(baidu_url);
|
|
@@ -104,31 +63,30 @@ public class BaiduChatUtils {
|
|
|
String requestBody=JSON.toJSONString(inputSendChat);
|
|
String requestBody=JSON.toJSONString(inputSendChat);
|
|
|
byte[] postData = requestBody.getBytes(StandardCharsets.UTF_8);
|
|
byte[] postData = requestBody.getBytes(StandardCharsets.UTF_8);
|
|
|
connection.setRequestProperty("Content-Length", String.valueOf(postData.length));
|
|
connection.setRequestProperty("Content-Length", String.valueOf(postData.length));
|
|
|
- connection.getOutputStream().write(postData);
|
|
|
|
|
|
|
+ outputStream =connection.getOutputStream();
|
|
|
|
|
+ outputStream.write(postData);
|
|
|
InputStream responseStream = connection.getInputStream();
|
|
InputStream responseStream = connection.getInputStream();
|
|
|
inputStreamReader = new InputStreamReader(responseStream, "UTF-8");
|
|
inputStreamReader = new InputStreamReader(responseStream, "UTF-8");
|
|
|
reader = new BufferedReader(inputStreamReader);
|
|
reader = new BufferedReader(inputStreamReader);
|
|
|
String line;
|
|
String line;
|
|
|
- res = SseMap.sseEmitterMap.get(inputSendChat.getUserId());
|
|
|
|
|
|
|
+ res = SseMap.sseEmitterMap.get(inputSendChat.getUserId());
|
|
|
while ((line = reader.readLine())!= null) {
|
|
while ((line = reader.readLine())!= null) {
|
|
|
// 每行数据中以 "data:" 开头的部分即为实际的响应数据
|
|
// 每行数据中以 "data:" 开头的部分即为实际的响应数据
|
|
|
if (StringUtils.isNotBlank(line)){
|
|
if (StringUtils.isNotBlank(line)){
|
|
|
- System.out.println(line);
|
|
|
|
|
if (line.startsWith("data:")) {
|
|
if (line.startsWith("data:")) {
|
|
|
String data = line.substring("data:".length()).trim();
|
|
String data = line.substring("data:".length()).trim();
|
|
|
JSONObject jsonObject = JSONObject.parseObject(data);
|
|
JSONObject jsonObject = JSONObject.parseObject(data);
|
|
|
Boolean isEnd = jsonObject.getBoolean("is_end");
|
|
Boolean isEnd = jsonObject.getBoolean("is_end");
|
|
|
-// OutSendChatOK out =jsonObject.toJavaObject(OutSendChatOK.class);
|
|
|
|
|
-// System.out.println(data);
|
|
|
|
|
res.sseEmitter.send(data);
|
|
res.sseEmitter.send(data);
|
|
|
if(isEnd){
|
|
if(isEnd){
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
|
|
+ //错误返回格式{"error_code":110,"error_msg":"Access token invalid or no longer valid"}
|
|
|
}else if(line.startsWith("{")) {
|
|
}else if(line.startsWith("{")) {
|
|
|
JSONObject jsonObject = JSONObject.parseObject(line);
|
|
JSONObject jsonObject = JSONObject.parseObject(line);
|
|
|
Integer errorCode = jsonObject.getInteger("error_code");
|
|
Integer errorCode = jsonObject.getInteger("error_code");
|
|
|
|
|
+ OutChatER out=new OutChatER();
|
|
|
if (errorCode!=null){
|
|
if (errorCode!=null){
|
|
|
- OutChatER out=new OutChatER();
|
|
|
|
|
out.setError_code(errorCode);
|
|
out.setError_code(errorCode);
|
|
|
if (errorCode.equals("336003")){
|
|
if (errorCode.equals("336003")){
|
|
|
out.setError_msg(BaiduChatErrorEnums.BycodeGetMsg(errorCode)+jsonObject.getString("error_msg"));
|
|
out.setError_msg(BaiduChatErrorEnums.BycodeGetMsg(errorCode)+jsonObject.getString("error_msg"));
|
|
@@ -136,11 +94,12 @@ public class BaiduChatUtils {
|
|
|
out.setError_msg(BaiduChatErrorEnums.BycodeGetMsg(errorCode));
|
|
out.setError_msg(BaiduChatErrorEnums.BycodeGetMsg(errorCode));
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- res.sseEmitter.send(JSON.toJSONString(errorCode));
|
|
|
|
|
|
|
+ String errorStr=JSON.toJSONString(out);
|
|
|
|
|
+ LoggerUtils.debug(getClass(),errorStr);
|
|
|
|
|
+ res.sseEmitter.send(errorStr);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
res.sseEmitter.complete();
|
|
res.sseEmitter.complete();
|
|
|
} catch (MalformedURLException e) {
|
|
} catch (MalformedURLException e) {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
@@ -151,6 +110,7 @@ public class BaiduChatUtils {
|
|
|
LoggerUtils.debug(getClass(),"前端网页已关闭");
|
|
LoggerUtils.debug(getClass(),"前端网页已关闭");
|
|
|
}finally {
|
|
}finally {
|
|
|
inputStreamReader.close();
|
|
inputStreamReader.close();
|
|
|
|
|
+ outputStream.close();
|
|
|
reader.close();
|
|
reader.close();
|
|
|
res.sseEmitter.complete();
|
|
res.sseEmitter.complete();
|
|
|
SseMap.sseEmitterMap.remove(inputSendChat.getUserId());
|
|
SseMap.sseEmitterMap.remove(inputSendChat.getUserId());
|
|
@@ -159,6 +119,31 @@ public class BaiduChatUtils {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ public String getBaiduAccessToken() throws IOException {
|
|
|
|
|
+ MediaType mediaType = MediaType.parse("application/json");
|
|
|
|
|
+ RequestBody body = RequestBody.create(mediaType, "");
|
|
|
|
|
+ StringBuffer url= new StringBuffer(BAIDU_ACCESSTOKEN_URL)
|
|
|
|
|
+ .append("client_id=").append(baiduApiKey).append("&client_secret=").append(baiduSecretKey);
|
|
|
|
|
+ Request request = new Request.Builder()
|
|
|
|
|
+ .url(url.toString())
|
|
|
|
|
+ .method("POST", body)
|
|
|
|
|
+ .addHeader("Content-Type", "application/json")
|
|
|
|
|
+ .addHeader("Accept", "application/json")
|
|
|
|
|
+ .build();
|
|
|
|
|
+ Response response = HTTP_CLIENT.newCall(request).execute();
|
|
|
|
|
+ String result=response.body().string();
|
|
|
|
|
+ HashMap<String,Object> map = JSON.parseObject(result, HashMap.class);
|
|
|
|
|
+ String accessToken = map.get("access_token").toString();
|
|
|
|
|
+ LoggerUtils.debug(getClass(),"获取accessToken="+accessToken);
|
|
|
|
|
+ return accessToken;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
private void sendJitaoBaiWen(String userId, String s) throws IOException {
|
|
private void sendJitaoBaiWen(String userId, String s) throws IOException {
|
|
|
SseResult res = SseMap.sseEmitterMap.get(userId);
|
|
SseResult res = SseMap.sseEmitterMap.get(userId);
|
|
|
res.sseEmitter.send(s);
|
|
res.sseEmitter.send(s);
|
|
@@ -166,9 +151,8 @@ public class BaiduChatUtils {
|
|
|
|
|
|
|
|
|
|
|
|
|
private String getRedisBaiduAccessToken() {
|
|
private String getRedisBaiduAccessToken() {
|
|
|
- String redisAccessToken=null;
|
|
|
|
|
|
|
+ String redisAccessToken;
|
|
|
String redisTime=redisUtil.getString("baiduAccessTime");
|
|
String redisTime=redisUtil.getString("baiduAccessTime");
|
|
|
- //没有
|
|
|
|
|
if (redisTime !=null){
|
|
if (redisTime !=null){
|
|
|
Calendar cal = Calendar.getInstance();
|
|
Calendar cal = Calendar.getInstance();
|
|
|
Long redisAccessTime=Long.valueOf(redisTime);
|
|
Long redisAccessTime=Long.valueOf(redisTime);
|
|
@@ -179,7 +163,6 @@ public class BaiduChatUtils {
|
|
|
LoggerUtils.debug(getClass(),"accessToken过期,重新获取!");
|
|
LoggerUtils.debug(getClass(),"accessToken过期,重新获取!");
|
|
|
}else {
|
|
}else {
|
|
|
redisAccessToken= redisUtil.getString("baiduAccessToken");
|
|
redisAccessToken= redisUtil.getString("baiduAccessToken");
|
|
|
- LoggerUtils.debug(getClass(),"accessToken从redis获取成功!");
|
|
|
|
|
}
|
|
}
|
|
|
}else {
|
|
}else {
|
|
|
redisAccessToken=pushRedisBaiduAccessToken();
|
|
redisAccessToken=pushRedisBaiduAccessToken();
|