diff --git a/backend/app/routers/information.py b/backend/app/routers/information.py index 1135447..cb7ae31 100644 --- a/backend/app/routers/information.py +++ b/backend/app/routers/information.py @@ -718,9 +718,15 @@ def match_seek( if not info: raise HTTPException(status_code=404, detail="寻配号信息不存在") + # 检查是否已被匹配(一个寻号只能被一个用户匹配) + if info.is_matched == "matched": + raise HTTPException(status_code=400, detail="该寻号已被其他用户匹配") + # 更新匹配状态 info.is_matched = "matched" info.matched_user_id = current_user.f99_90_id + # 保存匹配者的联系方式 + info.matched_contact = request.contact or '' # 更新发布寻号者的内容,显示有藏品被匹配 original_content = info.content or "" @@ -797,3 +803,82 @@ def get_comments( } for c in comments ] + + +# ============ 获取匹配者信息 ============ +@router.get("/seek/matched-user/{info_id}") +def get_matched_user( + info_id: str, + current_user: User = Depends(get_current_user), + db: Session = Depends(get_db) +): + """获取寻号的匹配者信息(仅发布者可见)""" + info = db.query(Information).filter( + Information.id == info_id, + Information.info_type == "seek" + ).first() + + if not info: + raise HTTPException(status_code=404, detail="寻配号信息不存在") + + # 只有发布者可以看到匹配者信息 + if info.user_id != current_user.f99_90_id: + raise HTTPException(status_code=403, detail="无权访问") + + if not info.matched_user_id: + return {"message": "暂无匹配者"} + + # 获取匹配者信息 + matched_user = db.query(User).filter(User.f99_90_id == info.matched_user_id).first() + if not matched_user: + return {"message": "匹配者不存在"} + + return { + "matched_user_id": info.matched_user_id, + "user_name": matched_user.f01_01_name, + "phone": matched_user.phone, + "matched_contact": info.matched_contact, + "matched_at": info.updated_at.isoformat() if info.updated_at else None + } + + +# ============ 获取发布者信息 ============ +@router.get("/seek/publisher/{info_id}") +def get_publisher_info( + info_id: str, + current_user: User = Depends(get_current_user), + db: Session = Depends(get_db) +): + """获取寻号的发布者信息(仅匹配者可见)""" + info = db.query(Information).filter( + Information.id == info_id, + Information.info_type == "seek" + ).first() + + if not info: + raise HTTPException(status_code=404, detail="寻配号信息不存在") + + # 只有匹配者可以看到发布者信息 + if info.matched_user_id != current_user.f99_90_id: + raise HTTPException(status_code=403, detail="无权访问") + + # 获取发布者信息 + publisher = db.query(User).filter(User.f99_90_id == info.user_id).first() + if not publisher: + return {"message": "发布者不存在"} + + # 从content中解析联系方式 + contact = '' + if info.content: + import re + match = re.search(r'联系方式[:\s]*(.+?)(?:\n|$)', info.content) + if match: + contact = match.group(1).strip() + + return { + "user_id": info.user_id, + "user_name": publisher.f01_01_name, + "phone": publisher.phone, + "contact": contact, + "created_at": info.created_at.isoformat() if info.created_at else None + } diff --git a/frontend/src/pages/News.jsx b/frontend/src/pages/News.jsx index 19aa7b3..353c23b 100644 --- a/frontend/src/pages/News.jsx +++ b/frontend/src/pages/News.jsx @@ -124,6 +124,24 @@ export default function News() { alert('操作失败: ' + e.message) } } + const fetchMatchedUserInfo = async (infoId) => { + const token = localStorage.getItem('token') + if (!token) return + try { + const res = await fetch(`${API_BASE}/api/information/seek/matched-user/${infoId}`, { headers: { Authorization: `Bearer ${token}` } }) + const data = await res.json() + if (data.matched_user_id) alert(`匹配者: ${data.user_name}, 联系方式: ${data.matched_contact || '未提供'}`) + } catch (e) { console.error(e) } + } + const fetchPublisherInfo = async (infoId) => { + const token = localStorage.getItem('token') + if (!token) return + try { + const res = await fetch(`${API_BASE}/api/information/seek/publisher/${infoId}`, { headers: { Authorization: `Bearer ${token}` } }) + const data = await res.json() + if (data.user_id) alert(`发布者: ${data.user_name}, 联系方式: ${data.contact || '未提供'}`) + } catch (e) { console.error(e) } + } const fetchMatchCollections = async (infoId) => { const token = localStorage.getItem('token') if (!token) { alert('请先登录'); return } @@ -465,20 +483,26 @@ export default function News() {