|
|
@@ -0,0 +1,103 @@
|
|
|
+package com.yw.android.client.action;
|
|
|
+
|
|
|
+import com.yw.core.clientImpl.model.ResultVo;
|
|
|
+import com.yw.core.clientImpl.service.RequestAbs;
|
|
|
+import com.yw.core.session.AppSession;
|
|
|
+import com.yw.core.utils.StringUtil;
|
|
|
+import com.yw.master1.eu.ai.model.AiMessage;
|
|
|
+import com.yw.master1.eu.ai.model.AiSession;
|
|
|
+import com.yw.master1.eu.ai.model.vo.AiMessageVo;
|
|
|
+import com.yw.master1.eu.ai.service.AiApi;
|
|
|
+import com.yw.master1.eu.ai.service.AiMessageService;
|
|
|
+import com.yw.master1.eu.ai.service.AiSessionService;
|
|
|
+import net.sf.json.JSONObject;
|
|
|
+import org.springframework.context.ApplicationContext;
|
|
|
+import org.springframework.util.ObjectUtils;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 小程序AI客服接口
|
|
|
+ *
|
|
|
+ * @author: wangpx
|
|
|
+ * @date: 2025-02-19 10:31
|
|
|
+ */
|
|
|
+public class MiniAppAIGetReceiveAction extends RequestAbs {
|
|
|
+ public final String serviceId = "miniapp_ai_getReceive";
|
|
|
+ private ApplicationContext cxt = null;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getResult() {
|
|
|
+ // 获取请求的参数
|
|
|
+ String param = getParameters();
|
|
|
+ JSONObject json = JSONObject.fromObject(param);
|
|
|
+ String prompt = json.getString("prompt");
|
|
|
+ Long sessionId = Long.valueOf(json.getString("sessionId"));
|
|
|
+ Long userId = Long.valueOf(json.getString("userId"));
|
|
|
+ Integer sort = Integer.parseInt(json.getString("sort"));
|
|
|
+
|
|
|
+ if (null == cxt) {
|
|
|
+ cxt = AppSession.getApplicationContext();
|
|
|
+ }
|
|
|
+
|
|
|
+ String receiveContent = null;
|
|
|
+ String returnCode = "-1";
|
|
|
+ String returnMsg = "failed";
|
|
|
+
|
|
|
+ AiMessageVo messageVo = new AiMessageVo();
|
|
|
+ AiMessage message = null;
|
|
|
+ try {
|
|
|
+ AiMessageService aiMessageService = (AiMessageService) cxt.getBean("aiMessageService");
|
|
|
+ AiSessionService aiSessionService = (AiSessionService) cxt.getBean("aiSessionService");
|
|
|
+ String sendContent = prompt;
|
|
|
+
|
|
|
+ // 获取返回消息
|
|
|
+ message = AiApi.sendAsk(sendContent);
|
|
|
+ receiveContent = message.getReceiveContent();
|
|
|
+
|
|
|
+ AiSession session = new AiSession();
|
|
|
+ if (sessionId == 0) { // 新开会话
|
|
|
+ String sessionName = sendContent;
|
|
|
+ if (sendContent.length() > 20) sessionName = sendContent.substring(0, 20);
|
|
|
+ sessionId = StringUtil.getSeq();
|
|
|
+ session.setSessionId(sessionId);
|
|
|
+ session.setSessionName(sessionName);
|
|
|
+ session.setUserId(userId);
|
|
|
+ aiSessionService.insert(session);
|
|
|
+
|
|
|
+ messageVo.setSessionName(sessionName);
|
|
|
+ } else {
|
|
|
+ session.setSessionId(sessionId);
|
|
|
+ session.setChangeTime(new Date());
|
|
|
+ aiSessionService.updateChangeTime(session);
|
|
|
+ }
|
|
|
+
|
|
|
+ message.setMessageId(StringUtil.getSeq());
|
|
|
+ message.setSendContent(prompt);
|
|
|
+// message.setReceiveContent(receiveContent);
|
|
|
+ message.setSessionId(sessionId);
|
|
|
+ message.setSort(sort);
|
|
|
+ aiMessageService.insert(message);
|
|
|
+
|
|
|
+ returnCode = "1";
|
|
|
+ returnMsg = "success";
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ receiveContent = "系统错误,请联系管理员";
|
|
|
+ }
|
|
|
+ messageVo.setReceiveContent(receiveContent);
|
|
|
+ messageVo.setSessionId(sessionId);
|
|
|
+ // 返回数据
|
|
|
+ ResultVo rv = new ResultVo();
|
|
|
+ rv.setReturnCode(returnCode);
|
|
|
+ rv.setReturnMsg(returnMsg);
|
|
|
+ rv.setReturnParams(messageVo);
|
|
|
+ return JSONObject.fromObject(rv, config).toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getServiceId() {
|
|
|
+ return serviceId;
|
|
|
+ }
|
|
|
+}
|