From b60225cb74e9730c282cbac071b590c83aae6949 Mon Sep 17 00:00:00 2001 From: DLmaster361 Date: Mon, 4 Aug 2025 19:44:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=85=A8=E5=B1=80?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=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/setting.py | 49 ++++++++++++++++++++++++++++++++++++++++ app/core/config.py | 19 +++++++++++++++- app/main.py | 3 ++- app/models/ConfigBase.py | 14 ++++++++---- app/models/schema.py | 8 +++++++ 6 files changed, 89 insertions(+), 7 deletions(-) create mode 100644 app/api/setting.py diff --git a/app/api/__init__.py b/app/api/__init__.py index 7cb2bc3..247175d 100644 --- a/app/api/__init__.py +++ b/app/api/__init__.py @@ -23,5 +23,6 @@ __author__ = "DLmaster361 " __license__ = "GPL-3.0 license" from .scripts import router as scripts_router +from .setting import router as setting_router -__all__ = ["scripts_router"] +__all__ = ["scripts_router", "setting_router"] diff --git a/app/api/setting.py b/app/api/setting.py new file mode 100644 index 0000000..9a10f8b --- /dev/null +++ b/app/api/setting.py @@ -0,0 +1,49 @@ +# 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/setting", tags=["全局设置"]) + + +@router.post("/get", summary="查询配置", response_model=SettingGetOut, status_code=200) +async def get_scripts() -> SettingGetOut: + """查询配置""" + + try: + data = await Config.get_setting() + except Exception as e: + return SettingGetOut(code=500, status="error", message=str(e), data={}) + return SettingGetOut(data=data) + + +@router.post("/update", summary="更新配置", response_model=BaseOut, status_code=200) +async def update_script(script: SettingUpdateIn = Body(...)) -> BaseOut: + """更新配置""" + + try: + await Config.update_setting(script.data) + except Exception as e: + return BaseOut(code=500, status="error", message=str(e)) + return BaseOut() diff --git a/app/core/config.py b/app/core/config.py index 8568056..4a57df1 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -561,7 +561,7 @@ class AppConfig(GlobalConfig): VERSION = "4.5.0.1" def __init__(self) -> None: - super().__init__() + super().__init__(if_save_multi_config=False) # self.app_path = Path(sys.argv[0]).resolve().parent # self.app_path_sys = Path(sys.argv[0]).resolve() @@ -671,6 +671,23 @@ class AppConfig(GlobalConfig): await self.ScriptConfig.remove(uuid.UUID(script_id)) + async def get_setting(self) -> Dict[str, Any]: + """获取全局设置""" + + self.logger.info("获取全局设置") + + return await self.toDict(ignore_multi_config=True) + + async def update_setting(self, data: Dict[str, Dict[str, Any]]) -> None: + """更新全局设置""" + + self.logger.info(f"更新全局设置") + + for group, items in data.items(): + for name, value in items.items(): + self.logger.debug(f"更新全局设置 - {group}.{name} = {value}") + await self.set(group, name, value) + # def check_data(self) -> None: # """检查用户数据文件并处理数据文件版本更新""" diff --git a/app/main.py b/app/main.py index cedebb6..e974284 100644 --- a/app/main.py +++ b/app/main.py @@ -27,7 +27,7 @@ from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware -from api import scripts_router +from api import scripts_router, setting_router from core import Config from utils import get_logger @@ -51,6 +51,7 @@ app.add_middleware( app.include_router(scripts_router) +app.include_router(setting_router) def is_admin() -> bool: diff --git a/app/models/ConfigBase.py b/app/models/ConfigBase.py index 60255fd..5a09a60 100644 --- a/app/models/ConfigBase.py +++ b/app/models/ConfigBase.py @@ -188,9 +188,10 @@ class ConfigBase: 子配置项可以是 `MultipleConfig` 的实例。 """ - def __init__(self): + def __init__(self, if_save_multi_config: bool = True): self.file: None | Path = None + self.if_save_multi_config = if_save_multi_config async def connect(self, path: Path): """ @@ -253,7 +254,7 @@ class ConfigBase: if self.file: await self.save() - async def toDict(self) -> Dict[str, Any]: + async def toDict(self, ignore_multi_config: bool = False) -> Dict[str, Any]: """将配置项转换为字典""" data = {} @@ -267,7 +268,7 @@ class ConfigBase: if item.name: data[item.group][item.name] = item.value - elif isinstance(item, MultipleConfig): + elif not ignore_multi_config and isinstance(item, MultipleConfig): if not data.get("SubConfigsInfo"): data["SubConfigsInfo"] = {} @@ -326,7 +327,12 @@ class ConfigBase: self.file.parent.mkdir(parents=True, exist_ok=True) with self.file.open("w", encoding="utf-8") as f: - json.dump(await self.toDict(), f, ensure_ascii=False, indent=4) + json.dump( + await self.toDict(not self.if_save_multi_config), + f, + ensure_ascii=False, + indent=4, + ) class MultipleConfig: diff --git a/app/models/schema.py b/app/models/schema.py index 4dae916..19c7fa8 100644 --- a/app/models/schema.py +++ b/app/models/schema.py @@ -54,3 +54,11 @@ class ScriptUpdateIn(BaseModel): class ScriptDeleteIn(BaseModel): scriptId: str = Field(..., description="脚本ID") + + +class SettingGetOut(BaseOut): + data: Dict[str, Dict[str, Any]] = Field(..., description="全局设置数据") + + +class SettingUpdateIn(BaseModel): + data: Dict[str, Dict[str, Any]] = Field(..., description="全局设置更新数据")