kongdeqiang
2026-10-12 7c687dd3c8fba9f7632f5e29d94a708e5f497abd
src/main/java/com/custom/webui/controller/ChatController.java
@@ -1,13 +1,17 @@
package com.custom.webui.controller;
import com.custom.webui.dto.req.ChatCreateRequest;
import com.custom.webui.dto.req.ChatGroupMoveRequest;
import com.custom.webui.dto.req.ChatUpdateRequest;
import com.custom.webui.dto.resp.ChatDTO;
import com.custom.webui.dto.resp.Result;
import com.custom.webui.security.CurrentUser;
import com.custom.webui.service.ChatExportService;
import com.custom.webui.service.ChatService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@@ -18,6 +22,8 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
/** 会话 CRUD 接口,对应侧边栏的对话列表。 */
@@ -27,11 +33,20 @@
public class ChatController {
    private final ChatService chatService;
    private final ChatExportService chatExportService;
    /** 当前用户的全部会话,按最近活跃排序 */
    /**
     * 当前用户的会话列表。
     *
     * <p>排序由后端固定为「置顶优先,其次最近活跃」—— 前端只负责按时间分组,
     * 不重新排序,避免两边规则不一致导致列表顺序跳动。
     *
     * @param archived false(默认)= 主列表;true = 归档列表
     */
    @GetMapping
    public Result<List<ChatDTO>> list() {
        return Result.success(chatService.listByUser(CurrentUser.requireUserId()));
    public Result<List<ChatDTO>> list(
            @RequestParam(value = "archived", defaultValue = "false") boolean archived) {
        return Result.success(chatService.listByUser(CurrentUser.requireUserId(), archived));
    }
    /**
@@ -40,13 +55,17 @@
     * <p>同时匹配会话标题与消息内容 —— 用户往往记得「聊过什么」却想不起原话,
     * 而标题只是首条提问的截断,只搜标题会漏掉大量结果。
     *
     * @param q     关键词;为空时返回空列表
     * @param limit 最多返回条数,默认 30,上限 100
     * @param q        关键词;为空时返回空列表
     * @param limit    最多返回条数,默认 30,上限 100
     * @param archived 搜索范围:默认只在未归档的对话里搜,
     *                 在「归档对话」界面里搜则传 true 只搜归档的
     */
    @GetMapping("/search")
    public Result<List<ChatDTO>> search(@RequestParam("q") String q,
                                       @RequestParam(value = "limit", defaultValue = "30") int limit) {
        return Result.success(chatService.search(CurrentUser.requireUserId(), q, limit));
    public Result<List<ChatDTO>> search(
            @RequestParam("q") String q,
            @RequestParam(value = "limit", defaultValue = "30") int limit,
            @RequestParam(value = "archived", defaultValue = "false") boolean archived) {
        return Result.success(chatService.search(CurrentUser.requireUserId(), q, limit, archived));
    }
    /** 新建会话 */
@@ -56,7 +75,12 @@
        return Result.success(chatService.create(CurrentUser.requireUserId(), request));
    }
    /** 修改会话标题或模型参数(temperature / top_p / systemPrompt / modelName) */
    /**
     * 修改会话:标题、模型参数、置顶、归档。
     *
     * <p>字段为 null 表示不改,因此「重命名」「置顶」「归档」都复用这一个接口,
     * 前端只提交需要变化的字段。
     */
    @PutMapping("/{id}")
    public Result<ChatDTO> update(@PathVariable Long id,
                                 @Valid @RequestBody ChatUpdateRequest request) {
@@ -69,4 +93,49 @@
        chatService.delete(CurrentUser.requireUserId(), id);
        return Result.success();
    }
    /**
     * 把会话移入 / 移出分组。
     *
     * <p>body 里 {@code groupId: null} 表示移出分组(回到未分组、继续按时间归类)。
     * 单独一个端点是因为 {@code PUT /api/chats/{id}} 的约定是
     * 「字段为 null = 不修改」,两者在同一个字段上会打架。
     */
    @PutMapping("/{id}/group")
    public Result<ChatDTO> moveToGroup(@PathVariable Long id,
                                       @RequestBody(required = false) ChatGroupMoveRequest request) {
        return Result.success(chatService.moveToGroup(
                CurrentUser.requireUserId(), id, request == null ? null : request.getGroupId()));
    }
    /**
     * 导出会话为 json / txt / pdf。
     *
     * <p>用 {@link ResponseEntity} 而不是统一的 {@code Result} 包装:
     * 导出要返回的是**文件本体**,套一层 JSON 就没法直接保存了
     * (这与 SSE 不走统一包装是同一个理由)。
     */
    @GetMapping("/{id}/export")
    public ResponseEntity<byte[]> export(
            @PathVariable Long id,
            @RequestParam(value = "format", defaultValue = "txt") String format) {
        ChatExportService.ExportFile file = chatExportService.export(
                CurrentUser.requireUserId(), id, ChatExportService.Format.parse(format));
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_TYPE, file.contentType())
                // filename 用 ASCII 兜底,filename* 用 UTF-8 编码给支持的标准浏览器用。
                // 中文标题不这么处理会出现乱码或下载失败(RFC 5987)。
                .header(HttpHeaders.CONTENT_DISPOSITION,
                        "attachment; filename=\"export." + extOf(file.filename()) + "\"; filename*=UTF-8''"
                                + URLEncoder.encode(file.filename(), StandardCharsets.UTF_8)
                                .replace("+", "%20"))
                .header(HttpHeaders.CACHE_CONTROL, "no-store")
                .body(file.data());
    }
    private String extOf(String filename) {
        int dot = filename.lastIndexOf('.');
        return dot >= 0 ? filename.substring(dot + 1) : "bin";
    }
}