1 Star 0 Fork 4

陈雄 / autoUpdater

forked from 呐喊 / autoUpdater 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
checkversion.cpp 2.56 KB
一键复制 编辑 原始数据 按行查看 历史
呐喊 提交于 2022-07-27 21:41 . V1.0.1 Release
#include "checkversion.h"
#pragma execution_character_set("utf-8")
CheckVersion::CheckVersion(QObject *parent,QString remoteUrl)
: QObject{parent}
{
this->remoteInfoUrl = remoteUrl;
}
/**
* 发起http请求,请求后端版本更新信息
* @brief CheckVersion::requestRemoteVersion
*/
void CheckVersion::requestRemoteVersion(){
QUrl url(this->remoteInfoUrl);
emit sendMsg(tr("请求中..."));
QNetworkRequest req;
QNetworkAccessManager nam;
connect(&nam,&QNetworkAccessManager::finished,this,&CheckVersion::requestRemoteVersionFinished);
req.setUrl(url);
QNetworkReply *reply = nam.get(req);
QEventLoop loop;
connect(reply,&QNetworkReply::finished,&loop,&QEventLoop::quit);
loop.exec();
}
/**
* 接收http请求响应数据
* @brief requestRemoteVersionFinished
* @param reply
*/
void CheckVersion::requestRemoteVersionFinished(QNetworkReply *reply){
QString rsdata = reply->readAll();
int sysVersion = this->readSysVersion();
QJsonParseError jsonParseError;
QJsonDocument jsonDoc(QJsonDocument::fromJson(rsdata.toUtf8(),&jsonParseError));
if(QJsonParseError::NoError !=jsonParseError.error){
emit sendMsg(tr("解析请求响应数据失败:%1").arg(jsonParseError.errorString()));
return;
}
QJsonObject jsonObj = jsonDoc.object();
int newVer = jsonObj.value("version").toInt();
if(newVer<=sysVersion){
//不更新
emit sendMsg(tr("当前版本已经是最新版本"));
return;
}
emit upgradeBtnStatus(1);
GlobalVal::newVersion = newVer;
QString newVerStr = jsonObj.value("versionStr").toString();
emit sendMsg(tr("发现新版本:%1,请点击立即更新按钮更新").arg(newVerStr));
int updateTtype = jsonObj.value("type").toInt();
GlobalVal::updateTtype = updateTtype;
QString zipurl = jsonObj.value("zipurl").toString();
QJsonArray fileList = jsonObj.value("filelist").toArray();
QString mainAppName = jsonObj.value("mainAppName").toString();
GlobalVal::zipurl = zipurl;
GlobalVal::fileList = fileList;
GlobalVal::mainAppName = mainAppName;
}
/**
* 获取本地程序版本号,版本号为纯数字
* @brief CheckVersion::readSysVersion
* @return
*/
int CheckVersion::readSysVersion()
{
QString configFilePath = QDir::currentPath()+"/version.dat";
configFilePath = QDir::toNativeSeparators(configFilePath);
QFile writeFile(configFilePath);
writeFile.open(QIODevice::ReadOnly);
QDataStream in(&writeFile);
int sysVersion;
in >> sysVersion;
writeFile.close();
return sysVersion;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/coldco519/autoUpdater.git
git@gitee.com:coldco519/autoUpdater.git
coldco519
autoUpdater
autoUpdater
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891