196 lines
6.7 KiB
C++
196 lines
6.7 KiB
C++
//
|
||
// Created by HW on 2023/07/27.
|
||
//
|
||
|
||
#include <QProcess>
|
||
#include <QCryptographicHash>
|
||
#include "netio.h"
|
||
#include <cstdlib>
|
||
#include <ctime>
|
||
#include <QNetworkAccessManager>
|
||
#include <QNetworkRequest>
|
||
#include <QEventLoop>
|
||
#include <QNetworkReply>
|
||
#include <QTimer>
|
||
#include <cstring>
|
||
#include <QJsonArray>
|
||
//读取注册表获取MachineUUID
|
||
inline QString getMachineGUID(){
|
||
HKEY hKey;
|
||
RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Cryptography",
|
||
0,KEY_READ | KEY_WOW64_64KEY, &hKey);
|
||
DWORD dwType1 = REG_SZ;
|
||
DWORD dwLen = MAX_PATH;
|
||
char buf[100];
|
||
RegQueryValueExA(hKey, "MachineGuid" ,0 ,&dwType1, (LPBYTE)buf, &dwLen);
|
||
QString guid=QString(buf);
|
||
return guid;
|
||
}
|
||
|
||
RequestBodyBase::RequestBodyBase(QString requestId){
|
||
request_id=requestId;
|
||
//打开配置文件
|
||
QFile infFile(".\\config\\information.cfg");
|
||
if(!infFile.open(QIODevice::ReadOnly|QIODevice::Text)){
|
||
QMessageBox::warning(nullptr,"错误","无法打开配置文件");
|
||
exit(1);
|
||
}
|
||
//读取配置文件
|
||
QByteArray bytes;
|
||
bytes=infFile.readAll();
|
||
//转化位json
|
||
qJsonDocument=QJsonDocument::fromJson(bytes);
|
||
if(qJsonDocument.isObject()){
|
||
//读取数据,写入对应字段
|
||
QJsonObject obj_root=qJsonDocument.object();
|
||
if(obj_root.value("product")==QJsonValue::Undefined){
|
||
QMessageBox::warning(nullptr,"错误","配置文件损坏");
|
||
exit(1);
|
||
}
|
||
product=obj_root.value("product").toString();
|
||
if(obj_root.value("partner_id")==QJsonValue::Undefined){
|
||
QMessageBox::warning(nullptr,"错误","配置文件损坏");
|
||
exit(1);
|
||
}
|
||
partner_id=obj_root.value("partner_id").toString();
|
||
if(obj_root.value("release")==QJsonValue::Undefined){
|
||
QMessageBox::warning(nullptr,"错误","配置文件损坏");
|
||
exit(1);
|
||
}
|
||
release=obj_root.value("release").toString();
|
||
if(obj_root.value("version")==QJsonValue::Undefined){
|
||
QMessageBox::warning(nullptr,"错误","配置文件损坏");
|
||
exit(1);
|
||
}
|
||
version=obj_root.value("version").toString();
|
||
if(obj_root.value("device_id")==QJsonValue::Undefined){
|
||
QMessageBox::warning(nullptr,"错误","配置文件损坏");
|
||
exit(1);
|
||
}
|
||
device_id=obj_root.value("device_id").toString();
|
||
}else{
|
||
//处理错误
|
||
QMessageBox::warning(nullptr,"错误","配置文件损坏");
|
||
exit(1);
|
||
}
|
||
//获取操作系统版本
|
||
os="Windows";
|
||
OSVERSIONINFOEX os;
|
||
os.dwOSVersionInfoSize=sizeof(os);
|
||
GetVersionEx((OSVERSIONINFO *)&os);
|
||
switch(os.dwMajorVersion){//主版本号
|
||
case 5: //不兼容Windows 2000,因此5.x一定是Windows XP
|
||
os_version="Windows XP";
|
||
break;
|
||
case 6:
|
||
switch(os.dwMinorVersion){ //次版本号
|
||
case 0:
|
||
os_version="Windows Vista or Windows Server 2008";
|
||
break;
|
||
case 1:
|
||
os_version="Windows 7 or Windows Server 2008 R2";
|
||
break;
|
||
case 2:
|
||
os_version="Windows 8 or Windows Server 2012";
|
||
break;
|
||
case 3:
|
||
os_version="Windows 8.1 or Windows Server 2012 R2";
|
||
break;
|
||
default:
|
||
os_version="Unknown";
|
||
}
|
||
case 10: //这几个系统都是10.0
|
||
os_version="Windows 10, Windows 11, Windows Server 2016 or Windows Server 2019";
|
||
break;
|
||
default:
|
||
os_version="Unknown";
|
||
}
|
||
//关闭文件
|
||
infFile.close();
|
||
//如果device_id是空值
|
||
if(device_id.isEmpty()){
|
||
//读取MachineGUID并取MD5作为device_id
|
||
QByteArray hash = QCryptographicHash::hash(getMachineGUID().toUtf8(), QCryptographicHash::Md5);
|
||
device_id=hash.toHex();
|
||
if(!infFile.open(QIODevice::WriteOnly|QIODevice::Text)){
|
||
//处理错误
|
||
QMessageBox::warning(nullptr,"错误","无法覆写配置文件");
|
||
exit(1);
|
||
}
|
||
//加入json序列
|
||
QJsonValue value=device_id;
|
||
QJsonObject obj_root;
|
||
obj_root.insert("device_id",value);
|
||
qJsonDocument.setObject(obj_root);
|
||
//写入配置文件
|
||
infFile.write(qJsonDocument.toJson())
|
||
//关闭文件;
|
||
infFile.close();
|
||
}
|
||
QJsonValue requestId_json=requestId;
|
||
QJsonObject obj_root;
|
||
//插入request_id
|
||
obj_root.insert("request_id",requestId_json);
|
||
qJsonDocument.setObject(obj_root);
|
||
}
|
||
|
||
|
||
|
||
void ConfigRequest::getConfig(ConfigResponse *configResponse) {
|
||
QTimer *timer = new QTimer(this);
|
||
QNetworkAccessManager *httpMgr = new QNetworkAccessManager();
|
||
QString url ="http://softapi.1.y01.cn/addons/Kmdsoft/Index/config";
|
||
QNetworkRequest requestInfo;
|
||
//HTTP请求
|
||
//请求头
|
||
requestInfo.setUrl(QUrl(url));
|
||
requestInfo.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json"));
|
||
//保存响应的变量
|
||
QNetworkReply *reply = httpMgr->post(requestInfo,qJsonDocument.toJson());
|
||
//开启一个循环,直到超时或者获取到数据为止
|
||
QEventLoop eventLoop;
|
||
connect(reply,&QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
|
||
//设置定时器防止超时
|
||
connect(timer,&QTimer::timeout,&eventLoop,&QEventLoop::quit);
|
||
timer->start(5000);
|
||
//启动循环
|
||
eventLoop.exec();
|
||
|
||
QJsonDocument result;
|
||
configResponse=new ConfigResponse;
|
||
memset(configResponse,0,sizeof(configResponse));
|
||
//如果没有错误
|
||
if(reply->error() == QNetworkReply::NoError) {
|
||
result = QJsonDocument::fromJson(reply->readAll());
|
||
}else{
|
||
//如果有错误
|
||
configResponse->succeed=false;
|
||
delete reply;
|
||
delete timer;
|
||
return;
|
||
}
|
||
//如果数据完整
|
||
if(result.isObject()){
|
||
QJsonObject obj_root=result.object();
|
||
QJsonArray array;
|
||
array = obj_root.value("menu").toArray();
|
||
configResponse->menu=new Menu[array.count()];
|
||
auto i=0;
|
||
for(auto value:array){
|
||
QJsonObject object=value.toObject();
|
||
configResponse->menu[i].img=object.value("img").toString();
|
||
configResponse->menu[i].img_cover=object.value("img_cover").toString();
|
||
configResponse->menu[i].title=object.value("title").toString();
|
||
configResponse->menu[i].func=object.value("func").toString();
|
||
configResponse->menu[i].url=object.value("url").toString();
|
||
i++;
|
||
}
|
||
}else{
|
||
//数据不完整
|
||
configResponse->succeed=false;
|
||
delete reply;
|
||
delete timer;
|
||
return;
|
||
}
|
||
}
|