feat: 接入数据库的用户概念,写入查找都要使用user_id
This commit is contained in:
parent
3421d0db47
commit
244907b152
|
|
@ -28,10 +28,10 @@ upload_dir.mkdir(exist_ok=True)
|
||||||
# ── 会话管理 ─────────────────────────────────────────────────────
|
# ── 会话管理 ─────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
async def get_conversations_handler():
|
async def get_conversations_handler(user_id: str = "default"):
|
||||||
"""获取所有对话处理器"""
|
"""获取所有对话处理器"""
|
||||||
db = get_db()
|
db = get_db()
|
||||||
return db.list_conversations()
|
return db.list_conversations(user_id)
|
||||||
|
|
||||||
|
|
||||||
async def get_conversation_handler(conversation_id: str):
|
async def get_conversation_handler(conversation_id: str):
|
||||||
|
|
|
||||||
|
|
@ -173,8 +173,8 @@ async def get_models():
|
||||||
|
|
||||||
|
|
||||||
@app.get("/api/chat-ui/conversations")
|
@app.get("/api/chat-ui/conversations")
|
||||||
async def get_conversations():
|
async def get_conversations(user_id: str = "default"):
|
||||||
return await get_conversations_handler()
|
return await get_conversations_handler(user_id)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/api/chat-ui/conversations/{conversation_id}")
|
@app.get("/api/chat-ui/conversations/{conversation_id}")
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { getAuthHeaders } from './request';
|
import { getAuthHeaders } from './request';
|
||||||
|
import { useAuthStore } from '@/stores/auth';
|
||||||
import type { Conversation, Message, MessageContent, ConversationSettings } from '@/types/chat';
|
import type { Conversation, Message, MessageContent, ConversationSettings } from '@/types/chat';
|
||||||
|
|
||||||
// API 端点
|
// API 端点
|
||||||
|
|
@ -81,10 +82,11 @@ function transformMessage(backendMsg: BackendMessage): Message {
|
||||||
/**
|
/**
|
||||||
* 将前端对话格式转换为后端格式
|
* 将前端对话格式转换为后端格式
|
||||||
*/
|
*/
|
||||||
function toBackendFormat(conversation: Partial<Conversation>): Record<string, unknown> {
|
function toBackendFormat(conversation: Partial<Conversation>, userId?: string): Record<string, unknown> {
|
||||||
const data: Record<string, unknown> = {};
|
const data: Record<string, unknown> = {};
|
||||||
|
|
||||||
if (conversation.id !== undefined) data.id = conversation.id;
|
if (conversation.id !== undefined) data.id = conversation.id;
|
||||||
|
if (userId !== undefined) data.user_id = userId; // 后端使用下划线命名
|
||||||
if (conversation.title !== undefined) data.title = conversation.title;
|
if (conversation.title !== undefined) data.title = conversation.title;
|
||||||
if (conversation.createdAt !== undefined) data.createdAt = conversation.createdAt;
|
if (conversation.createdAt !== undefined) data.createdAt = conversation.createdAt;
|
||||||
if (conversation.updatedAt !== undefined) data.updatedAt = conversation.updatedAt;
|
if (conversation.updatedAt !== undefined) data.updatedAt = conversation.updatedAt;
|
||||||
|
|
@ -112,7 +114,15 @@ export const conversationApi = {
|
||||||
* 获取所有对话列表(不含消息内容)
|
* 获取所有对话列表(不含消息内容)
|
||||||
*/
|
*/
|
||||||
async fetchConversations(): Promise<Conversation[]> {
|
async fetchConversations(): Promise<Conversation[]> {
|
||||||
const response = await fetch(ENDPOINTS.CONVERSATIONS, {
|
const authStore = useAuthStore();
|
||||||
|
const userId = authStore.userId;
|
||||||
|
|
||||||
|
// 构建 URL,添加 user_id 查询参数
|
||||||
|
const url = userId
|
||||||
|
? `${ENDPOINTS.CONVERSATIONS}?user_id=${encodeURIComponent(userId)}`
|
||||||
|
: ENDPOINTS.CONVERSATIONS;
|
||||||
|
|
||||||
|
const response = await fetch(url, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: getHeaders(),
|
headers: getHeaders(),
|
||||||
});
|
});
|
||||||
|
|
@ -149,10 +159,13 @@ export const conversationApi = {
|
||||||
* 创建新对话
|
* 创建新对话
|
||||||
*/
|
*/
|
||||||
async createConversation(data: Partial<Conversation>): Promise<Conversation> {
|
async createConversation(data: Partial<Conversation>): Promise<Conversation> {
|
||||||
|
const authStore = useAuthStore();
|
||||||
|
const userId = authStore.userId || undefined;
|
||||||
|
|
||||||
const response = await fetch(ENDPOINTS.CONVERSATIONS, {
|
const response = await fetch(ENDPOINTS.CONVERSATIONS, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: getHeaders(),
|
headers: getHeaders(),
|
||||||
body: JSON.stringify(toBackendFormat(data)),
|
body: JSON.stringify(toBackendFormat(data, userId)),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue