将看板功能独立,方便独立维护功能。
This commit is contained in:
@@ -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__)
|
||||
|
||||
@@ -9,170 +9,212 @@
|
||||
<link rel="shortcut icon" href="/static/favicon.ico" type="image/x-icon">
|
||||
<!-- Element UI CSS -->
|
||||
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
|
||||
<!-- 自定义样式 -->
|
||||
<link rel="stylesheet" href="/static/css/style.css">
|
||||
<!-- 图表库 -->
|
||||
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
||||
<!-- Vue.js -->
|
||||
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
|
||||
<!-- Element UI JS -->
|
||||
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
|
||||
<!-- Axios -->
|
||||
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
|
||||
<style>
|
||||
body {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;
|
||||
}
|
||||
.app-container {
|
||||
height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
.header {
|
||||
background-color: #409EFF;
|
||||
color: white;
|
||||
padding: 0 20px;
|
||||
height: 60px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
}
|
||||
.main-container {
|
||||
display: flex;
|
||||
flex: 1;
|
||||
overflow: hidden;
|
||||
}
|
||||
.sidebar {
|
||||
width: 200px;
|
||||
background-color: #545c64;
|
||||
height: 100%;
|
||||
overflow-y: auto;
|
||||
}
|
||||
.content {
|
||||
flex: 1;
|
||||
padding: 20px;
|
||||
overflow-y: auto;
|
||||
background-color: #f0f2f5;
|
||||
}
|
||||
.el-menu {
|
||||
border-right: none;
|
||||
}
|
||||
.chart-container {
|
||||
background-color: white;
|
||||
padding: 20px;
|
||||
border-radius: 4px;
|
||||
box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
.stats-card {
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
/* 添加退出登录按钮样式 */
|
||||
.logout-btn {
|
||||
color: white;
|
||||
cursor: pointer;
|
||||
margin-left: 15px;
|
||||
}
|
||||
.header-right {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
.user-info {
|
||||
margin-right: 10px;
|
||||
}
|
||||
</style>
|
||||
{% block head %}{% endblock %}
|
||||
</head>
|
||||
<body>
|
||||
<div id="app">
|
||||
<el-container>
|
||||
<el-aside width="200px">
|
||||
<div id="app" class="app-container">
|
||||
<header class="header">
|
||||
<h2>机器人管理后台</h2>
|
||||
<!-- 添加退出登录按钮 -->
|
||||
<div class="header-right">
|
||||
<span class="user-info">管理员</span>
|
||||
<el-button type="text" class="logout-btn" @click="logout">
|
||||
<i class="el-icon-switch-button"></i> 退出登录
|
||||
</el-button>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<div class="main-container">
|
||||
<!-- 左侧菜单 -->
|
||||
<div class="sidebar">
|
||||
<el-menu
|
||||
default-active="{{ request.path }}"
|
||||
:default-active="currentView"
|
||||
class="el-menu-vertical-demo"
|
||||
background-color="#545c64"
|
||||
text-color="#fff"
|
||||
active-text-color="#ffd04b"
|
||||
router>
|
||||
<div class="logo">WeChatRobot</div>
|
||||
<el-menu-item index="/">
|
||||
@select="handleSelect">
|
||||
<el-menu-item index="1">
|
||||
<i class="el-icon-s-home"></i>
|
||||
<span slot="title">首页概览</span>
|
||||
</el-menu-item>
|
||||
<el-menu-item index="/plugins">
|
||||
<el-menu-item index="2">
|
||||
<i class="el-icon-s-grid"></i>
|
||||
<span slot="title">插件统计</span>
|
||||
</el-menu-item>
|
||||
<el-menu-item index="/users">
|
||||
<el-menu-item index="3">
|
||||
<i class="el-icon-user"></i>
|
||||
<span slot="title">用户统计</span>
|
||||
</el-menu-item>
|
||||
<el-menu-item index="/groups">
|
||||
<el-menu-item index="4">
|
||||
<i class="el-icon-s-cooperation"></i>
|
||||
<span slot="title">群组统计</span>
|
||||
</el-menu-item>
|
||||
<el-menu-item index="/errors">
|
||||
<el-menu-item index="5">
|
||||
<i class="el-icon-warning"></i>
|
||||
<span slot="title">错误日志</span>
|
||||
</el-menu-item>
|
||||
<el-menu-item index="/robot_management">
|
||||
|
||||
<!-- 在左侧菜单中添加群机器人管理选项 -->
|
||||
<!-- 找到el-menu标签内的菜单项列表,添加以下内容 -->
|
||||
<el-menu-item index="6">
|
||||
<i class="el-icon-setting"></i>
|
||||
<span slot="title">群机器人管理</span>
|
||||
</el-menu-item>
|
||||
</el-menu>
|
||||
</el-aside>
|
||||
<el-container>
|
||||
<el-header>
|
||||
<div class="header-title">{% block header_title %}机器人管理后台{% endblock %}</div>
|
||||
<div class="header-actions">
|
||||
<el-dropdown @command="handleCommand">
|
||||
<span class="el-dropdown-link">
|
||||
管理员<i class="el-icon-arrow-down el-icon--right"></i>
|
||||
</span>
|
||||
<el-dropdown-menu slot="dropdown">
|
||||
<el-dropdown-item command="logout">退出登录</el-dropdown-item>
|
||||
</el-dropdown-menu>
|
||||
</el-dropdown>
|
||||
</div>
|
||||
</el-header>
|
||||
<el-main>
|
||||
<!-- 时间范围选择器 -->
|
||||
<el-row :gutter="20" style="margin-bottom: 20px;">
|
||||
<el-col :span="24">
|
||||
<el-card shadow="hover">
|
||||
<el-form :inline="true" size="small">
|
||||
<el-form-item label="统计时间范围">
|
||||
<el-select v-model="timeRange" @change="loadData">
|
||||
<el-option label="最近7天" value="7"></el-option>
|
||||
<el-option label="最近30天" value="30"></el-option>
|
||||
<el-option label="最近90天" value="90"></el-option>
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
<el-form-item>
|
||||
<el-button type="primary" @click="loadData">刷新数据</el-button>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
</el-card>
|
||||
</el-col>
|
||||
</el-row>
|
||||
|
||||
{% block content %}{% endblock %}
|
||||
</el-main>
|
||||
<el-footer>
|
||||
<div class="footer-content">
|
||||
WeChatRobot © 2024 - 由 Trae AI 提供技术支持
|
||||
</div>
|
||||
</el-footer>
|
||||
</el-container>
|
||||
</el-container>
|
||||
</div>
|
||||
|
||||
<!-- 右侧内容区 -->
|
||||
<div class="content">
|
||||
<!-- 时间范围选择器 -->
|
||||
<el-row :gutter="20" style="margin-bottom: 20px;">
|
||||
<el-col :span="24">
|
||||
<el-card shadow="hover">
|
||||
<el-form :inline="true" size="small">
|
||||
<el-form-item label="统计时间范围">
|
||||
<el-select v-model="timeRange" @change="loadData">
|
||||
<el-option label="最近7天" value="7"></el-option>
|
||||
<el-option label="最近30天" value="30"></el-option>
|
||||
<el-option label="最近90天" value="90"></el-option>
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
<el-form-item>
|
||||
<el-button type="primary" @click="loadData">刷新数据</el-button>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
</el-card>
|
||||
</el-col>
|
||||
</el-row>
|
||||
|
||||
<!-- 内容区域 -->
|
||||
{% block content %}{% endblock %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 先加载基础库 -->
|
||||
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
|
||||
<!-- 然后加载依赖库 -->
|
||||
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/chart.js@3.9.1/dist/chart.min.js"></script>
|
||||
<!-- 最后加载Element UI -->
|
||||
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
|
||||
|
||||
<!-- 添加基础样式 -->
|
||||
<style>
|
||||
.el-header {
|
||||
background-color: #409EFF;
|
||||
color: white;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 0 20px;
|
||||
}
|
||||
.el-footer {
|
||||
background-color: #f5f7fa;
|
||||
color: #606266;
|
||||
text-align: center;
|
||||
line-height: 60px;
|
||||
}
|
||||
.logo {
|
||||
height: 60px;
|
||||
line-height: 60px;
|
||||
text-align: center;
|
||||
color: white;
|
||||
font-size: 20px;
|
||||
font-weight: bold;
|
||||
}
|
||||
.el-dropdown-link {
|
||||
color: white;
|
||||
cursor: pointer;
|
||||
}
|
||||
.footer-content {
|
||||
font-size: 14px;
|
||||
}
|
||||
.header-title {
|
||||
font-size: 18px;
|
||||
font-weight: bold;
|
||||
}
|
||||
</style>
|
||||
|
||||
<!-- 基础 Vue 实例 -->
|
||||
<script>
|
||||
var app = new Vue({
|
||||
el: '#app',
|
||||
const baseApp = {
|
||||
data() {
|
||||
return {
|
||||
timeRange: '7'
|
||||
currentView: '1',
|
||||
timeRange: '7',
|
||||
charts: {}
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
handleCommand(command) {
|
||||
if (command === 'logout') {
|
||||
window.location.href = '/logout';
|
||||
handleSelect(key, keyPath) {
|
||||
this.currentView = key;
|
||||
// 添加页面跳转逻辑
|
||||
const routes = {
|
||||
'1': '/',
|
||||
'2': '/plugins',
|
||||
'3': '/users',
|
||||
'4': '/groups',
|
||||
'5': '/errors',
|
||||
'6': '/robot_management'
|
||||
};
|
||||
|
||||
// 如果当前不在对应页面,则跳转
|
||||
const currentPath = window.location.pathname;
|
||||
const targetPath = routes[key];
|
||||
if (currentPath !== targetPath && targetPath !== undefined) {
|
||||
window.location.href = targetPath;
|
||||
}
|
||||
},
|
||||
loadData() {
|
||||
// 由子组件实现
|
||||
console.log('父组件loadData被调用,时间范围:', this.timeRange);
|
||||
// 不使用事件发射,而是直接调用子组件方法
|
||||
if (window.pageComponent && typeof window.pageComponent.loadData === 'function') {
|
||||
window.pageComponent.loadData(this.timeRange);
|
||||
}
|
||||
},
|
||||
// 添加退出登录方法
|
||||
logout() {
|
||||
// 显示确认对话框
|
||||
this.$confirm('确认退出登录吗?', '提示', {
|
||||
confirmButtonText: '确定',
|
||||
cancelButtonText: '取消',
|
||||
type: 'warning'
|
||||
}).then(() => {
|
||||
// 用户点击确定,跳转到登出页面
|
||||
window.location.href = '/logout';
|
||||
}).catch(() => {
|
||||
// 用户点击取消,不做任何操作
|
||||
});
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
console.log('基础Vue实例已挂载');
|
||||
}
|
||||
});
|
||||
|
||||
// 导出Vue实例供子组件使用
|
||||
window.baseApp = app;
|
||||
};
|
||||
</script>
|
||||
|
||||
{% block scripts %}{% endblock %}
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user