feat: 添加计划表相关API

This commit is contained in:
DLmaster361
2025-08-05 02:14:10 +08:00
parent 6fb4fa7683
commit 4a9c9ab1f3
5 changed files with 172 additions and 2 deletions

View File

@@ -23,7 +23,8 @@ __author__ = "DLmaster361 <DLmaster_361@163.com>"
__license__ = "GPL-3.0 license"
from .scripts import router as scripts_router
from .plan import router as plan_router
from .queue import router as queue_router
from .setting import router as setting_router
__all__ = ["scripts_router", "queue_router", "setting_router"]
__all__ = ["scripts_router", "plan_router", "queue_router", "setting_router"]

82
app/api/plan.py Normal file
View File

@@ -0,0 +1,82 @@
# AUTO_MAA:A MAA Multi Account Management and Automation Tool
# Copyright © 2024-2025 DLmaster361
# This file is part of AUTO_MAA.
# AUTO_MAA is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License,
# or (at your option) any later version.
# AUTO_MAA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
# the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with AUTO_MAA. If not, see <https://www.gnu.org/licenses/>.
# Contact: DLmaster_361@163.com
from fastapi import APIRouter, Body
from core import Config
from models.schema import *
router = APIRouter(prefix="/api/plan", tags=["计划管理"])
@router.post(
"/add", summary="添加计划表", response_model=PlanCreateOut, status_code=200
)
async def add_plan(plan: PlanCreateIn = Body(...)) -> PlanCreateOut:
uid, config = await Config.add_plan(plan.type)
return PlanCreateOut(planId=str(uid), data=await config.toDict())
@router.post(
"/get", summary="查询计划表配置信息", response_model=PlanGetOut, status_code=200
)
async def get_plan(plan: PlanGetIn = Body(...)) -> PlanGetOut:
try:
index, data = await Config.get_plan(plan.planId)
except Exception as e:
return PlanGetOut(code=500, status="error", message=str(e), index=[], data={})
return PlanGetOut(index=index, data=data)
@router.post(
"/update", summary="更新计划表配置信息", response_model=OutBase, status_code=200
)
async def update_plan(plan: PlanUpdateIn = Body(...)) -> OutBase:
try:
await Config.update_plan(plan.planId, plan.data)
except Exception as e:
return OutBase(code=500, status="error", message=str(e))
return OutBase()
@router.post("/delete", summary="删除计划表", response_model=OutBase, status_code=200)
async def delete_plan(plan: PlanDeleteIn = Body(...)) -> OutBase:
try:
await Config.del_plan(plan.planId)
except Exception as e:
return OutBase(code=500, status="error", message=str(e))
return OutBase()
@router.post(
"/order", summary="重新排序计划表", response_model=OutBase, status_code=200
)
async def reorder_plan(plan: PlanReorderIn = Body(...)) -> OutBase:
try:
await Config.reorder_plan(plan.indexList)
except Exception as e:
return OutBase(code=500, status="error", message=str(e))
return OutBase()

View File

@@ -831,6 +831,61 @@ class AppConfig(GlobalConfig):
await queue_config.TimeSet.setOrder([uuid.UUID(_) for _ in index_list])
await self.QueueConfig.save()
async def add_plan(
self, script: Literal["MaaPlan"]
) -> tuple[uuid.UUID, ConfigBase]:
"""添加计划表"""
self.logger.info(f"添加计划表:{script}")
class_book = {"MaaPlan": MaaPlanConfig}
return await self.PlanConfig.add(class_book[script])
async def get_plan(self, plan_id: Optional[str]) -> tuple[list, dict]:
"""获取计划表配置"""
self.logger.info(f"获取计划表配置:{plan_id}")
if plan_id is None:
data = await self.PlanConfig.toDict()
else:
data = await self.PlanConfig.get(uuid.UUID(plan_id))
index = data.pop("instances", [])
return list(index), data
async def update_plan(self, plan_id: str, data: Dict[str, Dict[str, Any]]) -> None:
"""更新计划表配置"""
self.logger.info(f"更新计划表配置:{plan_id}")
uid = uuid.UUID(plan_id)
for group, items in data.items():
for name, value in items.items():
self.logger.debug(
f"更新计划表配置:{plan_id} - {group}.{name} = {value}"
)
await self.PlanConfig[uid].set(group, name, value)
await self.PlanConfig.save()
async def del_plan(self, plan_id: str) -> None:
"""删除计划表配置"""
self.logger.info(f"删除计划表配置:{plan_id}")
await self.PlanConfig.remove(uuid.UUID(plan_id))
async def reorder_plan(self, index_list: list[str]) -> None:
"""重新排序计划表"""
self.logger.info(f"重新排序计划表:{index_list}")
await self.PlanConfig.setOrder([uuid.UUID(_) for _ in index_list])
async def add_queue_item(self, queue_id: str) -> tuple[uuid.UUID, ConfigBase]:
"""添加队列项配置"""

View File

@@ -58,7 +58,7 @@ def main():
logger.info("----------------END----------------")
from fastapi.middleware.cors import CORSMiddleware
from api import scripts_router, queue_router, setting_router
from api import scripts_router, plan_router, queue_router, setting_router
app = FastAPI(
title="AUTO_MAA",
@@ -76,6 +76,7 @@ def main():
)
app.include_router(scripts_router)
app.include_router(plan_router)
app.include_router(queue_router)
app.include_router(setting_router)

View File

@@ -82,6 +82,37 @@ class UserReorderIn(UserInBase):
indexList: List[str] = Field(..., description="用户ID列表按新顺序排列")
class PlanCreateIn(BaseModel):
type: Literal["MaaPlan"]
class PlanCreateOut(OutBase):
planId: str = Field(..., description="新创建的计划ID")
data: Dict[str, Any] = Field(..., description="计划配置数据")
class PlanGetIn(BaseModel):
planId: Optional[str] = Field(None, description="计划ID仅在模式为Single时需要")
class PlanGetOut(OutBase):
index: List[Dict[str, str]] = Field(..., description="计划索引列表")
data: Dict[str, Any] = Field(..., description="计划列表或单个计划数据")
class PlanUpdateIn(BaseModel):
planId: str = Field(..., description="计划ID")
data: Dict[str, Dict[str, Any]] = Field(..., description="计划更新数据")
class PlanDeleteIn(BaseModel):
planId: str = Field(..., description="计划ID")
class PlanReorderIn(BaseModel):
indexList: List[str] = Field(..., description="计划ID列表按新顺序排列")
class QueueCreateOut(OutBase):
queueId: str = Field(..., description="新创建的队列ID")
data: Dict[str, Any] = Field(..., description="队列配置数据")