Jelajahi Sumber

完善 本地配置 函数 独立

zhuizhu 1 tahun lalu
induk
melakukan
66b2c15c7d
3 mengubah file dengan 264 tambahan dan 121 penghapusan
  1. 1 1
      api/configapi.cpp
  2. 253 120
      processthread.cpp
  3. 10 0
      processthread.h

+ 1 - 1
api/configapi.cpp

@@ -40,7 +40,7 @@ std::optional<QVariant> ConfigApi::get()
 {
     std::optional<QJsonValue> responseData = sendRequest(QNetworkAccessManager::GetOperation, url);
     if (!responseData.has_value()) {
-        return false;
+        return std::nullopt;
     }
 
     const QJsonObject &object = responseData.value().toObject();

+ 253 - 120
processthread.cpp

@@ -1,5 +1,6 @@
 #include "processthread.h"
 #include <QProcess>
+#include <QSettings>
 #include <QTemporaryFile>
 #include "api/configapi.h"
 #include "appevent.h"
@@ -59,11 +60,119 @@ QString convertSecondsToMinutesTimeFormat(int totalSeconds)
     int minutes = totalSeconds / 60;
     return QString::number(minutes);
 }
-void ProcessThread::sendExitTime(qint64 updataTime)
+
+static QVariant config()
+{
+    QSettings settings(QSettings::NativeFormat,
+                       QSettings::UserScope,
+                       QCoreApplication::organizationName(),
+                       QCoreApplication::applicationName());
+
+    settings.beginGroup("config");
+    QVariant value;
+    if (settings.contains("serverConfig")) {
+        value = settings.value("serverConfig");
+    } else {
+        value = R"Raw([
+        {
+            "uuid": "3d8a0525-76fa-45bb-b0db-ed7260e31bdc",
+            "name": "sldworks.exe",
+            "chinese_name": "SolidWorks",
+            "pid_type": "学习",
+            "is_confirmed": 1,
+            "is_prompt": 2,
+            "prompt_type": "",
+            "prompt_time": 2400,
+            "path": "",
+            "notes": "",
+            "c_time": 1734346737,
+            "u_time": 0
+        },
+        {
+            "uuid": "daa98539-fb91-4eb6-a59f-0fd4509e926c",
+            "name": "sldworks_fs.exe",
+            "chinese_name": "SolidWorks",
+            "pid_type": "学习",
+            "is_confirmed": 2,
+            "is_prompt": 1,
+            "prompt_type": "",
+            "prompt_time": 120,
+            "path": "",
+            "notes": "",
+            "c_time": 1733906424,
+            "u_time": 0
+        },
+        {
+            "uuid": "d09df0b1-2386-4527-afa7-3f1fb37b6171",
+            "name": "SLDWORKS.exe",
+            "chinese_name": "SolidWorks",
+            "pid_type": "学习",
+            "is_confirmed": 1,
+            "is_prompt": 2,
+            "prompt_type": "",
+            "prompt_time": 2400,
+            "path": "",
+            "notes": "",
+            "c_time": 1733652641,
+            "u_time": 0
+        },
+        {
+            "uuid": "c4de744f-0afe-4e0f-a39e-7ee59f46c042",
+            "name": "WeChat.exe",
+            "chinese_name": "微信",
+            "pid_type": "日常",
+            "is_confirmed": 1,
+            "is_prompt": 2,
+            "prompt_type": "",
+            "prompt_time": 15,
+            "path": "2",
+            "notes": "测试",
+            "c_time": 1733455089,
+            "u_time": 0
+        }
+    ])Raw";
+    }
+    settings.endGroup();
+    return value;
+}
+
+const QJsonArray processNameArray()
 {
     TC::ProcessNameApi processNameApi;
     QJsonArray serverArray = processNameApi.get();
 
+    if (serverArray.isEmpty()) {
+        // 读取本地配置 转 QJsonArray
+        const QString configText = config().toString();
+        QJsonDocument jsonDoc = QJsonDocument::fromJson(configText.toUtf8());
+        if (jsonDoc.isArray()) {
+            serverArray = jsonDoc.array();
+        } else if (jsonDoc.isObject()) {
+            // If the config is a single object, we can wrap it in an array
+            serverArray.append(jsonDoc.object());
+        }
+    } else {
+        // 写入本地配置
+        QSettings settings(QSettings::NativeFormat,
+                           QSettings::UserScope,
+                           QCoreApplication::organizationName(),
+                           QCoreApplication::applicationName());
+
+        settings.beginGroup("config");
+        settings.setValue("serverArray",
+                          QString(QJsonDocument(serverArray).toJson(QJsonDocument::Compact)));
+        settings.endGroup();
+    }
+
+    return serverArray;
+}
+
+void ProcessThread::sendExitTime(qint64 updataTime)
+{
+    serverConfig();
+
+    QJsonArray serverArray = processNameArray();
+
     // 获取过滤名单
     QSet<QString> filter;
     QSet<QString> gameFilter;
@@ -78,104 +187,11 @@ void ProcessThread::sendExitTime(qint64 updataTime)
                 filter.insert(name);
             }
         }
-        if (object.contains("pid_type")) {
-            const QString type = object["pid_type"].toString();
-            if (type == "游戏" || type == "game") {
-                gameFilter.insert(name);
-            }
-        }
     }
+    // 发送数据到服务器
+    sendToServer(filter, updataTime);
 
     ProcessModel processModel{storage};
-    // 在数据库 获取 不是这个更新日期的数据
-
-    CWF::SqlQueryManager qry(storage);
-    qry.select("*", processModel.getTableName())
-        .where(QString("updataTime != '%1' OR updataTime IS NULL").arg(updataTime));
-    qry.prepare();
-    QJsonObject jsonObject = qry.exec();
-
-    QJsonArray jsonArray = qry.toJson();
-
-    QJsonArray sendJsonArray;
-    if (jsonArray.size() > 0) {
-        for (const auto &json : jsonArray) {
-            const QJsonObject object = json.toObject();
-
-            const QString name = object["processName"].toString();
-            QJsonObject sendObject;
-            sendObject.insert("pid", object["pid"]);
-            sendObject.insert("pid_name", object["processName"]);
-            sendObject.insert("begin_time", object["creationTime"]);
-            sendObject.insert("end_time", object["exitTime"]);
-            sendObject.insert("last_check_time", object["updataTime"]);
-            sendObject.insert("status", 1);
-            sendObject.insert("notes", "");
-
-            // 存在过滤里面的 不发送数据
-            if (!filter.contains(name)) {
-                sendJsonArray.append(sendObject);
-            }
-        }
-    }
-    // 上传 后 删除
-    if (sendJsonArray.size() > 0) {
-        TC::ProcessApi processApi(sendJsonArray);
-        bool isSendok = processApi.post();
-        if (isSendok) {
-            // 移除发送到服务器的本地数据
-            CWF::SqlQueryManager qry(storage);
-            qry.remove(processModel.getTableName(),
-                       QString("updataTime != '%1' OR updataTime IS NULL").arg(updataTime));
-            qry.prepare();
-            QJsonObject jsonObject = qry.exec();
-
-            QJsonArray jsonArray = qry.toJson();
-        }
-    }
-
-    // 获取配置
-    QVariant messageBoxPointX = QVariant();
-    QVariant messageBoxPointY = QVariant();
-    QVariant messageText = QVariant("");
-    QVariant gameMessageText = QVariant("");
-    QVariant messageTitle = QVariant(Tr::tr("Tips"));
-    {
-        TC::ConfigApi configApi("messageBoxPointX");
-        std::optional<QVariant> value = configApi.get();
-        if (value.has_value()) {
-            messageBoxPointX = value.value();
-        }
-    }
-    {
-        TC::ConfigApi configApi("messageBoxPointY");
-        std::optional<QVariant> value = configApi.get();
-        if (value.has_value()) {
-            messageBoxPointY = value.value();
-        }
-    }
-    {
-        TC::ConfigApi configApi("messageText");
-        std::optional<QVariant> value = configApi.get();
-        if (value.has_value()) {
-            messageText = value.value();
-        }
-    }
-    {
-        TC::ConfigApi configApi("messageTitle");
-        std::optional<QVariant> value = configApi.get();
-        if (value.has_value()) {
-            messageTitle = value.value();
-        }
-    }
-    {
-        TC::ConfigApi configApi("gameMessageText");
-        std::optional<QVariant> value = configApi.get();
-        if (value.has_value()) {
-            gameMessageText = value.value();
-        }
-    }
-
     // 获取服务器 对应的时间信息
     for (const QJsonValue &item : serverArray) {
         if (!item.isObject()) {
@@ -260,8 +276,6 @@ void ProcessThread::sendExitTime(qint64 updataTime)
                                                      .arg(convertSecondsToMinutesTimeFormat(runTime))
                                                      .arg(messageText.toString()));
 
-                    qDebug() << "玩游戏?" << text;
-
                     QTemporaryFile tempFile("tempfile_XXXXXX.png");
                     tempFile.setAutoRemove(true);
                     tempFile.open();
@@ -272,10 +286,7 @@ void ProcessThread::sendExitTime(qint64 updataTime)
                     qDebug() << "发送截图" << processImageApi.post({tempFile.fileName()});
                     tempFile.close();
                     qDebug() << "发送截图" << tempFile.remove() << tempFile.errorString();
-                    // if (object.contains("is_prompt")) { // 提示
-                    //     if (object["is_prompt"].toInt() == 2) {
-                    //     }
-                    // }
+
                     const QString message = gameMessageText.toString().isEmpty()
                                                 ? text
                                                 : gameMessageText.toString();
@@ -293,26 +304,61 @@ void ProcessThread::sendExitTime(qint64 updataTime)
                     processModel.setLastAlertTime(updataTime);
                     processModel.save();
                 }
+            }
+        }
+    }
+}
+
+void ProcessThread::sendToServer(const QSet<QString> &filter, qint64 updataTime)
+{
+    ProcessModel processModel{storage};
+    // 在数据库 获取 不是这个更新日期的数据
+
+    CWF::SqlQueryManager qry(storage);
+    qry.select("*", processModel.getTableName())
+        .where(QString("updataTime != '%1' OR updataTime IS NULL").arg(updataTime));
+    qry.prepare();
+    QJsonObject jsonObject = qry.exec();
+
+    QJsonArray jsonArray = qry.toJson();
+
+    QJsonArray sendJsonArray;
+    if (jsonArray.size() > 0) {
+        for (const auto &json : jsonArray) {
+            const QJsonObject object = json.toObject();
 
-                // 获取游戏 截图 并关闭进程
-                // QTemporaryFile tempFile("tempfile_XXXXXX.png");
-                // tempFile.setAutoRemove(true);
-                // tempFile.open();
-                // AppEvent::captureDesktop(&tempFile, "PNG");
-
-                // qDebug() << "发送截图" << tempFile.fileName();
-                // TC::ProcessImageApi processImageApi;
-                // qDebug() << "发送截图" << processImageApi.post({tempFile.fileName()});
-
-                // tempFile.close();
-                // qDebug() << "发送截图" << tempFile.remove() << tempFile.errorString();
-                // if (object.contains("is_prompt")) { // 提示
-                //     if (object["is_prompt"].toInt() == 2) {
-                //     }
-                // }
+            const QString name = object["processName"].toString();
+            QJsonObject sendObject;
+            sendObject.insert("pid", object["pid"]);
+            sendObject.insert("pid_name", object["processName"]);
+            sendObject.insert("begin_time", object["creationTime"]);
+            sendObject.insert("end_time", object["exitTime"]);
+            sendObject.insert("last_check_time", object["updataTime"]);
+            sendObject.insert("status", 1);
+            sendObject.insert("notes", "");
+
+            // 存在过滤里面的 不发送数据
+            if (!filter.contains(name)) {
+                sendJsonArray.append(sendObject);
             }
         }
     }
+    // 上传 后 删除
+    if (sendJsonArray.size() > 0) {
+        TC::ProcessApi processApi(sendJsonArray);
+        bool isSendok = processApi.post();
+        if (isSendok) {
+            // 上传 成功会 推送 并清理本地数据
+            // 移除发送到服务器的本地数据
+            CWF::SqlQueryManager qry(storage);
+            qry.remove(processModel.getTableName(),
+                       QString("updataTime != '%1' OR updataTime IS NULL").arg(updataTime));
+            qry.prepare();
+            QJsonObject jsonObject = qry.exec();
+
+            QJsonArray jsonArray = qry.toJson();
+        }
+    }
 }
 
 bool ProcessThread::upDataProcessSql()
@@ -367,11 +413,98 @@ bool ProcessThread::upDataProcessSql()
     return false;
 }
 
-ProcessThread::ProcessThread(
-    QObject *parent)
+ProcessThread::ProcessThread(QObject *parent)
     : QThread{parent}
+    , messageBoxPointX(QVariant())
+    , messageBoxPointY(QVariant())
+    , messageText(QVariant(""))
+    , gameMessageText(QVariant(""))
+    , messageTitle(QVariant(Tr::tr("Tips")))
 {}
 
+bool ProcessThread::serverConfig()
+{
+    QSettings settings(QSettings::NativeFormat,
+                       QSettings::UserScope,
+                       QCoreApplication::organizationName(),
+                       QCoreApplication::applicationName());
+    settings.beginGroup("config");
+    // 获取配置
+    // QVariant messageBoxPointX = QVariant();
+    // QVariant messageBoxPointY = QVariant();
+    // QVariant messageText = QVariant("");
+    // QVariant gameMessageText = QVariant("");
+    // QVariant messageTitle = QVariant(Tr::tr("Tips"));
+    {
+        TC::ConfigApi configApi("messageBoxPointX");
+        std::optional<QVariant> value = configApi.get();
+        if (value.has_value()) {
+            messageBoxPointX = value.value();
+            settings.setValue("messageBoxPointX", messageBoxPointX);
+        } else {
+            if (settings.contains("messageBoxPointX")) {
+                messageBoxPointX = settings.value("messageBoxPointX");
+            }
+        }
+    }
+    {
+        TC::ConfigApi configApi("messageBoxPointY");
+        std::optional<QVariant> value = configApi.get();
+        if (value.has_value()) {
+            messageBoxPointY = value.value();
+            settings.setValue("messageBoxPointY", messageBoxPointY);
+        } else {
+            if (settings.contains("messageBoxPointX")) {
+                messageBoxPointY = settings.value("messageBoxPointY");
+            }
+        }
+    }
+    {
+        TC::ConfigApi configApi("messageText");
+        std::optional<QVariant> value = configApi.get();
+        if (value.has_value()) {
+            messageText = value.value();
+            settings.setValue("messageText", messageText);
+        } else {
+            if (settings.contains("messageText")) {
+                messageText = settings.value("messageText");
+            } else {
+                messageText = ", 请运动一下";
+            }
+        }
+    }
+    {
+        TC::ConfigApi configApi("messageTitle");
+        std::optional<QVariant> value = configApi.get();
+        if (value.has_value()) {
+            messageTitle = value.value();
+            settings.setValue("messageTitle", messageTitle);
+        } else {
+            if (settings.contains("messageTitle")) {
+                messageTitle = settings.value("messageTitle");
+            } else {
+                messageTitle = "提示";
+            }
+        }
+    }
+    {
+        TC::ConfigApi configApi("gameMessageText");
+        std::optional<QVariant> value = configApi.get();
+        if (value.has_value()) {
+            gameMessageText = value.value();
+            settings.setValue("gameMessageText", gameMessageText);
+        } else {
+            if (settings.contains("gameMessageText")) {
+                gameMessageText = settings.value("gameMessageText");
+            } else {
+                gameMessageText = "请不要玩游戏";
+            }
+        }
+    }
+    settings.endGroup();
+    return true;
+};
+
 void ProcessThread::run()
 {
     ProcessModel processModel{storage};

+ 10 - 0
processthread.h

@@ -11,6 +11,8 @@ class ProcessThread : public QThread
     Q_OBJECT
 public:
     explicit ProcessThread(QObject *parent = nullptr);
+
+    bool serverConfig();
 Q_SIGNALS:
 
     void messageBox(const QString &text,
@@ -24,8 +26,16 @@ private:
 protected:
     void run() override;
     void sendExitTime(qint64 updataTime);
+    void sendToServer(const QSet<QString> &filter, qint64 updataTime);
 
     ProcessMonitor processMonitor;
+
+private:
+    QVariant messageBoxPointX;
+    QVariant messageBoxPointY;
+    QVariant messageText;
+    QVariant gameMessageText;
+    QVariant messageTitle;
 };
 
 #endif // PROCESSTHREAD_H