package com.qiwu.miao.chat.service; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.qiwu.miao.chat.bo.QiwuInterfaceBO; import com.qiwu.miao.chat.bo.RequestParams; import com.qiwu.miao.chat.bo.ServerResponse; import com.qiwu.miao.chat.common.Constant; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import java.io.IOException; import java.util.HashMap; import java.util.Map; /** * @author 苗权威 * @dateTime 19-6-22 上午11:09 */ @Service("chatService") public class ChatService { private static final Logger logger = LoggerFactory.getLogger(ChatService.class); public ServerResponse getResponse(RequestParams requestParams){ Long userId = requestParams.getUserId(); if (userId == null) return ServerResponse.createByErrorMsg("THE USER ID IS NULL"); String tts = requestParams.getTts(); if (tts == null || tts.isEmpty()) { return ServerResponse.createByErrorMsg("THE USER WORD IS EMPTY"); } return requestQiwuInterface(userId, tts); } private ServerResponse requestQiwuInterface(Long userId, String tts) { CloseableHttpClient client = null; CloseableHttpResponse response = null; String audioPath; String text; try { client = HttpClientBuilder.create().build(); HttpPost post = new HttpPost(Constant.QIWU_URL); Map params = new HashMap<>(); params.put("user_id", String.valueOf(userId)); params.put("text", tts); params.put("access", "WEIXINXIAOCHENGXU"); String paramsJson = JSON.toJSONString(params); StringEntity body = new StringEntity(paramsJson, "UTF-8"); post.setHeader("Content-Type", "application/json;charset=utf-8"); post.setEntity(body); response = client.execute(post); HttpEntity responseBody = response.getEntity(); if (responseBody != null) { String responseContent = EntityUtils.toString(responseBody); logger.info("齐悟接口返回: "+ responseContent); JSONObject responseJson = JSONObject.parseObject(responseContent); audioPath = (String) responseJson.get("audio"); text = (String) responseJson.get("text"); }else { logger.info("RESPONSE FROM QIWU INTERFACE IS EMPTY"); return ServerResponse.createByErrorMsg("THE RESPONSE FROM QIWU INTERFACE IS EMPTY"); } } catch (IOException e) { logger.info("SEND TO QIWU INTERFACE FAILED: "+e.toString()); return ServerResponse.createByErrorMsg("FIND IOException IN ChatService#requestQiwuInterface"); } finally { try { if (client != null) client.close(); if (response != null) response.close(); } catch (IOException e) { logger.info("CLOSE HTTPCLIENT FALSE:"+e.toString()); return ServerResponse.createByErrorMsg("FAIL TO CLOSE CONNECTION WITH QIWU"); } } return buildResponse(audioPath, text); } private ServerResponse buildResponse(String audioPath, String text) { QiwuInterfaceBO bo = new QiwuInterfaceBO(); bo.setText(text); bo.setUrl(audioPath); return ServerResponse.createBySuccessMsgData("RESPONSE SUCCESS", bo); } }