瀏覽代碼

feat: ChatMessage音频支持 + 语音上传接口

- ChatMessage新增audioUrl字段

- ChatService.sendMessage接受audioUrl参数并存入消息

- ChatController新增POST /api/chat/voice语音上传端点
liaoxg 5 月之前
父節點
當前提交
7990351659

+ 17 - 2
num-server/src/main/java/com/etotem/num/controller/ChatController.java

@@ -3,11 +3,14 @@ package com.etotem.num.controller;
 import com.etotem.num.common.Result;
 import com.etotem.num.entity.ChatMessage;
 import com.etotem.num.service.ChatService;
+import com.etotem.num.service.UploadService;
 import javax.servlet.http.HttpServletRequest;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
 
 import java.util.Collections;
 import java.util.List;
@@ -18,9 +21,11 @@ import java.util.Map;
 public class ChatController {
 
     private final ChatService chatService;
+    private final UploadService uploadService;
 
-    public ChatController(ChatService chatService) {
+    public ChatController(ChatService chatService, UploadService uploadService) {
         this.chatService = chatService;
+        this.uploadService = uploadService;
     }
 
     @PostMapping("/send")
@@ -29,8 +34,9 @@ public class ChatController {
         Long userId = (Long) request.getAttribute("userId");
         Long chartRecordId = ((Number) body.get("chartRecordId")).longValue();
         String query = (String) body.get("query");
+        String audioUrl = (String) body.get("audioUrl");
 
-        String reply = chatService.sendMessage(userId, chartRecordId, query);
+        String reply = chatService.sendMessage(userId, chartRecordId, query, audioUrl);
         return Result.success(Collections.singletonMap("reply", reply));
     }
 
@@ -39,4 +45,13 @@ public class ChatController {
         Long chartRecordId = body.get("chartRecordId");
         return Result.success(chatService.getHistory(chartRecordId));
     }
+
+    @PostMapping("/voice")
+    public Result<Map<String, String>> uploadVoice(HttpServletRequest request,
+                                                    @RequestParam("file") MultipartFile file,
+                                                    @RequestParam("chartRecordId") Long chartRecordId) {
+        Long userId = (Long) request.getAttribute("userId");
+        String url = uploadService.storeAudio(file, userId);
+        return Result.success(Collections.singletonMap("url", url));
+    }
 }

+ 5 - 0
num-server/src/main/java/com/etotem/num/entity/ChatMessage.java

@@ -20,6 +20,9 @@ public class ChatMessage {
     @Column(length = 20, nullable = false)
     private String role; // "user" or "ai"
 
+    @Column(name = "audio_url", length = 500)
+    private String audioUrl;
+
     @Column(columnDefinition = "text")
     private String content;
 
@@ -43,4 +46,6 @@ public class ChatMessage {
     public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; }
     public String getSenderType() { return senderType; }
     public void setSenderType(String senderType) { this.senderType = senderType; }
+    public String getAudioUrl() { return audioUrl; }
+    public void setAudioUrl(String audioUrl) { this.audioUrl = audioUrl; }
 }

+ 2 - 1
num-server/src/main/java/com/etotem/num/service/ChatService.java

@@ -27,7 +27,7 @@ public class ChatService {
      * Send a chat message and get AI response.
      * Checks quota, saves messages, calls Dify, returns response.
      */
-    public String sendMessage(Long userId, Long chartRecordId, String query) {
+    public String sendMessage(Long userId, Long chartRecordId, String query, String audioUrl) {
         // Check daily chat quota
         userService.checkDailyQuota(userId, "chat");
 
@@ -43,6 +43,7 @@ public class ChatService {
         userMsg.setUserId(userId);
         userMsg.setRole("user");
         userMsg.setContent(query);
+        userMsg.setAudioUrl(audioUrl);
         chatMessageRepository.save(userMsg);
 
         // Call Dify with chart context