Spaces:
Running
Running
优化任务榜
Browse files- router_posts.py +8 -7
- router_tasks.py +41 -14
router_posts.py
CHANGED
|
@@ -306,14 +306,14 @@ async def get_post_comments(post_id: str):
|
|
| 306 |
"""
|
| 307 |
获取帖子评论
|
| 308 |
"""
|
| 309 |
-
comments_db = db.load_data("comments.json", default_data=
|
| 310 |
users_db = db.load_data("users.json", default_data={})
|
| 311 |
|
| 312 |
# users_db 已经是 {account: user_info} 格式,直接使用
|
| 313 |
user_map = users_db
|
| 314 |
|
| 315 |
-
#
|
| 316 |
-
post_comments =
|
| 317 |
|
| 318 |
# 附加用户信息
|
| 319 |
result = []
|
|
@@ -336,7 +336,7 @@ async def add_post_comment(post_id: str, content: str, current_user: str = Depen
|
|
| 336 |
raise HTTPException(status_code=400, detail="评论内容不能为空")
|
| 337 |
|
| 338 |
posts_db = db.load_data("posts.json", default_data=[])
|
| 339 |
-
comments_db = db.load_data("comments.json", default_data=
|
| 340 |
|
| 341 |
# 检查帖子是否存在
|
| 342 |
post_exists = any(p["id"] == post_id for p in posts_db)
|
|
@@ -345,14 +345,15 @@ async def add_post_comment(post_id: str, content: str, current_user: str = Depen
|
|
| 345 |
|
| 346 |
new_comment = {
|
| 347 |
"id": f"comment_{int(time.time())}_{uuid.uuid4().hex[:6]}",
|
| 348 |
-
"target_id": post_id,
|
| 349 |
-
"target_type": "post",
|
| 350 |
"author": current_user,
|
| 351 |
"content": content.strip(),
|
| 352 |
"created_at": int(time.time())
|
| 353 |
}
|
| 354 |
|
| 355 |
-
comments_db
|
|
|
|
|
|
|
|
|
|
| 356 |
db.save_data("comments.json", comments_db)
|
| 357 |
|
| 358 |
# 更新帖子评论数
|
|
|
|
| 306 |
"""
|
| 307 |
获取帖子评论
|
| 308 |
"""
|
| 309 |
+
comments_db = db.load_data("comments.json", default_data={})
|
| 310 |
users_db = db.load_data("users.json", default_data={})
|
| 311 |
|
| 312 |
# users_db 已经是 {account: user_info} 格式,直接使用
|
| 313 |
user_map = users_db
|
| 314 |
|
| 315 |
+
# comments_db 是 {item_id: [comments]} 格式
|
| 316 |
+
post_comments = comments_db.get(post_id, [])
|
| 317 |
|
| 318 |
# 附加用户信息
|
| 319 |
result = []
|
|
|
|
| 336 |
raise HTTPException(status_code=400, detail="评论内容不能为空")
|
| 337 |
|
| 338 |
posts_db = db.load_data("posts.json", default_data=[])
|
| 339 |
+
comments_db = db.load_data("comments.json", default_data={})
|
| 340 |
|
| 341 |
# 检查帖子是否存在
|
| 342 |
post_exists = any(p["id"] == post_id for p in posts_db)
|
|
|
|
| 345 |
|
| 346 |
new_comment = {
|
| 347 |
"id": f"comment_{int(time.time())}_{uuid.uuid4().hex[:6]}",
|
|
|
|
|
|
|
| 348 |
"author": current_user,
|
| 349 |
"content": content.strip(),
|
| 350 |
"created_at": int(time.time())
|
| 351 |
}
|
| 352 |
|
| 353 |
+
# comments_db 是 {item_id: [comments]} 格式
|
| 354 |
+
post_comments = comments_db.get(post_id, [])
|
| 355 |
+
post_comments.insert(0, new_comment)
|
| 356 |
+
comments_db[post_id] = post_comments
|
| 357 |
db.save_data("comments.json", comments_db)
|
| 358 |
|
| 359 |
# 更新帖子评论数
|
router_tasks.py
CHANGED
|
@@ -345,7 +345,9 @@ async def create_task(task: TaskCreate, current_user: str = Depends(require_auth
|
|
| 345 |
@router.put("/api/tasks/{task_id}")
|
| 346 |
async def update_task(task_id: str, update_data: TaskUpdate, current_user: str = Depends(require_auth)):
|
| 347 |
"""
|
| 348 |
-
更新任务(仅发布者可操作
|
|
|
|
|
|
|
| 349 |
"""
|
| 350 |
tasks_db = db.load_data("tasks.json", default_data=[])
|
| 351 |
|
|
@@ -354,27 +356,52 @@ async def update_task(task_id: str, update_data: TaskUpdate, current_user: str =
|
|
| 354 |
if task.get("publisher") != current_user:
|
| 355 |
raise HTTPException(status_code=403, detail="无权修改他人任务")
|
| 356 |
|
| 357 |
-
|
| 358 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 359 |
|
| 360 |
-
if update_data.title is not None:
|
| 361 |
task["title"] = update_data.title
|
| 362 |
-
|
|
|
|
| 363 |
task["description"] = update_data.description
|
|
|
|
| 364 |
if update_data.referenceImages is not None:
|
| 365 |
task["reference_images"] = update_data.referenceImages[:6]
|
| 366 |
-
|
|
|
|
| 367 |
task["reference_link"] = update_data.referenceLink
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
if
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 376 |
|
| 377 |
db.save_data("tasks.json", tasks_db)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 378 |
return {"status": "success"}
|
| 379 |
|
| 380 |
raise HTTPException(status_code=404, detail="任务不存在")
|
|
|
|
| 345 |
@router.put("/api/tasks/{task_id}")
|
| 346 |
async def update_task(task_id: str, update_data: TaskUpdate, current_user: str = Depends(require_auth)):
|
| 347 |
"""
|
| 348 |
+
更新任务(仅发布者可操作)
|
| 349 |
+
- open状态:可修改所有字段
|
| 350 |
+
- in_progress/submitted状态:只能修改标题/描述/参考图/链接,不能改价格,并通知接单人
|
| 351 |
"""
|
| 352 |
tasks_db = db.load_data("tasks.json", default_data=[])
|
| 353 |
|
|
|
|
| 356 |
if task.get("publisher") != current_user:
|
| 357 |
raise HTTPException(status_code=403, detail="无权修改他人任务")
|
| 358 |
|
| 359 |
+
status = task.get("status")
|
| 360 |
+
if status not in ["open", "in_progress", "submitted"]:
|
| 361 |
+
raise HTTPException(status_code=400, detail="当前状态不允许修改")
|
| 362 |
+
|
| 363 |
+
# 记录修改内容(用于通知)
|
| 364 |
+
changes = []
|
| 365 |
|
| 366 |
+
if update_data.title is not None and update_data.title != task.get("title"):
|
| 367 |
task["title"] = update_data.title
|
| 368 |
+
changes.append("标题")
|
| 369 |
+
if update_data.description is not None and update_data.description != task.get("description"):
|
| 370 |
task["description"] = update_data.description
|
| 371 |
+
changes.append("描述")
|
| 372 |
if update_data.referenceImages is not None:
|
| 373 |
task["reference_images"] = update_data.referenceImages[:6]
|
| 374 |
+
changes.append("参考图")
|
| 375 |
+
if update_data.referenceLink is not None and update_data.referenceLink != task.get("reference_link"):
|
| 376 |
task["reference_link"] = update_data.referenceLink
|
| 377 |
+
changes.append("参考链接")
|
| 378 |
+
|
| 379 |
+
# 价格和订金只能在open状态修改
|
| 380 |
+
if status == "open":
|
| 381 |
+
if update_data.totalPrice is not None:
|
| 382 |
+
task["total_price"] = update_data.totalPrice
|
| 383 |
+
task["deposit_amount"] = int(update_data.totalPrice * task["deposit_ratio"] / 100)
|
| 384 |
+
changes.append("价格")
|
| 385 |
+
if update_data.depositRatio is not None and update_data.depositRatio in [10, 20, 30, 50]:
|
| 386 |
+
task["deposit_ratio"] = update_data.depositRatio
|
| 387 |
+
task["deposit_amount"] = int(task["total_price"] * update_data.depositRatio / 100)
|
| 388 |
+
changes.append("订金比例")
|
| 389 |
+
if update_data.deadline is not None:
|
| 390 |
+
task["deadline"] = update_data.deadline
|
| 391 |
+
changes.append("截止日期")
|
| 392 |
|
| 393 |
db.save_data("tasks.json", tasks_db)
|
| 394 |
+
|
| 395 |
+
# 📢 如果有接单人,发送通知
|
| 396 |
+
if task.get("assignee") and changes:
|
| 397 |
+
add_notification(
|
| 398 |
+
task["assignee"],
|
| 399 |
+
"task_updated",
|
| 400 |
+
f"任务「{task['title']}」已更新:{'、'.join(changes)}",
|
| 401 |
+
related_id=task_id,
|
| 402 |
+
sender=current_user
|
| 403 |
+
)
|
| 404 |
+
|
| 405 |
return {"status": "success"}
|
| 406 |
|
| 407 |
raise HTTPException(status_code=404, detail="任务不存在")
|