from fastapi import APIRouter, BackgroundTasks, HTTPException from wechat_auto.models.activity import ActivityModel, TaskStatus from wechat_auto.core.task_scheduler import task_scheduler from wechat_auto.utils.logger import logger router = APIRouter() @router.post("/api/publish", response_model=dict) async def publish_activity(activity: ActivityModel, background_tasks: BackgroundTasks): logger.info(f"收到发布活动请求: {activity.title}") result = await task_scheduler.publish_activity(activity) return { "code": 200 if result["status"] == "success" else 500, "message": "任务已提交" if result["status"] == "success" else "任务失败", "data": result } @router.get("/api/task/{task_id}", response_model=dict) async def get_task_status(task_id: str): task = task_scheduler.get_task_status(task_id) if not task: raise HTTPException(status_code=404, detail="任务不存在") return { "code": 200, "data": task } @router.get("/api/tasks", response_model=dict) async def list_tasks(): tasks = task_scheduler.list_tasks() return { "code": 200, "data": tasks } @router.get("/api/health") async def health_check(): return { "status": "ok", "service": "wechat_auto" }