Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/BarcodeWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "version_info/version.h"
#include <QCheckBox>
#include <QComboBox>
#include <QDesktopServices>
#include <QFileDialog>
#include <QFont>
#include <QFutureWatcher>
Expand Down Expand Up @@ -361,6 +362,8 @@ BarcodeWidget::BarcodeWidget(QWidget *parent)
if (!styleSheet.isEmpty()) {
this->setStyleSheet(styleSheet);
}
// 进行版本检查
QTimer::singleShot(0, this, &BarcodeWidget::checkUpdateOnStartup);
}

void BarcodeWidget::updateButtonStates() const {
Expand Down Expand Up @@ -1119,6 +1122,46 @@ ZXing::BarcodeFormat BarcodeWidget::stringToBarcodeFormat(const QString &formatS
return map.value(key, ZXing::BarcodeFormat::None); // 未匹配时返回None
}

void BarcodeWidget::checkUpdateOnStartup() {
if (!m_updateChecker) {
m_updateChecker = new UpdateChecker(this);
}
// 后端接口地址
QUrl apiUrl("http://106.14.192.58:10000/update/check_version");
// 获取当前版本和架构
//QString version = version::git_tag.data();
//QString architecture = version::architecture.data();
QString test_version = "v1.0";
QString test_os = "windows-x64";
UpdateCheckRequest request(test_version, test_os);
// 连接信号
connect(m_updateChecker, &UpdateChecker::UpdateAvailable, this, [this](const UpdateInfo &info) {
QMessageBox box(this);
box.setWindowTitle("发现新版本");
box.setText(QString("最新版本:%1\n\n更新日志:\n%2\n\n更新链接:%3\n")
.arg(info.latest, info.changeLog, info.downloadUrl));

QPushButton *btnUpdate = box.addButton("立即更新", QMessageBox::AcceptRole);
QPushButton *btnLater = box.addButton("稍后再说", QMessageBox::RejectRole);

box.exec();

if (box.clickedButton() == btnUpdate) {
QDesktopServices::openUrl(QUrl(info.downloadUrl));
} else if (box.clickedButton() == btnLater) {
// 关闭弹窗即可;exec() 返回后 box 会析构
}
});
connect(m_updateChecker, &UpdateChecker::NoUpdate, this, []() { spdlog::info("No update!"); });

connect(m_updateChecker, &UpdateChecker::ErrorOccured, this, [](const QString &msg) {
spdlog::error("UpdateCheck Error, Error Message: {}", msg.toStdString());
});

// 发起检查
m_updateChecker->Check(apiUrl, request);
}

void BarcodeWidget::setupLanguageAction() {
LanguageManager &languageMgr = LanguageManager::instance();

Expand Down
8 changes: 8 additions & 0 deletions src/BarcodeWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <qfuturewatcher.h>

#include "CameraWidget.h"
#include "UpdateChecker.h"
#include "convert.h"
#include "mqtt/MQTTMessageWidget.h"
#include "mqtt/mqtt_client.h"
Expand Down Expand Up @@ -122,6 +123,12 @@ private slots:
*/
static ZXing::BarcodeFormat stringToBarcodeFormat(const QString &formatStr);

/**
* @brief 在程序启动时与服务器通信判断是否需要更新
*
*/
void checkUpdateOnStartup();

private:
/**
* @brief 初始化语言切换的Action
Expand Down Expand Up @@ -173,4 +180,5 @@ private slots:
std::unique_ptr<MqttSubscriber> subscriber_; /**< MQTT订阅者实例 */
std::unique_ptr<MQTTMessageWidget> messageWidget; /**< MQTT消息展示窗口 */
CameraWidget preview; /**< 摄像头预览窗口 */
UpdateChecker *m_updateChecker = nullptr; /**< 版本检查实例 */
};
53 changes: 53 additions & 0 deletions src/UpdateChecker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "UpdateChecker.h"
#include <QJsonDocument>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QTimer>

UpdateChecker::UpdateChecker(QObject *parent)
: QObject(parent) {}

void UpdateChecker::Check(const QUrl &apiUrl, const UpdateCheckRequest &request) {
// 1. ���� request
QNetworkRequest req(apiUrl);
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
QJsonDocument doc(request.toJson());
QByteArray body = doc.toJson(QJsonDocument::Compact);

// 2. �����������󣬳�ʱ����
QNetworkReply *reply = m_nam.post(req, body);
QTimer *timer = new QTimer(reply);
timer->setSingleShot(true);
timer->start(4000);
QObject::connect(timer, &QTimer::timeout, reply, [reply]() { reply->abort(); });

// 3. �յ��ذ�����
QObject::connect(reply, &QNetworkReply::finished, this, [this, reply]() {
const auto err = reply->error();
const QByteArray body = reply->readAll();
reply->deleteLater();
// 3.1 ��������
if (err != QNetworkReply::NoError) {
emit ErrorOccured(QString("�������: %1").arg(reply->errorString()));
return;
}
// 3.2 ��ȡ�ذ�
QJsonParseError pe{};
QJsonDocument doc = QJsonDocument::fromJson(body, &pe);
if (pe.error != QJsonParseError::NoError || !doc.isObject()) {
emit ErrorOccured(QString("���ز��ǺϷ� json"));
return;
}
QJsonObject o = doc.object();
UpdateInfo info;
info.latest = o.value("version").toString();
info.downloadUrl = o.value("update_url").toString();
info.changeLog = o.value("update_log").toString();
info.updateNeed = o.value("update_need").toBool();
if (info.updateNeed) {
emit UpdateAvailable(info);
} else {
emit NoUpdate();
}
});
}
38 changes: 38 additions & 0 deletions src/UpdateChecker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once
#include <QJsonObject>
#include <QNetworkAccessManager>
#include <QObject>

struct UpdateInfo {
QString latest; /**< ���°汾�� */
QString downloadUrl; /**< ����·�� */
QString changeLog; /**< �汾������־ */
bool updateNeed = false; /**< �Ƿ���Ҫ���� */
};

struct UpdateCheckRequest {
QString version; /**< �ͻ��˰汾 */
QString osArch; /**< �ͻ��˼ܹ� */
UpdateCheckRequest(const QString _version, const QString _os)
: version(_version), osArch(_os) {}
// ����� JSON
QJsonObject toJson() const {
QJsonObject o;
o.insert("version", version);
o.insert("os-arch", osArch);
return o;
}
};
class UpdateChecker : public QObject {
Q_OBJECT
public:
explicit UpdateChecker(QObject *parent = nullptr);
void Check(const QUrl &apiUrl, const UpdateCheckRequest &request);
signals:
void NoUpdate();
void UpdateAvailable(const UpdateInfo &info);
void ErrorOccured(const QString &msg);

private:
QNetworkAccessManager m_nam;
};
Loading