From dac184862935099bcb575f1b0db7910279d6e7dd Mon Sep 17 00:00:00 2001 From: liuwei Date: Thu, 27 Mar 2025 11:39:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E7=9C=8B=E6=9D=BF=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E7=8B=AC=E7=AB=8B=EF=BC=8C=E6=96=B9=E4=BE=BF=E7=8B=AC=E7=AB=8B?= =?UTF-8?q?=E7=BB=B4=E6=8A=A4=E5=8A=9F=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin/dashboard/server.py | 85 ++++++++- admin/dashboard/templates/base.html | 286 ++++++++++++++++------------ 2 files changed, 244 insertions(+), 127 deletions(-) diff --git a/admin/dashboard/server.py b/admin/dashboard/server.py index 0928ada..9704c9e 100644 --- a/admin/dashboard/server.py +++ b/admin/dashboard/server.py @@ -3,6 +3,7 @@ import os import sys import threading import time +import yaml from datetime import datetime # 添加项目根目录到系统路径,确保可以导入项目模块 @@ -27,17 +28,91 @@ class DashboardServer: self.username = username self.password = password self.logger = logging.getLogger("DashboardServer") - - # 使用单例模式获取数据库连接 - self.db_manager = DBConnectionManager.get_instance() - self.stats_db = StatsDBOperator(self.db_manager) - self.message_storage = MessageStorageDB(self.db_manager) + + # 初始化数据库连接 + self._init_database() + # 获取联系人管理器实例 self.contact_manager = ContactManager.get_instance() self.app = self._create_app() self._stop_event = threading.Event() self._server = None # 存储服务器实例 + def _load_config(self): + """从配置文件加载配置""" + try: + # 获取项目根目录的config.yaml文件路径 + config_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'config.yaml')) + self.logger.info(f"正在加载配置文件: {config_path}") + + with open(config_path, 'r', encoding='utf-8') as f: + config = yaml.safe_load(f) + + return config + except Exception as e: + self.logger.error(f"加载配置文件失败: {e}") + # 返回默认配置 + return { + "db_config": { + "pool_name": "wechat_boot_pool", + "pool_size": 10, + "host": "localhost", + "user": "root", + "password": "password", + "database": "message_archive", + "charset": "utf8mb4", + "use_unicode": True, + "get_warnings": True, + "pool_reset_session": True + }, + "redis_config": { + "host": "localhost", + "port": 6379, + "db": 0, + "decode_responses": True + } + } + + def _init_database(self): + """初始化数据库连接""" + try: + # 加载配置 + config = self._load_config() + + # 获取数据库配置 + db_config = config.get("db_config", {}) + redis_config = config.get("redis_config", {}) + + self.logger.info("正在初始化数据库连接...") + self.logger.info(f"数据库主机: {db_config.get('host')}, 数据库: {db_config.get('database')}") + + # 初始化数据库连接管理器 + self.db_manager = DBConnectionManager.get_instance( + pool_name=db_config.get("pool_name"), + pool_size=db_config.get("pool_size"), + host=db_config.get("host"), + user=db_config.get("user"), + password=db_config.get("password"), + database=db_config.get("database"), + charset=db_config.get("charset"), + use_unicode=db_config.get("use_unicode"), + get_warnings=db_config.get("get_warnings"), + pool_reset_session=db_config.get("pool_reset_session"), + redis_host=redis_config.get("host"), + redis_port=redis_config.get("port"), + redis_db=redis_config.get("db"), + redis_decode_responses=redis_config.get("decode_responses") + ) + + # 初始化数据库操作类 + self.stats_db = StatsDBOperator(self.db_manager) + self.message_storage = MessageStorageDB(self.db_manager) + + self.logger.info("数据库连接初始化成功") + except Exception as e: + self.logger.error(f"初始化数据库连接失败: {e}") + raise + def _create_app(self) -> Flask: """创建Flask应用""" app = Flask(__name__) diff --git a/admin/dashboard/templates/base.html b/admin/dashboard/templates/base.html index e1f37c5..4d79fec 100644 --- a/admin/dashboard/templates/base.html +++ b/admin/dashboard/templates/base.html @@ -9,170 +9,212 @@ - - + + + + + + + + + {% block head %}{% endblock %} -
- - +
+
+

机器人管理后台

+ +
+ + + 退出登录 + +
+
+ +
+ + + + +
+ + + + + + + + + + + + + + 刷新数据 + + + + + + + + {% block content %}{% endblock %} +
+
- - - - - - - - - - - + + {% block scripts %}{% endblock %} \ No newline at end of file