fix: 修复寻配号两个问题
1. matched_count为0:添加match_collections_count计算 2. 留言功能失效:迁移到/api/seek/comment和/api/seek/comments/ News.jsx: 0.0.4 → 0.0.5 seek.py: 0.0.5 → 0.0.6
This commit is contained in:
parent
c326d184f9
commit
877f314617
|
|
@ -1,6 +1,6 @@
|
||||||
# seek - 寻配号路由
|
# seek - 寻配号路由
|
||||||
# Version: 0.0.5 (2026-04-24)
|
# Version: 0.0.6 (2026-04-24)
|
||||||
# 更新:路由顺序调整,将/{seek_id}移到具体路由之后
|
# 更新:添加matched_count计算,留言功能迁移到seek.py
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, Query, HTTPException
|
from fastapi import APIRouter, Depends, Query, HTTPException
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
@ -11,6 +11,7 @@ from app.core.database import get_db
|
||||||
from app.core.auth import get_current_user
|
from app.core.auth import get_current_user
|
||||||
from app.models.seek_info import SeekInfo
|
from app.models.seek_info import SeekInfo
|
||||||
from app.models.models import User, Collection
|
from app.models.models import User, Collection
|
||||||
|
from app.routers.information import match_collections_count
|
||||||
from app.utils.coolbot_matcher import (
|
from app.utils.coolbot_matcher import (
|
||||||
match_pattern,
|
match_pattern,
|
||||||
match_self_collections_count,
|
match_self_collections_count,
|
||||||
|
|
@ -103,6 +104,12 @@ def get_seek_list(
|
||||||
user_name = user.f01_01_name if user else '匿名用户'
|
user_name = user.f01_01_name if user else '匿名用户'
|
||||||
|
|
||||||
# 构建响应(不实时计算网络匹配,网络匹配在点击时再查)
|
# 构建响应(不实时计算网络匹配,网络匹配在点击时再查)
|
||||||
|
# 计算自有匹配数量
|
||||||
|
matched_count = 0
|
||||||
|
if item.expect_number and len(item.expect_number) == 10:
|
||||||
|
if current_user and current_user.f99_90_id:
|
||||||
|
matched_count = match_collections_count(db, current_user.f99_90_id, item.expect_number)
|
||||||
|
|
||||||
result.append(SeekInfoResponse(
|
result.append(SeekInfoResponse(
|
||||||
id=item.id,
|
id=item.id,
|
||||||
user_id=item.user_id,
|
user_id=item.user_id,
|
||||||
|
|
@ -123,7 +130,7 @@ def get_seek_list(
|
||||||
created_at=item.created_at,
|
created_at=item.created_at,
|
||||||
updated_at=item.updated_at,
|
updated_at=item.updated_at,
|
||||||
user_name=user_name,
|
user_name=user_name,
|
||||||
matched_count=0,
|
matched_count=matched_count,
|
||||||
network_matched_count=0
|
network_matched_count=0
|
||||||
))
|
))
|
||||||
|
|
||||||
|
|
@ -404,3 +411,68 @@ def delete_seek(
|
||||||
seek.status = "deleted"
|
seek.status = "deleted"
|
||||||
db.commit()
|
db.commit()
|
||||||
return {"message": "删除成功"}
|
return {"message": "删除成功"}
|
||||||
|
|
||||||
|
# ============ 留言功能(从information.py迁移)============
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
class CommentRequest(BaseModel):
|
||||||
|
information_id: str
|
||||||
|
content: str
|
||||||
|
|
||||||
|
@router.post("/comment")
|
||||||
|
def add_comment(
|
||||||
|
request: CommentRequest,
|
||||||
|
current_user = Depends(get_current_user),
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
"""添加留言 - 支持seek_info表"""
|
||||||
|
if not current_user:
|
||||||
|
raise HTTPException(status_code=401, detail="请先登录")
|
||||||
|
|
||||||
|
# 查询seek_info表
|
||||||
|
info = db.query(SeekInfo).filter(SeekInfo.id == request.information_id).first()
|
||||||
|
if not info:
|
||||||
|
raise HTTPException(status_code=404, detail="寻配号不存在")
|
||||||
|
|
||||||
|
# 创建留言
|
||||||
|
from app.models.models import InformationComment
|
||||||
|
comment = InformationComment(
|
||||||
|
information_id=request.information_id,
|
||||||
|
user_id=current_user.f99_90_id,
|
||||||
|
content=request.content
|
||||||
|
)
|
||||||
|
db.add(comment)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
return {
|
||||||
|
"message": "留言成功",
|
||||||
|
"comment": {
|
||||||
|
"id": comment.id,
|
||||||
|
"content": comment.content,
|
||||||
|
"user_name": current_user.f01_01_name,
|
||||||
|
"user_avatar": current_user.avatar,
|
||||||
|
"created_at": comment.created_at
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@router.get("/comments/{information_id}")
|
||||||
|
def get_comments(
|
||||||
|
information_id: str,
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
"""获取寻配号的评论列表"""
|
||||||
|
from app.models.models import InformationComment
|
||||||
|
comments = db.query(InformationComment).filter(
|
||||||
|
InformationComment.information_id == information_id
|
||||||
|
).order_by(InformationComment.created_at.desc()).all()
|
||||||
|
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
"id": c.id,
|
||||||
|
"content": c.content,
|
||||||
|
"user_name": c.user.f01_01_name if c.user else '匿名用户',
|
||||||
|
"user_avatar": c.user.avatar if c.user else None,
|
||||||
|
"created_at": c.created_at
|
||||||
|
}
|
||||||
|
for c in comments
|
||||||
|
]
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
/**
|
/**
|
||||||
* News - 资讯列表页面
|
* News - 资讯列表页面
|
||||||
* Version: 0.0.4 (2026-04-24)
|
* Version: 0.0.5 (2026-04-24)
|
||||||
* 更新:API已统一到/api/seek/*,移除列表实时网络匹配计算,留言时间显示
|
* 更新:API统一到/api/seek/*,留言功能迁移
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import React, { useState, useEffect } from 'react'
|
import React, { useState, useEffect } from 'react'
|
||||||
|
|
@ -145,7 +145,7 @@ export default function News() {
|
||||||
const fetchComments = async (infoId) => {
|
const fetchComments = async (infoId) => {
|
||||||
// 每次都重新获取评论
|
// 每次都重新获取评论
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`${API_BASE}/api/information/comments/${infoId}`)
|
const res = await fetch(`${API_BASE}/api/seek/comments/${infoId}`)
|
||||||
const data = await res.json()
|
const data = await res.json()
|
||||||
setComments(prev => ({...prev, [infoId]: data || []}))
|
setComments(prev => ({...prev, [infoId]: data || []}))
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
@ -159,7 +159,7 @@ export default function News() {
|
||||||
const token = localStorage.getItem('token')
|
const token = localStorage.getItem('token')
|
||||||
if (!token) { alert('请先登录'); return }
|
if (!token) { alert('请先登录'); return }
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`${API_BASE}/api/information/comment`, {
|
const res = await fetch(`${API_BASE}/api/seek/comment`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' },
|
headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ information_id: infoId, content: commentText })
|
body: JSON.stringify({ information_id: infoId, content: commentText })
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue