855 协议版本-调整完毕内容
This commit is contained in:
@@ -2,23 +2,22 @@
|
||||
"""
|
||||
统计看板服务器 - 使用Flask蓝图重构版
|
||||
"""
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
from datetime import datetime
|
||||
|
||||
from gewechat_client import GewechatClient
|
||||
from loguru import logger
|
||||
|
||||
from db.message_storage import MessageStorageDB
|
||||
from db.stats_db import StatsDBOperator
|
||||
from flask import Flask, send_from_directory
|
||||
import toml
|
||||
|
||||
from wechat_ipad import WechatAPIClient
|
||||
|
||||
# 添加项目根目录到系统路径,确保可以导入项目模块
|
||||
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))
|
||||
|
||||
|
||||
class DashboardServer:
|
||||
"""统计看板服务器"""
|
||||
|
||||
@@ -34,10 +33,8 @@ class DashboardServer:
|
||||
self.username = username or self.config.get("auth", {}).get("username", "admin")
|
||||
self.password = password or self.config.get("auth", {}).get("password", "admin123")
|
||||
|
||||
self.logger = logging.getLogger("DashboardServer")
|
||||
self.logger.info(f"Dashboard配置加载完成: 服务器将运行在 {self.host}:{self.port}")
|
||||
logging.getLogger("werkzeug").setLevel(logging.WARNING)
|
||||
|
||||
self.LOG = logger
|
||||
self.LOG.info(f"Dashboard配置加载完成: 服务器将运行在 {self.host}:{self.port}")
|
||||
# 如果提供了robot实例,则使用其对象
|
||||
if robot_instance:
|
||||
self.db_manager = robot_instance.db_manager
|
||||
@@ -47,11 +44,10 @@ class DashboardServer:
|
||||
self.contact_manager = robot_instance.contact_manager
|
||||
self.plugin_manager = robot_instance.plugin_manager
|
||||
self.plugin_registry = robot_instance.plugin_registry
|
||||
self.client:GewechatClient= robot_instance.client
|
||||
self.app_id = robot_instance.app_id
|
||||
self.logger.info("使用Robot实例的对象进行初始化")
|
||||
self.client: WechatAPIClient = robot_instance.ipad_bot
|
||||
self.LOG.info("使用Robot实例的对象进行初始化")
|
||||
else:
|
||||
self.logger.error("未提供Robot实例,Dashboard无法正常工作")
|
||||
self.LOG.error("未提供Robot实例,Dashboard无法正常工作")
|
||||
raise ValueError("必须提供Robot实例")
|
||||
|
||||
self.app = self._create_app()
|
||||
@@ -75,7 +71,7 @@ class DashboardServer:
|
||||
toml.dump(default_config, f)
|
||||
return default_config
|
||||
except Exception as e:
|
||||
self.logger.error(f"加载Dashboard配置文件失败: {e}")
|
||||
self.LOG.error(f"加载Dashboard配置文件失败: {e}")
|
||||
# 返回默认配置
|
||||
return {
|
||||
"server": {"host": "0.0.0.0", "port": 8888},
|
||||
@@ -90,10 +86,10 @@ class DashboardServer:
|
||||
app.secret_key = "stats_dashboard_secret_key"
|
||||
# 禁用模板缓存,使修改HTML文件后立即生效
|
||||
app.config['TEMPLATES_AUTO_RELOAD'] = True
|
||||
|
||||
|
||||
# 将dashboard_server实例设置为app的属性
|
||||
app.dashboard_server = self
|
||||
|
||||
|
||||
# 配置静态文件访问
|
||||
static_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static')
|
||||
|
||||
@@ -120,9 +116,9 @@ class DashboardServer:
|
||||
|
||||
# 注册蓝图
|
||||
self._register_blueprints(app)
|
||||
|
||||
|
||||
return app
|
||||
|
||||
|
||||
def _register_blueprints(self, app):
|
||||
"""注册所有蓝图"""
|
||||
# 在函数内部导入蓝图,避免循环导入
|
||||
@@ -146,52 +142,52 @@ class DashboardServer:
|
||||
app.register_blueprint(stats_bp)
|
||||
app.register_blueprint(system_bp)
|
||||
app.register_blueprint(plugin_routes)
|
||||
|
||||
self.logger.info("所有蓝图已注册")
|
||||
|
||||
self.LOG.info("所有蓝图已注册")
|
||||
|
||||
def run(self):
|
||||
"""运行服务器"""
|
||||
from werkzeug.serving import make_server
|
||||
|
||||
self.logger.info(f"启动服务器: {self.host}:{self.port}")
|
||||
self.LOG.info(f"启动服务器: {self.host}:{self.port}")
|
||||
try:
|
||||
# 使用线程安全的方式运行服务器
|
||||
self._server = make_server(self.host, self.port, self.app)
|
||||
self._server.serve_forever()
|
||||
except Exception as e:
|
||||
self.logger.error(f"服务器运行失败: {e}")
|
||||
self.LOG.error(f"服务器运行失败: {e}")
|
||||
self._stop_event.set()
|
||||
|
||||
def stop(self):
|
||||
"""停止服务器"""
|
||||
self.logger.info("正在停止服务器...")
|
||||
self.LOG.info("正在停止服务器...")
|
||||
self._stop_event.set()
|
||||
|
||||
# 使用werkzeug服务器的关闭方法
|
||||
if self._server:
|
||||
self._server.shutdown()
|
||||
|
||||
self.logger.info("服务器已停止")
|
||||
self.LOG.info("服务器已停止")
|
||||
|
||||
def get_current_user_info(self):
|
||||
"""获取当前登录的微信用户信息"""
|
||||
try:
|
||||
if not self.client:
|
||||
self.logger.error("client实例不可用,无法获取当前用户信息")
|
||||
self.LOG.error("client实例不可用,无法获取当前用户信息")
|
||||
return {"success": False, "message": "实例不可用"}
|
||||
|
||||
|
||||
# 获取当前登录的微信ID
|
||||
resp = self.client.get_profile(self.app_id)
|
||||
|
||||
resp = self.client.get_profile(self.client.wxid)
|
||||
self.LOG.info(f"get_current_user_info:{resp}")
|
||||
if not resp or resp.get("ret") != 200:
|
||||
self.logger.error(f"获取用户信息失败: {resp}")
|
||||
self.LOG.error(f"获取用户信息失败: {resp}")
|
||||
return {"success": False, "message": "获取用户信息失败"}
|
||||
|
||||
|
||||
# 从新的resp格式中获取用户信息
|
||||
user_data = resp.get("data", {})
|
||||
if not user_data:
|
||||
return {"success": False, "message": "未获取到用户数据"}
|
||||
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"data": {
|
||||
@@ -203,5 +199,5 @@ class DashboardServer:
|
||||
}
|
||||
}
|
||||
except Exception as e:
|
||||
self.logger.error(f"获取当前用户信息失败: {e}")
|
||||
self.LOG.error(f"获取当前用户信息失败: {e}")
|
||||
return {"success": False, "message": f"获取用户信息出错: {str(e)}"}
|
||||
|
||||
Reference in New Issue
Block a user