mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-02 10:10:01 +08:00
重构webui适配 (#1801)
* ♻️ 使用Uninfo重构PlatformUtils基础方法 * 🩹 优化插件加载与模块格式转换逻辑 * 🚑 修复商店道具无法使用 * 🚑 修复道具无法正常使用 * 🔧 增加Bot状态管理及模块禁用功能 * 🎨 优化Web UI代码结构,修改target方法 * 🚨 auto fix by pre-commit hooks * 🎨 添加菜单API及优化异常处理 * 🐛 优化菜单API及模型结构,修复WebUi插件列表Api * 📝 更新仓库readme * 🚨 add mdlint file * 📝 Add help chapter. * 🐛 修复优化AuthChecker逻辑 * 🐛 优化数据库API,移除冗余导入及修正SQL_DICT引用 --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: BalconyJH <balconyjh@gmail.com>
This commit is contained in:
co-authored by
pre-commit-ci[bot]
BalconyJH
parent
ebf05fd884
commit
35014e4048
@@ -0,0 +1,26 @@
|
||||
from fastapi import APIRouter
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from zhenxun.services.log import logger
|
||||
|
||||
from ...base_model import Result
|
||||
from ...utils import authentication
|
||||
from .data_source import menu_manage
|
||||
from .model import MenuData
|
||||
|
||||
router = APIRouter(prefix="/menu")
|
||||
|
||||
|
||||
@router.get(
|
||||
"/get_menus",
|
||||
dependencies=[authentication()],
|
||||
response_model=Result[list[MenuData]],
|
||||
response_class=JSONResponse,
|
||||
deprecated="获取菜单列表", # type: ignore
|
||||
)
|
||||
async def _() -> Result[list[MenuData]]:
|
||||
try:
|
||||
return Result.ok(menu_manage.get_menus(), "拿到菜单了哦!")
|
||||
except Exception as e:
|
||||
logger.error(f"{router.prefix}/get_menus 调用错误", "WebUi", e=e)
|
||||
return Result.fail(f"发生了一点错误捏 {type(e)}: {e}")
|
||||
@@ -0,0 +1,64 @@
|
||||
import ujson as json
|
||||
|
||||
from zhenxun.configs.path_config import DATA_PATH
|
||||
from zhenxun.services.log import logger
|
||||
|
||||
from .model import MenuData, MenuItem
|
||||
|
||||
|
||||
class MenuManage:
|
||||
def __init__(self) -> None:
|
||||
self.file = DATA_PATH / "web_ui" / "menu.json"
|
||||
self.menu = []
|
||||
if self.file.exists():
|
||||
try:
|
||||
self.menu = json.load(self.file.open(encoding="utf8"))
|
||||
except Exception as e:
|
||||
logger.warning("菜单文件损坏,已重新生成...", "WebUi", e=e)
|
||||
if not self.menu:
|
||||
self.menu = [
|
||||
MenuItem(
|
||||
name="仪表盘",
|
||||
module="dashboard",
|
||||
router="/dashboard",
|
||||
icon="dashboard",
|
||||
default=True,
|
||||
),
|
||||
MenuItem(
|
||||
name="真寻控制台",
|
||||
module="command",
|
||||
router="/command",
|
||||
icon="command",
|
||||
),
|
||||
MenuItem(
|
||||
name="插件列表", module="plugin", router="/plugin", icon="plugin"
|
||||
),
|
||||
MenuItem(
|
||||
name="插件商店", module="store", router="/store", icon="store"
|
||||
),
|
||||
MenuItem(
|
||||
name="好友/群组", module="manage", router="/manage", icon="user"
|
||||
),
|
||||
MenuItem(
|
||||
name="数据库管理",
|
||||
module="database",
|
||||
router="/database",
|
||||
icon="database",
|
||||
),
|
||||
MenuItem(
|
||||
name="系统信息", module="system", router="/system", icon="system"
|
||||
),
|
||||
]
|
||||
self.save()
|
||||
|
||||
def get_menus(self):
|
||||
return MenuData(menus=self.menu)
|
||||
|
||||
def save(self):
|
||||
self.file.parent.mkdir(parents=True, exist_ok=True)
|
||||
temp = [menu.dict() for menu in self.menu]
|
||||
with self.file.open("w", encoding="utf8") as f:
|
||||
json.dump(temp, f, ensure_ascii=False, indent=4)
|
||||
|
||||
|
||||
menu_manage = MenuManage()
|
||||
@@ -0,0 +1,21 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class MenuItem(BaseModel):
|
||||
module: str
|
||||
"""模块名称"""
|
||||
name: str
|
||||
"""菜单名称"""
|
||||
router: str
|
||||
"""路由"""
|
||||
icon: str
|
||||
"""图标"""
|
||||
default: bool = False
|
||||
"""默认选中"""
|
||||
|
||||
|
||||
class MenuData(BaseModel):
|
||||
bot_type: str = "zhenxun"
|
||||
"""bot类型"""
|
||||
menus: list[MenuItem]
|
||||
"""菜单列表"""
|
||||
Reference in New Issue
Block a user