完成RequestBase构造函数个getConfig()函数

master
Mike Solar 2023-07-28 07:32:36 +08:00
parent b262eca43c
commit 926ddcdc63
6 changed files with 1467 additions and 1 deletions

View File

@ -12,13 +12,15 @@ find_package(Qt5 COMPONENTS
Core Core
Gui Gui
Widgets Widgets
Network
REQUIRED) REQUIRED)
add_executable(officeassistant WIN32 main.cpp mainwindow.cpp mainwindow.h mainwindow.ui mainwindowlayout.cpp mainwindowlayout.h mainwindowlayout.ui navbar.cpp navbar.h navbar.ui MyButton.cpp MyButton.h buttonstruct.h globalvariables.h mainscreen.cpp mainscreen.h mainscreen.ui) add_executable(officeassistant WIN32 main.cpp mainwindow.cpp mainwindow.h mainwindow.ui mainwindowlayout.cpp mainwindowlayout.h mainwindowlayout.ui navbar.cpp navbar.h navbar.ui MyButton.cpp MyButton.h buttonstruct.h globalvariables.h mainscreen.cpp mainscreen.h mainscreen.ui netio.cpp netio.h config.h)
target_link_libraries(officeassistant target_link_libraries(officeassistant
Qt5::Core Qt5::Core
Qt5::Gui Qt5::Gui
Qt5::Widgets Qt5::Widgets
Qt5::Network
) )
if (WIN32 AND NOT DEFINED CMAKE_TOOLCHAIN_FILE) if (WIN32 AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
set(DEBUG_SUFFIX) set(DEBUG_SUFFIX)

10
config.h Normal file
View File

@ -0,0 +1,10 @@
//
// Created by HW on 2023/07/27.
//
#ifndef OFFICEASSISTANT_CONFIG_H
#define OFFICEASSISTANT_CONFIG_H
#define VERSION "0.0.0";
#define RELEASE "release";
#define PARTNER_ID "partner"
#endif //OFFICEASSISTANT_CONFIG_H

1176
mb.h Normal file

File diff suppressed because it is too large Load Diff

BIN
miniblink_4949_x32.dll Normal file

Binary file not shown.

195
netio.cpp Normal file
View File

@ -0,0 +1,195 @@
//
// 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;
}
}

83
netio.h Normal file
View File

@ -0,0 +1,83 @@
//
// Created by HW on 2023/07/27.
//
#ifndef OFFICEASSISTANT_NETIO_H
#define OFFICEASSISTANT_NETIO_H
#include <QString>
#include <QFile>
#include <QMessageBox>
#include <cstring>
#include <QJsonDocument>
#include <QJsonObject>
#include <Windows.h>
typedef struct {
QString img;
QString img_cover;
QString title;
QString func;
QString url;
} Menu;
typedef struct {
bool succeed;
struct {
QString logo_url;
QString device_id;
QString dev_id;
QString token;
QString backgroud_color;
QString title_color;
QString title_cover_color;
}basic;
Menu *menu;
}ConfigResponse;
class RequestBodyBase:public QObject{
public:
RequestBodyBase(QString requestId);
protected:
QString product;
QString partner_id;
QString os;
QString os_version;
QString device_id;
QString request_id;
QString version;
QString release;
QString sign;
QJsonDocument qJsonDocument;
};
class ConfigRequest:public RequestBodyBase{
public:
ConfigRequest(QString requestId):RequestBodyBase(requestId){}
void getConfig(ConfigResponse * configResponse);
};
class OpRequest:public RequestBodyBase{
public:
OpRequest(QString Op,QString OpValue,QString requestId):RequestBodyBase(requestId),op(Op),op_value(OpValue){}
QString getOpValue(){
return op_value;
}
QString getOp(){
return op;
}
void setOpValue(QString op_value){
this->op_value=op_value;
}
void setOp(QString op){
this->op=op;
}
protected:
QString op;
QString op_value;
};
class DeviceRequest:RequestBodyBase{
DeviceRequest(QString requestId):RequestBodyBase(requestId){}
};
#endif //OFFICEASSISTANT_NETIO_H