From 4a9c9ab1f3c1e7560aeef655e98490bda38a9b38 Mon Sep 17 00:00:00 2001 From: DLmaster361 Date: Tue, 5 Aug 2025 02:14:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E8=AE=A1=E5=88=92?= =?UTF-8?q?=E8=A1=A8=E7=9B=B8=E5=85=B3API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/__init__.py | 3 +- app/api/plan.py | 82 ++++++++++++++++++++++++++++++++++++++++++++ app/core/config.py | 55 +++++++++++++++++++++++++++++ app/main.py | 3 +- app/models/schema.py | 31 +++++++++++++++++ 5 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 app/api/plan.py diff --git a/app/api/__init__.py b/app/api/__init__.py index ded081d..2e221b6 100644 --- a/app/api/__init__.py +++ b/app/api/__init__.py @@ -23,7 +23,8 @@ __author__ = "DLmaster361 " __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"] diff --git a/app/api/plan.py b/app/api/plan.py new file mode 100644 index 0000000..dafe00d --- /dev/null +++ b/app/api/plan.py @@ -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 . + +# 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() diff --git a/app/core/config.py b/app/core/config.py index a9ffb91..5f6a307 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -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]: """添加队列项配置""" diff --git a/app/main.py b/app/main.py index 24a94b0..450fe13 100644 --- a/app/main.py +++ b/app/main.py @@ -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) diff --git a/app/models/schema.py b/app/models/schema.py index 7a86ff1..156ce8a 100644 --- a/app/models/schema.py +++ b/app/models/schema.py @@ -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="队列配置数据")