Prechádzať zdrojové kódy

添加数据 call 0 1 2 统计

zhuizhu 7 mesiacov pred
rodič
commit
7b2ebb16a6

+ 2 - 0
LearningSmartClient.pro

@@ -44,6 +44,7 @@ SOURCES += \
     widgets/joinmeetingdialog.cpp \
     widgets/maskoverlay.cpp \
     widgets/meetingselectionwidget.cpp \
+    widgets/statswidget.cpp \
     widgets/userprofilewidget.cpp
 
 HEADERS += \
@@ -80,6 +81,7 @@ HEADERS += \
     widgets/joinmeetingdialog.h \
     widgets/maskoverlay.h \
     widgets/meetingselectionwidget.h \
+    widgets/statswidget.h \
     widgets/userprofilewidget.h
 
 msvc {

+ 4 - 0
MainPanel.cpp

@@ -17,6 +17,7 @@
 #include "widgets/chatView/chatwindow.h"
 #include "widgets/maskoverlay.h"
 #include "widgets/userprofilewidget.h"
+#include "widgets/statswidget.h"
 
 #include "AvPlayer2/PlayWidget.h"
 #include "api/roomapi.h"
@@ -38,11 +39,13 @@ MainPanel::MainPanel(QWidget *parent)
     webSocketClient = new WebSocketClient(this);
     chatView = new ChatWindow(webSocketClient);
     chatView->setMinimumWidth(400);
+    statsWidget = new StatsWidget(this);
 
     QWidget *rightWidget = new QWidget;
     QVBoxLayout *vbox = new QVBoxLayout(rightWidget);
     vbox->setContentsMargins(0, 0, 0, 0);
     vbox->addWidget(userProfile, 0);
+    vbox->addWidget(statsWidget, 0);
     vbox->addWidget(chatView, 1);
 
     m_roomListWidget = new QListWidget;
@@ -68,6 +71,7 @@ MainPanel::MainPanel(QWidget *parent)
     });
     connect(m_roomListWidget, &QListWidget::itemDoubleClicked, this, &MainPanel::roomItemChanged);
     connect(userProfile, &UserProfileWidget::logoutClicked, this, &MainPanel::logoutClicked);
+    connect(webSocketClient, &WebSocketClient::statsUpdate, statsWidget, &StatsWidget::updateStats);
     connect(webSocketClient, &WebSocketClient::liveStatus, this, [this](const QString &msg) {
         // 这里可以处理 liveStatus 相关逻辑
         QJsonParseError err;

+ 2 - 0
MainPanel.h

@@ -12,6 +12,7 @@ class UserProfileWidget;
 class ChatWindow;
 class QListWidgetItem;
 class WebSocketClient;
+class StatsWidget;
 
 class MainPanel : public QWidget
 {
@@ -41,6 +42,7 @@ private:
     ChatWindow *chatView = nullptr;
     QListWidget *m_roomListWidget = nullptr;
     WebSocketClient *webSocketClient = nullptr;
+    StatsWidget *statsWidget = nullptr;
     bool m_isStartingPlay = false;
     QMutex m_playMutex;
     QWaitCondition m_playCond;

+ 4 - 1
network/networkaccessmanager.cpp

@@ -13,7 +13,10 @@
 #include <QtConcurrent>
 
 // 基础URL配置
-static QString base_url("http://106.55.186.74:8200");
+// static QString base_url("http://106.55.186.74:8200");
+
+static QString base_url("http://127.0.0.1:8200");
+
 static const QString messageVal = "msg";
 
 static NetworkAccessManager* namInstance = nullptr;

+ 5 - 0
network/websocketclient.cpp

@@ -161,9 +161,14 @@ void WebSocketClient::onTextMessageReceived(const QString& message)
             chatMessage.type = MessageType::System;
         } else if (type == "private") {
             chatMessage.type = MessageType::Right;
+        } else if (type == "ping") {
+            return;
         } else if (type == "room_live_status") {
             emit liveStatus(content);
             return;
+        } else if (type == "stats_update") {
+            emit statsUpdate(obj);
+            return;
         }
 
         // 设置消息内容

+ 2 - 0
network/websocketclient.h

@@ -40,6 +40,8 @@ signals:
     void messageReceived(const ChatMessage& message);
     // 直播状态
     void liveStatus(const QString& message);
+    // 统计信息更新
+    void statsUpdate(const QJsonObject& statsData);
 
     // 连接状态变化信号
     void connectionStateChanged(bool connected);

+ 162 - 0
widgets/statswidget.cpp

@@ -0,0 +1,162 @@
+#include "statswidget.h"
+#include <QDebug>
+
+StatsWidget::StatsWidget(QWidget *parent)
+    : QWidget(parent)
+    , m_statsGroup(nullptr)
+    , m_totalCountLabel(nullptr)
+    , m_type0CountLabel(nullptr)
+    , m_type1CountLabel(nullptr)
+    , m_type2CountLabel(nullptr)
+    , m_lastUpdateLabel(nullptr)
+    , m_totalCount(0)
+    , m_type0Count(0)
+    , m_type1Count(0)
+    , m_type2Count(0)
+{
+    setupUI();
+    applyStyles();
+}
+
+StatsWidget::~StatsWidget()
+{
+}
+
+void StatsWidget::setupUI()
+{
+    // 创建主布局
+    QVBoxLayout *mainLayout = new QVBoxLayout(this);
+    mainLayout->setContentsMargins(5, 5, 5, 5);
+    mainLayout->setSpacing(5);
+
+    // 创建统计信息组
+    m_statsGroup = new QGroupBox("消息统计", this);
+    QVBoxLayout *statsLayout = new QVBoxLayout(m_statsGroup);
+    statsLayout->setContentsMargins(10, 10, 10, 10);
+    statsLayout->setSpacing(8);
+
+    // 总消息数
+    m_totalCountLabel = new QLabel("总消息数: 0", this);
+    m_totalCountLabel->setObjectName("totalCountLabel");
+    statsLayout->addWidget(m_totalCountLabel);
+
+    // 创建消息类型统计的水平布局
+    QHBoxLayout *typeLayout = new QHBoxLayout();
+    typeLayout->setSpacing(15);
+
+    // 类型0消息数
+    m_type0CountLabel = new QLabel("类型0: 0", this);
+    m_type0CountLabel->setObjectName("type0CountLabel");
+    typeLayout->addWidget(m_type0CountLabel);
+
+    // 类型1消息数
+    m_type1CountLabel = new QLabel("类型1: 0", this);
+    m_type1CountLabel->setObjectName("type1CountLabel");
+    typeLayout->addWidget(m_type1CountLabel);
+
+    // 类型2消息数
+    m_type2CountLabel = new QLabel("类型2: 0", this);
+    m_type2CountLabel->setObjectName("type2CountLabel");
+    typeLayout->addWidget(m_type2CountLabel);
+
+    statsLayout->addLayout(typeLayout);
+
+    // 最后更新时间
+    m_lastUpdateLabel = new QLabel("最后更新: --", this);
+    m_lastUpdateLabel->setObjectName("lastUpdateLabel");
+    statsLayout->addWidget(m_lastUpdateLabel);
+
+    // 添加到主布局
+    mainLayout->addWidget(m_statsGroup);
+    mainLayout->addStretch();
+
+    setLayout(mainLayout);
+    setMaximumHeight(150);
+}
+
+void StatsWidget::applyStyles()
+{
+    setStyleSheet(
+        "QGroupBox {"
+        "    font-weight: bold;"
+        "    border: 2px solid #cccccc;"
+        "    border-radius: 5px;"
+        "    margin-top: 1ex;"
+        "    padding-top: 5px;"
+        "}"
+        "QGroupBox::title {"
+        "    subcontrol-origin: margin;"
+        "    left: 10px;"
+        "    padding: 0 5px 0 5px;"
+        "}"
+        "#totalCountLabel {"
+        "    font-weight: bold;"
+        "    color: #2c3e50;"
+        "    font-size: 14px;"
+        "}"
+        "#type0CountLabel, #type1CountLabel, #type2CountLabel {"
+        "    color: #34495e;"
+        "    font-size: 12px;"
+        "}"
+        "#lastUpdateLabel {"
+        "    color: #7f8c8d;"
+        "    font-size: 11px;"
+        "}"
+    );
+}
+
+void StatsWidget::updateStats(const QJsonObject& statsData)
+{
+    qDebug() << "[StatsWidget] 收到统计更新:" << statsData;
+    
+    // 解析data数组并累加所有用户的统计数据
+    if (statsData.contains("data") && statsData["data"].isArray()) {
+        QJsonArray dataArray = statsData["data"].toArray();
+        
+        // 重置累计数据
+        m_totalCount = 0;
+        m_type0Count = 0;
+        m_type1Count = 0;
+        m_type2Count = 0;
+        QDateTime latestUpdate;
+        
+        // 遍历所有用户数据进行累加
+        for (const QJsonValue& value : dataArray) {
+            if (value.isObject()) {
+                QJsonObject userStats = value.toObject();
+                
+                // 累加各类型消息数
+                m_totalCount += userStats["totalCount"].toInt();
+                m_type0Count += userStats["type0Count"].toInt();
+                m_type1Count += userStats["type1Count"].toInt();
+                m_type2Count += userStats["type2Count"].toInt();
+                
+                // 找到最新的更新时间
+                QString lastUpdateStr = userStats["lastUpdate"].toString();
+                QDateTime userUpdate = QDateTime::fromString(lastUpdateStr, Qt::ISODate);
+                if (userUpdate.isValid() && (!latestUpdate.isValid() || userUpdate > latestUpdate)) {
+                    latestUpdate = userUpdate;
+                }
+            }
+        }
+        
+        m_lastUpdate = latestUpdate;
+        
+        // 更新UI显示
+        m_totalCountLabel->setText(QString("总消息数: %1").arg(m_totalCount));
+        m_type0CountLabel->setText(QString("类型0: %1").arg(m_type0Count));
+        m_type1CountLabel->setText(QString("类型1: %1").arg(m_type1Count));
+        m_type2CountLabel->setText(QString("类型2: %1").arg(m_type2Count));
+        
+        if (m_lastUpdate.isValid()) {
+            m_lastUpdateLabel->setText(QString("最后更新: %1")
+                .arg(m_lastUpdate.toString("yyyy-MM-dd hh:mm:ss")));
+        } else {
+            m_lastUpdateLabel->setText("最后更新: --");
+        }
+        
+        qDebug() << "[StatsWidget] 全局统计信息已更新 - 总数:" << m_totalCount 
+                 << "类型0:" << m_type0Count << "类型1:" << m_type1Count 
+                 << "类型2:" << m_type2Count << "用户数:" << dataArray.size();
+    }
+}

+ 44 - 0
widgets/statswidget.h

@@ -0,0 +1,44 @@
+#ifndef STATSWIDGET_H
+#define STATSWIDGET_H
+
+#include <QWidget>
+#include <QLabel>
+#include <QVBoxLayout>
+#include <QHBoxLayout>
+#include <QGroupBox>
+#include <QJsonObject>
+#include <QJsonArray>
+#include <QDateTime>
+
+class StatsWidget : public QWidget
+{
+    Q_OBJECT
+
+public:
+    explicit StatsWidget(QWidget *parent = nullptr);
+    ~StatsWidget();
+
+    // 更新统计信息
+    void updateStats(const QJsonObject& statsData);
+
+private:
+    void setupUI();
+    void applyStyles();
+    
+    // UI组件
+    QGroupBox *m_statsGroup;
+    QLabel *m_totalCountLabel;
+    QLabel *m_type0CountLabel;
+    QLabel *m_type1CountLabel;
+    QLabel *m_type2CountLabel;
+    QLabel *m_lastUpdateLabel;
+    
+    // 数据
+    int m_totalCount;
+    int m_type0Count;
+    int m_type1Count;
+    int m_type2Count;
+    QDateTime m_lastUpdate;
+};
+
+#endif // STATSWIDGET_H