feat(content): 资讯详情 阅读数 + 留言
- 迁移0017:content_items.read_count + content_comments 表
- ContentRepository:_to_dict 加 read_count;incr_read_count/content_comments/add_content_comment
- rbac_opc:GET /opc/content/{id} 自增阅读数;GET/POST /opc/content/{id}/comments(发布需 opc_member,带昵称/头像)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -91,9 +91,8 @@ async def opc_policy(
|
||||
async def opc_content(
|
||||
type: str = "news",
|
||||
db: Database = Depends(get_db),
|
||||
_u: dict = Depends(require_roles("opc_member")),
|
||||
):
|
||||
"""C 端资讯:type ∈ policy(政策)/news(资讯)/skill(技能)/dynamic(动态),仅 published。"""
|
||||
"""C 端资讯:type ∈ policy(政策)/news(资讯)/skill(技能)/dynamic(动态),仅 published。公开浏览。"""
|
||||
return {"items": await db.content.list(ctype=type, status="published")}
|
||||
|
||||
|
||||
@@ -101,14 +100,50 @@ async def opc_content(
|
||||
async def opc_content_detail(
|
||||
content_id: str,
|
||||
db: Database = Depends(get_db),
|
||||
_u: dict = Depends(require_roles("opc_member")),
|
||||
):
|
||||
item = await db.content.get(content_id)
|
||||
if item is None or item.get("status") != "published":
|
||||
raise HTTPException(status_code=404, detail="内容不存在")
|
||||
# 阅读数自增(每次详情浏览 +1)
|
||||
if item.get("status") == "published":
|
||||
await db.content.incr_read_count(content_id)
|
||||
item["read_count"] = (item.get("read_count") or 0) + 1
|
||||
return item
|
||||
|
||||
|
||||
@router.get("/content/{content_id}/comments", summary="资讯留言列表")
|
||||
async def opc_content_comments(
|
||||
content_id: str,
|
||||
db: Database = Depends(get_db),
|
||||
):
|
||||
return {"items": await db.content.content_comments(content_id)}
|
||||
|
||||
|
||||
class ContentCommentBody(BaseModel):
|
||||
content: str
|
||||
|
||||
|
||||
@router.post("/content/{content_id}/comments", summary="发表留言")
|
||||
async def opc_content_add_comment(
|
||||
content_id: str,
|
||||
body: ContentCommentBody,
|
||||
db: Database = Depends(get_db),
|
||||
user: dict = Depends(require_roles("opc_member")),
|
||||
):
|
||||
if not body.content.strip():
|
||||
raise HTTPException(status_code=400, detail="留言不能为空")
|
||||
item = await db.content.get(content_id)
|
||||
if item is None or item.get("status") != "published":
|
||||
raise HTTPException(status_code=404, detail="内容不存在")
|
||||
# 取当前用户资料(昵称/头像)用于留言展示
|
||||
me = await db.users.get_by_id(user["id"]) or {}
|
||||
actor = {"id": user["id"], "username": user.get("username", ""),
|
||||
"nickname": (me.get("nickname") or user.get("username", "")),
|
||||
"avatar": me.get("avatar", "")}
|
||||
comment = await db.content.add_content_comment(content_id, actor, body.content.strip())
|
||||
return {"ok": True, "comment": comment}
|
||||
|
||||
|
||||
@router.get("/finance", summary="财务流水")
|
||||
async def opc_finance(
|
||||
db: Database = Depends(get_db),
|
||||
|
||||
Reference in New Issue
Block a user