diff --git a/server/api/share_routes.py b/server/api/share_routes.py index 3372a0b..8fa2453 100644 --- a/server/api/share_routes.py +++ b/server/api/share_routes.py @@ -23,7 +23,8 @@ from core import log_error, log_info class CreateShareRequest(BaseModel): """创建分享请求""" - conversationIds: List[str] + conversationIds: List[str] = [] + messages: Optional[List[dict]] = None # 直接传递消息数据(消息分享模式) passwordHash: str expiresIn: Optional[int] = 604800 # 默认7天(秒) @@ -42,27 +43,68 @@ async def create_share_handler(data: dict): 请求体: { "conversationIds": ["conv-1", "conv-2"], + "messages": [...], // 可选,消息分享模式 "passwordHash": "sha256-hash", "expiresIn": 604800 } """ try: conversation_ids = data.get("conversationIds", []) + messages = data.get("messages") # 消息分享模式 password_hash = data.get("passwordHash", "") expires_in = data.get("expiresIn", 604800) - if not conversation_ids: - raise HTTPException(status_code=400, detail="请选择要分享的对话") - - if len(conversation_ids) > 10: - raise HTTPException(status_code=400, detail="最多分享10个对话") - if not password_hash: raise HTTPException(status_code=400, detail="请设置访问密码") # 获取数据库实例 db = get_db() + # 计算过期时间 + now = int(datetime.now(timezone.utc).timestamp() * 1000) + expires_at = now + (expires_in * 1000) + + # 消息分享模式 + if messages: + if len(messages) == 0: + raise HTTPException(status_code=400, detail="请选择要分享的消息") + + # 创建虚拟对话 + virtual_conv_id = str(uuid.uuid4()) + virtual_conversation = { + "id": virtual_conv_id, + "title": f"分享的消息 ({len(messages)}条)", + "messages": messages, + "createdAt": now, + "updatedAt": now, + } + + # 创建分享记录 + share_data = { + "conversationIds": [virtual_conv_id], + "conversations": [virtual_conversation], + "passwordHash": password_hash, + "expiresAt": expires_at, + } + + # 保存到数据库 + share = db.create_share(share_data) + + log_info(f"[分享] 消息分享创建成功: {share['id']}, 消息数: {len(messages)}") + + return { + "id": share["id"], + "shareUrl": f"/#/share/{share['id']}", + "expiresAt": share["expiresAt"], + } + + # 对话分享模式 + if not conversation_ids: + raise HTTPException(status_code=400, detail="请选择要分享的对话") + + if len(conversation_ids) > 10: + raise HTTPException(status_code=400, detail="最多分享10个对话") + # 获取对话数据(快照) conversations = [] for conv_id in conversation_ids: @@ -81,10 +123,6 @@ async def create_share_handler(data: dict): if not conversations: raise HTTPException(status_code=404, detail="未找到有效的对话") - # 计算过期时间 - now = int(datetime.now(timezone.utc).timestamp() * 1000) - expires_at = now + (expires_in * 1000) - # 创建分享记录 share_data = { "conversationIds": conversation_ids, diff --git a/src/components/chat/MessageList.vue b/src/components/chat/MessageList.vue index 28572ae..f1e5988 100644 --- a/src/components/chat/MessageList.vue +++ b/src/components/chat/MessageList.vue @@ -1,5 +1,29 @@