From 9983979d7c78a8c53b6945da28c0977a9a283dd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wuyi=E6=97=A0=E7=96=91?= <444835641@qq.com> Date: Fri, 24 Mar 2023 23:41:41 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E4=B8=BA=E7=BD=91=E9=A1=B5?= =?UTF-8?q?=E6=8E=A7=E5=88=B6=E5=8F=B0=E6=B7=BB=E5=8A=A0`=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E8=AE=BE=E5=AE=9A`=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gsuid_core/core.py | 21 ++++- gsuid_core/webconsole/create_base_panel.py | 83 ++++++++++++++++++++ gsuid_core/webconsole/create_config_panel.py | 65 ++++++++++++--- gsuid_core/webconsole/create_sv_panel.py | 2 +- gsuid_core/webconsole/mount_app.py | 30 ++++++- 5 files changed, 185 insertions(+), 16 deletions(-) create mode 100644 gsuid_core/webconsole/create_base_panel.py diff --git a/gsuid_core/core.py b/gsuid_core/core.py index 0b1161a..50f2349 100644 --- a/gsuid_core/core.py +++ b/gsuid_core/core.py @@ -8,7 +8,7 @@ from msgspec import json as msgjson from starlette.requests import Request from fastapi import FastAPI, WebSocket, WebSocketDisconnect -sys.path.append(str(Path(__file__).parents[1])) +sys.path.append(str(Path(__file__).resolve().parents[1])) from gsuid_core.sv import SL # noqa: E402 from gsuid_core.gss import gss # noqa: E402 from gsuid_core.logger import logger # noqa: E402 @@ -60,8 +60,12 @@ async def shutdown_event(): if __name__ == "__main__": try: from gsuid_core.webconsole.mount_app import site + from gsuid_core.webconsole.create_config_panel import ( + GsListStrConfig, + gsconfig, + ) - @app.post('/setSV/{name}') + @app.post('/genshinuid/setSV/{name}') @site.auth.requires('admin') async def _set_SV(request: Request, data: Dict, name: str): if name in SL.lst: @@ -70,6 +74,19 @@ if __name__ == "__main__": data['black_list'] = data['black_list'].split(';') sv.set(**data) + @app.post('/genshinuid/setGsConfig') + @site.auth.requires('admin') + async def _set_Config(request: Request, data: Dict): + for name in data: + if name == 'params': + continue + config = gsconfig[name] + if isinstance(config, GsListStrConfig): + value = data[name].split(':') + else: + value = data[name] + gsconfig.set_config(name, value) + site.mount_app(app) except ImportError: logger.warning('未加载GenshinUID...网页控制台启动失败...') diff --git a/gsuid_core/webconsole/create_base_panel.py b/gsuid_core/webconsole/create_base_panel.py new file mode 100644 index 0000000..bec95ee --- /dev/null +++ b/gsuid_core/webconsole/create_base_panel.py @@ -0,0 +1,83 @@ +from typing import Dict, List + + +def get_switch_panel(label: str, name: str, value: bool): + return { + 'type': 'switch', + 'label': label, + 'option': '', + 'name': name, + 'falseValue': False, + 'trueValue': True, + 'id': 'u:d0bc78558aa9', + 'value': value, + } + + +def get_text_panel(label: str, name: str, value: str): + return { + 'type': 'input-text', + 'label': label, + 'name': name, + 'id': 'u:2de3dcaddcc1', + 'value': value, + } + + +def get_container_panel( + content: List[Dict], +): + return { + 'type': 'flex', + 'className': 'p-1', + 'items': [ + { + 'type': 'container', + 'body': [content[0]] if len(content) >= 1 else [], + 'size': 'xs', + 'style': { + 'position': 'static', + 'display': 'block', + 'flex': '0 0 200px', + 'flexGrow': 1, + 'flexBasis': 'auto', + }, + 'wrapperBody': False, + 'isFixedHeight': False, + 'isFixedWidth': False, + 'id': 'u:7433706a00d0', + }, + { + 'type': 'container', + 'body': [content[1]] if len(content) >= 2 else [], + 'size': 'xs', + 'style': { + 'position': 'static', + 'display': 'block', + 'flex': '0 0 200px', + 'flexGrow': 1, + 'flexBasis': 'auto', + }, + 'wrapperBody': False, + 'isFixedHeight': False, + 'isFixedWidth': False, + 'id': 'u:5baaf2f4b281', + }, + { + 'type': 'container', + 'body': [content[2]] if len(content) >= 3 else [], + 'size': 'xs', + 'style': { + 'position': 'static', + 'display': 'block', + 'flex': '0 0 200px', + 'flexGrow': 1, + 'flexBasis': 'auto', + }, + 'wrapperBody': False, + 'isFixedHeight': False, + 'isFixedWidth': False, + 'id': 'u:0f837da20702', + }, + ], + } diff --git a/gsuid_core/webconsole/create_config_panel.py b/gsuid_core/webconsole/create_config_panel.py index 3884978..4696bfd 100644 --- a/gsuid_core/webconsole/create_config_panel.py +++ b/gsuid_core/webconsole/create_config_panel.py @@ -1,31 +1,76 @@ -from typing import Dict - from gsuid_core.plugins.GenshinUID.GenshinUID.genshinuid_config import ( gs_config, ) +from gsuid_core.webconsole.create_base_panel import ( + get_text_panel, + get_switch_panel, + get_container_panel, +) from gsuid_core.plugins.GenshinUID.GenshinUID.genshinuid_config.models import ( GsStrConfig, + GsBoolConfig, + GsListStrConfig, ) gsconfig = gs_config.gsconfig -def get_str_panel(name: str, value: str) -> Dict: - return {} - - -def get_all_config(): +def get_config_page(): page = { 'type': 'page', 'title': '配置管理', 'body': [], 'id': 'u:a9be7e0dc626', } + card = { + 'type': 'card', + 'header': {'title': '', 'subTitle': ''}, + 'body': [], + 'actions': [ + { + 'type': 'button', + 'label': '确认修改', + 'id': 'u:5784cfaa5c0a', + 'actionType': 'ajax', + 'api': '/genshinuid/setGsConfig', + 'onEvent': { + 'click': { + 'weight': 0, + 'actions': [ + { + 'args': { + 'msgType': 'success', + 'position': 'top-center', + 'closeButton': True, + 'showIcon': True, + 'msg': '成功设置!', + 'timeout': 100, + }, + 'actionType': 'toast', + } + ], + } + }, + } + ], + 'id': 'u:69b06813bfbe', + } body = [] + solo_body = [] for config in gsconfig: gsc = gsconfig[config] if isinstance(gsc, GsStrConfig): - body.append(get_str_panel(config, gsc.data)) - - page['body'] = body + solo_body.append(get_text_panel(gsc.title, config, gsc.data)) + elif isinstance(gsc, GsBoolConfig): + solo_body.append(get_switch_panel(gsc.title, config, gsc.data)) + elif isinstance(gsc, GsListStrConfig): + solo_body.append( + get_text_panel(gsc.title, config, ':'.join(gsc.data)) + ) + if len(solo_body) == 3: + body.append(get_container_panel(solo_body)) + solo_body = [] + body.append(get_container_panel(solo_body)) + card['body'] = body + page['body'].append(card) return page diff --git a/gsuid_core/webconsole/create_sv_panel.py b/gsuid_core/webconsole/create_sv_panel.py index b1cfa11..7862cbf 100644 --- a/gsuid_core/webconsole/create_sv_panel.py +++ b/gsuid_core/webconsole/create_sv_panel.py @@ -11,7 +11,7 @@ def get_sv_panel( area: Literal['GROUP', 'DIRECT', 'ALL'] = 'ALL', black_list: List = [], ): - api = f'/setSV/{name}' + api = f'/genshinuid/setSV/{name}' card = { "type": "service", "body": { diff --git a/gsuid_core/webconsole/mount_app.py b/gsuid_core/webconsole/mount_app.py index 0fc71fa..b7d54c8 100644 --- a/gsuid_core/webconsole/mount_app.py +++ b/gsuid_core/webconsole/mount_app.py @@ -7,7 +7,6 @@ from pydantic import BaseModel from fastapi_user_auth.auth import Auth from fastapi_amis_admin import amis, admin from fastapi_user_auth.app import UserAuthApp -from sqlalchemy_database import AsyncDatabase from fastapi_amis_admin.crud import BaseApiOut from sqlalchemy.ext.asyncio import AsyncEngine from fastapi_user_auth.site import AuthAdminSite @@ -40,6 +39,7 @@ from fastapi_amis_admin.amis.components import ( from gsuid_core.webconsole.models import WebUser from gsuid_core.webconsole.html import gsuid_webconsole_help from gsuid_core.webconsole.create_sv_panel import get_sv_page +from gsuid_core.webconsole.create_config_panel import get_config_page from gsuid_core.plugins.GenshinUID.GenshinUID.utils.database import db_url from gsuid_core.plugins.GenshinUID.GenshinUID.version import GenshinUID_version from gsuid_core.plugins.GenshinUID.GenshinUID.genshinuid_user.add_ck import ( @@ -373,9 +373,9 @@ class MyHomeAdmin(admin.HomeAdmin): @site.register_admin class SVManagePage(admin.PageAdmin): page_schema = PageSchema( - label=('修改配置'), + label=('功能配置'), icon='fa fa-sliders', - url='/SVmanage', + url='/SvManage', isDefaultPage=True, sort=100, ) @@ -394,5 +394,29 @@ class SVManagePage(admin.PageAdmin): ) +@site.register_admin +class ConfigManagePage(admin.PageAdmin): + page_schema = PageSchema( + label=('修改设定'), + icon='fa fa-sliders', + url='/ConfigManage', + isDefaultPage=True, + sort=100, + ) + page = Page.parse_obj(get_config_page()) + + async def has_page_permission( + self, + request: Request, + obj: Optional[admin.ModelAdmin] = None, + action: Optional[str] = None, + ) -> bool: + return await super().has_page_permission( + request + ) and await request.auth.requires(roles='admin', response=False)( + request + ) + + # 取消注册默认管理类 site.unregister_admin(admin.HomeAdmin, APIDocsApp)