寻配号功能完整版
This commit is contained in:
parent
4d3feeae45
commit
840c8d3cfb
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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() {
|
|||
<div style={{ marginBottom: '8px' }}>
|
||||
<div style={{ color: '#94a3b8', fontSize: '12px', marginBottom: '4px' }}>联系方式</div>
|
||||
<div style={{ color: '#f97316', fontSize: '13px', padding: '8px', background: 'rgba(249,115,22,0.1)', borderRadius: '6px' }}>
|
||||
联系方式:{showContactId === item.id ? contact : '***'}
|
||||
联系方式:{showContactId === item.id ? contact : '匹配成功后可查看'}
|
||||
</div>
|
||||
|
||||
{/* 是否匹配按钮 - 仅对已登录用户显示 */}
|
||||
{currentUser && (
|
||||
<div style={{ marginTop: '8px' }}>
|
||||
<span style={{
|
||||
padding: '4px 12px',
|
||||
padding: '5px 14px',
|
||||
borderRadius: '4px',
|
||||
fontSize: '12px',
|
||||
background: (item.is_matched === 'matched' || matchedStatus[item.id] === 'matched') ? '#10b981' : '#6b7280',
|
||||
color: '#fff'
|
||||
background: (item.is_matched === 'matched' || matchedStatus[item.id] === 'matched')
|
||||
? ((item.user_id === currentUserId || item.matched_user_id === currentUserId) ? '#10b981' : '#6b7280')
|
||||
: '#6b7280',
|
||||
color: '#fff',
|
||||
opacity: (item.is_matched === 'matched' || matchedStatus[item.id] === 'matched')
|
||||
? ((item.user_id === currentUserId || item.matched_user_id === currentUserId) ? 1 : 0.5)
|
||||
: 1
|
||||
}}>
|
||||
{(item.is_matched === 'matched' || matchedStatus[item.id] === 'matched') ? '已经匹配' : '尚未匹配'}
|
||||
{(item.is_matched === 'matched' || matchedStatus[item.id] === 'matched') && <button onClick={() => item.user_id === currentUserId ? fetchMatchedUserInfo(item.id) : fetchPublisherInfo(item.id)} style={{padding:'4px 10px',borderRadius:'4px',fontSize:'12px',background:(item.user_id === currentUserId || item.matched_user_id === currentUserId) ? '#10b981' : '#6b7280',border:'none',color:(item.user_id === currentUserId || item.matched_user_id === currentUserId) ? '#fb923c' : '#9ca3af',fontWeight:'bold',cursor:(item.user_id === currentUserId || item.matched_user_id === currentUserId) ? 'pointer' : 'not-allowed',marginLeft:'4px'}}>{item.user_id === currentUserId ? '匹配者信息' : '发布者信息'}</button>}
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
|
|
|
|||
Loading…
Reference in New Issue