为网页控制台添加修改设定的功能

This commit is contained in:
Wuyi无疑 2023-03-24 23:41:41 +08:00
parent 4d0014a092
commit 9983979d7c
5 changed files with 185 additions and 16 deletions

View File

@ -8,7 +8,7 @@ from msgspec import json as msgjson
from starlette.requests import Request from starlette.requests import Request
from fastapi import FastAPI, WebSocket, WebSocketDisconnect 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.sv import SL # noqa: E402
from gsuid_core.gss import gss # noqa: E402 from gsuid_core.gss import gss # noqa: E402
from gsuid_core.logger import logger # noqa: E402 from gsuid_core.logger import logger # noqa: E402
@ -60,8 +60,12 @@ async def shutdown_event():
if __name__ == "__main__": if __name__ == "__main__":
try: try:
from gsuid_core.webconsole.mount_app import site 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') @site.auth.requires('admin')
async def _set_SV(request: Request, data: Dict, name: str): async def _set_SV(request: Request, data: Dict, name: str):
if name in SL.lst: if name in SL.lst:
@ -70,6 +74,19 @@ if __name__ == "__main__":
data['black_list'] = data['black_list'].split(';') data['black_list'] = data['black_list'].split(';')
sv.set(**data) 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) site.mount_app(app)
except ImportError: except ImportError:
logger.warning('未加载GenshinUID...网页控制台启动失败...') logger.warning('未加载GenshinUID...网页控制台启动失败...')

View File

@ -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',
},
],
}

View File

@ -1,31 +1,76 @@
from typing import Dict
from gsuid_core.plugins.GenshinUID.GenshinUID.genshinuid_config import ( from gsuid_core.plugins.GenshinUID.GenshinUID.genshinuid_config import (
gs_config, 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 ( from gsuid_core.plugins.GenshinUID.GenshinUID.genshinuid_config.models import (
GsStrConfig, GsStrConfig,
GsBoolConfig,
GsListStrConfig,
) )
gsconfig = gs_config.gsconfig gsconfig = gs_config.gsconfig
def get_str_panel(name: str, value: str) -> Dict: def get_config_page():
return {}
def get_all_config():
page = { page = {
'type': 'page', 'type': 'page',
'title': '配置管理', 'title': '配置管理',
'body': [], 'body': [],
'id': 'u:a9be7e0dc626', '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 = [] body = []
solo_body = []
for config in gsconfig: for config in gsconfig:
gsc = gsconfig[config] gsc = gsconfig[config]
if isinstance(gsc, GsStrConfig): if isinstance(gsc, GsStrConfig):
body.append(get_str_panel(config, gsc.data)) solo_body.append(get_text_panel(gsc.title, config, gsc.data))
elif isinstance(gsc, GsBoolConfig):
page['body'] = body 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 return page

View File

@ -11,7 +11,7 @@ def get_sv_panel(
area: Literal['GROUP', 'DIRECT', 'ALL'] = 'ALL', area: Literal['GROUP', 'DIRECT', 'ALL'] = 'ALL',
black_list: List = [], black_list: List = [],
): ):
api = f'/setSV/{name}' api = f'/genshinuid/setSV/{name}'
card = { card = {
"type": "service", "type": "service",
"body": { "body": {

View File

@ -7,7 +7,6 @@ from pydantic import BaseModel
from fastapi_user_auth.auth import Auth from fastapi_user_auth.auth import Auth
from fastapi_amis_admin import amis, admin from fastapi_amis_admin import amis, admin
from fastapi_user_auth.app import UserAuthApp from fastapi_user_auth.app import UserAuthApp
from sqlalchemy_database import AsyncDatabase
from fastapi_amis_admin.crud import BaseApiOut from fastapi_amis_admin.crud import BaseApiOut
from sqlalchemy.ext.asyncio import AsyncEngine from sqlalchemy.ext.asyncio import AsyncEngine
from fastapi_user_auth.site import AuthAdminSite 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.models import WebUser
from gsuid_core.webconsole.html import gsuid_webconsole_help 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_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.utils.database import db_url
from gsuid_core.plugins.GenshinUID.GenshinUID.version import GenshinUID_version from gsuid_core.plugins.GenshinUID.GenshinUID.version import GenshinUID_version
from gsuid_core.plugins.GenshinUID.GenshinUID.genshinuid_user.add_ck import ( from gsuid_core.plugins.GenshinUID.GenshinUID.genshinuid_user.add_ck import (
@ -373,9 +373,9 @@ class MyHomeAdmin(admin.HomeAdmin):
@site.register_admin @site.register_admin
class SVManagePage(admin.PageAdmin): class SVManagePage(admin.PageAdmin):
page_schema = PageSchema( page_schema = PageSchema(
label=('修改配置'), label=('功能配置'),
icon='fa fa-sliders', icon='fa fa-sliders',
url='/SVmanage', url='/SvManage',
isDefaultPage=True, isDefaultPage=True,
sort=100, 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) site.unregister_admin(admin.HomeAdmin, APIDocsApp)