master
Mike Solar 2023-07-29 07:20:49 +08:00
parent 926ddcdc63
commit c9db35e414
5 changed files with 378 additions and 32 deletions

View File

@ -5,9 +5,9 @@ set(CMAKE_CXX_STANDARD 11)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_PREFIX_PATH "C:/Qt/Qt5.6.3/5.6.3/mingw49_32")
add_definitions(-DOFFICEASSISTANT_NO_CONSOLE)
add_compile_definitions(UNICODE)
set(CMAKE_PREFIX_PATH "C:/Qt/Qt5.6.3_msvc/5.6.3/msvc2015")
set(CMAKE_EXE_LINKER_FLAGS "/SAFESEH:NO" )
find_package(Qt5 COMPONENTS
Core
Gui

View File

@ -32,7 +32,7 @@ public:
return QSize(width2,height2);
};
}
int setSize(int width,int height,int logicalwidth,int logicalheight){
void setSize(int width,int height,int logicalwidth,int logicalheight){
this->width2=width;
this->height2=height;
this->logicalwidth=logicalwidth;

View File

@ -4,7 +4,7 @@
#ifndef OFFICEASSISTANT_CONFIG_H
#define OFFICEASSISTANT_CONFIG_H
#define VERSION "0.0.0";
#define RELEASE "release";
#define PARTNER_ID "partner"
#define CONFIG_URL "http://softapi.1.y01.cn/addons/Kmdsoft/Index/config"
#define OP_URL "http://softapi.1.y01.cn/addons/Kmdsoft/Index/op"
#define DEVICE_URL "http://softapi.1.y01.cn/addons/Kmdsoft/Index/device"
#endif //OFFICEASSISTANT_CONFIG_H

371
netio.cpp
View File

@ -3,8 +3,10 @@
//
#include <QProcess>
#include <QScreen>
#include <QCryptographicHash>
#include "netio.h"
#include "config.h"
#include <cstdlib>
#include <ctime>
#include <QNetworkAccessManager>
@ -14,30 +16,43 @@
#include <QTimer>
#include <cstring>
#include <QJsonArray>
#include <QSysInfo>
#include <QStorageInfo>
#include <comutil.h>
#include <wbemidl.h>
#include <iostream>
#include <QGuiApplication>
#include <ctime>
#pragma comment(lib, "wbemuuid.lib")
//读取注册表获取MachineUUID
inline QString getMachineGUID(){
HKEY hKey;
RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Cryptography",
RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Cryptography",
0,KEY_READ | KEY_WOW64_64KEY, &hKey);
DWORD dwType1 = REG_SZ;
DWORD dwLen = MAX_PATH;
char buf[100];
WCHAR buf[100];
RegQueryValueExA(hKey, "MachineGuid" ,0 ,&dwType1, (LPBYTE)buf, &dwLen);
QString guid=QString(buf);
QString guid=QString::fromWCharArray(buf);
return guid;
}
RequestBodyBase::RequestBodyBase(QString requestId){
request_id=requestId;
RequestBodyBase::RequestBodyBase(){
wchar_t unix_time[65]={0};
wsprintf(unix_time,L"%ld",std::time(0));
QString request_id=QString::fromWCharArray(unix_time);
//打开配置文件
QFile infFile(".\\config\\information.cfg");
if(!infFile.open(QIODevice::ReadOnly|QIODevice::Text)){
QFile *infFile=new QFile(".\\config\\information.cfg");
if(!infFile->open(QIODevice::ReadOnly|QIODevice::Text)){
QMessageBox::warning(nullptr,"错误","无法打开配置文件");
delete infFile;
exit(1);
}
//读取配置文件
QByteArray bytes;
bytes=infFile.readAll();
bytes=infFile->readAll();
infFile->close();
delete infFile;
//转化位json
qJsonDocument=QJsonDocument::fromJson(bytes);
if(qJsonDocument.isObject()){
@ -105,16 +120,17 @@ RequestBodyBase::RequestBodyBase(QString requestId){
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)){
infFile=new QFile(".\\config\\information.cfg");
if(!infFile->open(QIODevice::WriteOnly|QIODevice::Text)){
//处理错误
QMessageBox::warning(nullptr,"错误","无法覆写配置文件");
infFile->close();
delete infFile;
exit(1);
}
//加入json序列
@ -123,11 +139,12 @@ RequestBodyBase::RequestBodyBase(QString requestId){
obj_root.insert("device_id",value);
qJsonDocument.setObject(obj_root);
//写入配置文件
infFile.write(qJsonDocument.toJson())
infFile->write(qJsonDocument.toJson());
//关闭文件;
infFile.close();
infFile->close();
delete infFile;
}
QJsonValue requestId_json=requestId;
QJsonValue requestId_json=QJsonValue(request_id);
QJsonObject obj_root;
//插入request_id
obj_root.insert("request_id",requestId_json);
@ -136,14 +153,20 @@ RequestBodyBase::RequestBodyBase(QString requestId){
void ConfigRequest::getConfig(ConfigResponse *configResponse) {
void ConfigRequest::sendRequest(ConfigResponse *configResponse) {
QTimer *timer = new QTimer(this);
QNetworkAccessManager *httpMgr = new QNetworkAccessManager();
QString url ="http://softapi.1.y01.cn/addons/Kmdsoft/Index/config";
/* QFile *file=new QFile(".\\config\\config.cfg");
if(!file->open(QIODevice::ReadOnly|QIODevice::Text)){
QMessageBox::warning(nullptr,"错误","无法打开配置文件");
file->close();
delete file;
exit(1);
}*/
QNetworkRequest requestInfo;
//HTTP请求
//请求头
requestInfo.setUrl(QUrl(url));
requestInfo.setUrl(QUrl(CONFIG_URL));
requestInfo.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json"));
//保存响应的变量
QNetworkReply *reply = httpMgr->post(requestInfo,qJsonDocument.toJson());
@ -155,10 +178,10 @@ void ConfigRequest::getConfig(ConfigResponse *configResponse) {
timer->start(5000);
//启动循环
eventLoop.exec();
delete httpMgr;
QJsonDocument result;
configResponse=new ConfigResponse;
memset(configResponse,0,sizeof(configResponse));
memset(configResponse,0,sizeof(*configResponse));
//如果没有错误
if(reply->error() == QNetworkReply::NoError) {
result = QJsonDocument::fromJson(reply->readAll());
@ -193,3 +216,313 @@ void ConfigRequest::getConfig(ConfigResponse *configResponse) {
return;
}
}
void RequestBodyBase::sendRequest() {
QTimer *timer = new QTimer(this);
QNetworkAccessManager *httpMgr = new QNetworkAccessManager();
QNetworkRequest requestInfo;
//HTTP请求
//请求头
requestInfo.setUrl(QUrl(OP_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();
delete timer;
delete httpMgr;
delete reply;
}
DeviceRequest::DeviceRequest() : RequestBodyBase() {
//CPU
QString cpu = QSysInfo::currentCpuArchitecture();
//内存大小
MEMORYSTATUSEX status;
status.dwLength = sizeof(status);
GlobalMemoryStatusEx(&status);
int ram = status.ullTotalPhys / 1024 / 1024;
//硬盘大小
QStorageInfo storage = QStorageInfo::root();
int disk = storage.bytesTotal() / static_cast<qulonglong>(1024 * 1024 * 1024);
//显卡型号
QStringList gpus;
BOOL success;
DWORD deviceIndex = 0;
DISPLAY_DEVICE displayDevice;
displayDevice.cb = sizeof(displayDevice);
while (EnumDisplayDevices(NULL, deviceIndex, &displayDevice, 0)) {
WCHAR valueName[128];
DWORD valueSize;
DWORD valueType;
BYTE valueData[MAX_PATH];
HKEY hKey;
WCHAR keyName[128];
wsprintf(keyName, L"SYSTEM\\CurrentControlSet\\Control\\Video\\%s\\HardwareInformation",
displayDevice.DeviceID);
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, keyName, 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
valueSize = sizeof(valueData);
wsprintf(valueName, L"HardwareInformation.AdapterString");
if (RegQueryValueEx(hKey, valueName, NULL, &valueType, valueData, &valueSize) == ERROR_SUCCESS) {
Q_ASSERT(valueType == REG_SZ);
QString adapterString = QString::fromWCharArray((wchar_t *) valueData, valueSize / sizeof(wchar_t) - 1);
gpus.append(adapterString);
}
RegCloseKey(hKey);
}
deviceIndex++;
displayDevice.cb = sizeof(displayDevice);
}
//主板型号
QString motherboard = QSysInfo::prettyProductName();
//WMI获取网卡型号
HRESULT hr = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hr)) {
std::cerr << "Failed to initialize COM library." << std::endl;
return;
}
QStringList netCards;
hr = CoInitializeSecurity(
NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_DEFAULT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE,
NULL
);
if (FAILED(hr)) {
std::cerr << "Failed to initialize security." << std::endl;
CoUninitialize();
return;
}
IWbemLocator* pLoc = NULL;
hr = CoCreateInstance(
CLSID_WbemLocator,
NULL,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator,
(LPVOID*)&pLoc
);
if (FAILED(hr)) {
std::cerr << "Failed to create IWbemLocator object." << std::endl;
CoUninitialize();
return;
}
IWbemServices* pSvc = NULL;
hr = pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"),
NULL,
NULL,
0,
NULL,
0,
0,
&pSvc
);
if (FAILED(hr)) {
std::cerr << "Failed to connect to WMI service." << std::endl;
pLoc->Release();
CoUninitialize();
return;
}
hr = CoSetProxyBlanket(
pSvc,
RPC_C_AUTHN_WINNT,
RPC_C_AUTHZ_NONE,
NULL,
RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE
);
if (FAILED(hr)) {
std::cerr << "Failed to set security blanket." << std::endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return;
}
IEnumWbemClassObject* pEnumerator = NULL;
hr = pSvc->ExecQuery(
_bstr_t("WQL"),
_bstr_t("SELECT * FROM Win32_NetworkAdapter WHERE PhysicalAdapter = TRUE AND NetConnectionStatus = 3"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator
);
if (FAILED(hr)) {
std::cerr << "Failed to execute WQL query for Win32_NetworkAdapter." << std::endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return;
}
IWbemClassObject* pClassObj = NULL;
ULONG uReturn = 0;
while (pEnumerator) {
hr = pEnumerator->Next(WBEM_INFINITE, 1, &pClassObj, &uReturn);
if (uReturn == 0) {
break;
}
VARIANT vtProp;
hr = pClassObj->Get(L"Caption", 0, &vtProp, 0, 0);
if (SUCCEEDED(hr)) {
QString str=QString::fromWCharArray(vtProp.bstrVal);
VariantClear(&vtProp);
netCards.append(str);
}
pClassObj->Release();
}
//WMI获取声卡型号
hr = pSvc->ExecQuery(
_bstr_t("WQL"),
_bstr_t("SELECT * FROM Win32_SoundDevice"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator
);
if (FAILED(hr)) {
std::cerr << "Failed to execute WQL query for Win32_SoundDevice." << std::endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return;
}
QStringList audioCards;
while (pEnumerator) {
hr = pEnumerator->Next(WBEM_INFINITE, 1, &pClassObj, &uReturn);
if (uReturn == 0) {
break;
}
VARIANT vtProp;
hr = pClassObj->Get(L"Name", 0, &vtProp, 0, 0);
if (SUCCEEDED(hr)) {
QString str=QString::fromWCharArray(vtProp.bstrVal);
VariantClear(&vtProp);
audioCards.append(str);
}
pClassObj->Release();
}
hr = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_PhysicalMemory"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator
);
if (FAILED(hr))
{
std::cout << "Query failed. Error code: " << hr << std::endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return;
}
QStringList RAMModel;
while (pEnumerator)
{
hr = pEnumerator->Next(WBEM_INFINITE, 1, &pClassObj, &uReturn);
if (uReturn == 0)
{
break;
}
VARIANT vtProp;
hr = pClassObj->Get(L"Manufacturer", 0, &vtProp, 0, 0);
if (SUCCEEDED(hr))
{
std::wcout << "Manufacturer: " << vtProp.bstrVal << std::endl;
VariantClear(&vtProp);
}
hr = pClassObj->Get(L"PartNumber", 0, &vtProp, 0, 0);
if (SUCCEEDED(hr))
{
RAMModel.append(QString::fromWCharArray(vtProp.bstrVal));
VariantClear(&vtProp);
}
pClassObj->Release();
}
pSvc->Release();
pLoc->Release();
CoUninitialize();
DISPLAY_DEVICE d = { sizeof(DISPLAY_DEVICE) };
::EnumDisplayDevices(NULL, 0, &d, 0);
QString monitor=QString::fromWCharArray(d.DeviceString, wcslen(d.DeviceString));
//发送
//构造JSON
QJsonDocument *device=new QJsonDocument;
QJsonObject *object=new QJsonObject;
object->insert("CPU",QJsonValue(cpu));
object->insert("RAM",QJsonValue(ram));
object->insert("Hard_Disk",QJsonValue(disk));
QJsonArray *gpu_array=new QJsonArray;
for(auto gpu:gpus) {
gpu_array->append(gpu);
}
object->insert("GPU",*gpu_array);
delete gpu_array;
object->insert("Mother_Board",motherboard);
QJsonArray *RAMModel_json=new QJsonArray;
for(auto RAM:RAMModel){
RAMModel_json->append(RAM);
}
object->insert("Net_Card",*RAMModel_json);
delete RAMModel_json;
QJsonArray *netCards_json=new QJsonArray;
for(auto netCard:netCards){
netCards_json->append(netCard);
}
object->insert("Net_Card",*netCards_json);
delete netCards_json;
QJsonArray *audioCards_json=new QJsonArray;
for(auto audioCard:audioCards){
audioCards_json->append(audioCard);
}
object->insert("Audio_Card",*audioCards_json);
delete audioCards_json;
object->insert("Monitor",monitor);
device->setObject(*object);
delete object;
QJsonObject obj_root=qJsonDocument.object();
obj_root.insert("data",QString(device->toJson()));
delete device;
qJsonDocument.setObject(obj_root);
QTimer *timer = new QTimer(this);
QNetworkAccessManager *httpMgr = new QNetworkAccessManager();
QNetworkRequest requestInfo;
//HTTP请求
//请求头
requestInfo.setUrl(QUrl(DEVICE_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();
delete httpMgr;
}

25
netio.h
View File

@ -36,7 +36,8 @@ typedef struct {
class RequestBodyBase:public QObject{
public:
RequestBodyBase(QString requestId);
RequestBodyBase();
virtual void sendRequest();
protected:
QString product;
QString partner_id;
@ -51,12 +52,19 @@ protected:
};
class ConfigRequest:public RequestBodyBase{
public:
ConfigRequest(QString requestId):RequestBodyBase(requestId){}
void getConfig(ConfigResponse * configResponse);
ConfigRequest():RequestBodyBase(){}
void sendRequest(ConfigResponse * configResponse);
};
class OpRequest:public RequestBodyBase{
public:
OpRequest(QString Op,QString OpValue,QString requestId):RequestBodyBase(requestId),op(Op),op_value(OpValue){}
OpRequest(QString Op,QString OpValue):RequestBodyBase(),op(Op),op_value(OpValue){
QJsonObject obj_root=qJsonDocument.object();
QJsonValue value=op;
obj_root.insert("op",op);
value=op_value;
obj_root.insert("op_value",op_value);
qJsonDocument.setObject(obj_root);
}
QString getOpValue(){
return op_value;
}
@ -69,15 +77,20 @@ public:
void setOp(QString op){
this->op=op;
}
void sendRequest();
protected:
QString op;
QString op_value;
};
class DeviceRequest:RequestBodyBase{
DeviceRequest(QString requestId):RequestBodyBase(requestId){}
DeviceRequest();
};
class OpenMulitiWechat:public RequestBodyBase{
public:
OpenMulitiWechat(): RequestBodyBase(){}
};
#endif //OFFICEASSISTANT_NETIO_H