123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- #include "home.h"
- #include "ui_home.h"
- #include <QDateTime>
- #include <QMessageBox>
- #include <QScreen>
- #include <QTimer>
- #include <QDebug>
- #include <QNetworkRequest>
- #include <QHttpMultiPart>
- #include <QNetworkAccessManager>
- #include <QFile>
- #include <QProcess>
- #include <QVBoxLayout>
- //#include <QtWin>
- //#include <QtWinExtras/qwinfunctions.h>
- #pragma comment(lib, "user32.lib")
- #include "httpapi.h"
- #include "qresource.h"
- //#include <windows.h>
- //#include <tlhelp32.h>
- #include <stdio.h>
- #pragma execution_character_set("utf-8")
- Home::Home(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::Home)
- {
- ui->setupUi(this);
- myTimer = new QTimer(this);
- //start timer for screen capture
- connect(myTimer,&QTimer::timeout,[this](){
- captureScreen();
- });
- myTimer->start(60000);
- setWindowTitle(tr("智能工时管家"));
- //set ui data
- webWidget = new QWebEngineView(this);
- QNetworkProxyFactory::setUseSystemConfiguration(false);
- QVBoxLayout *mainLayout = new QVBoxLayout;
- mainLayout->addWidget(webWidget);
- // setLayout(mainLayout);
- QWidget* widget = new QWidget(this);
- widget->setLayout(mainLayout);
- this->setCentralWidget(widget);
- setWindowState(Qt::WindowMaximized);//最大化
- QSettings *qs = new QSettings("MyManager",QSettings::IniFormat,this);
- qs->beginGroup("user");
- QString id = qs->value("id").toString();
- qDebug() << "id=="<<id<<", phone="<<qs->value("name").toString();
- QString str = HOME_PAGE_URL + id;
- loadNavigate(str);
- //首次登陆时立即截图
- captureScreen();
- }
- void Home::captureScreen() {
- QScreen *screen = QGuiApplication::primaryScreen();
- QString filePathName = "timeManager_screencap.jpg";
- QFile oldFile(filePathName);
- if (oldFile.exists()) {
- QImage oldImg(filePathName);
- uchar* data = oldImg.bits();
- int height = oldImg.height();
- int width = oldImg.width();
- bool result = screen->grabWindow(0).save(filePathName, "jpg"); // 0值为整个电脑屏幕WId
- if (!result) {
- QMessageBox msg3(QMessageBox::Information,windowTitle(),"请尝试使用管理员运行程序!",QMessageBox::Ok,this);
- msg3.exec();
- }
- //判断图像是否和之前的那个一样
- QImage curImg = QImage(filePathName);
- int curHeight = curImg.height();
- int curWidth = curImg.width();
- uchar* curData = curImg.bits();
- int similarPercent = 0;
- if (height == curHeight && width == curWidth) {//必须先满足尺寸一致
- unsigned char r,g,b,curR, curG, curB;
- int totalPixel = 0;
- int samePixel = 0;
- for (int i=0;i<height;i++)
- {
- for (int j=0;j<width;j++)
- {
- totalPixel++;
- r = *(data+2);
- g = *(data+1);
- b = *data;
- //判断像素点的值, 一致的数量
- curR = *(curData+2);
- curG = *(curData+1);
- curB = *curData;
- if (r == curR && g == curG && b == curB) {
- samePixel++;
- }
- data+=4;
- curData +=4;
- }
- }
- similarPercent = samePixel *100/totalPixel;
- qDebug() <<"similar percent = "<<similarPercent<<"%";
- } else {
- similarPercent = 0;
- }
- QString qs = QString::number(similarPercent);
- if (similarPercent >= 95) {//相似度90%以上,认为是一样的,不要上传
- qDebug() << "不上传!!!";
- return;
- }
- } else {
- //直接保存
- screen->grabWindow(0).save(filePathName, "jpg");
- }
- //start upload file
- QNetworkRequest request;
- request.setUrl(QUrl(SERVER_URL + "/imageProcessing/saveAndProcessImage"));
- QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
- QHttpPart imagePart;
- imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));//如果是png图片填image/png
- imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\"; filename=\"image.jpg\""));
- // imagePart.setRawHeader("Content-ID", "my@content.id"); // 添加任何你喜欢的 headers
- QFile *file = new QFile(filePathName);
- file->open(QIODevice::ReadOnly);
- imagePart.setBodyDevice(file);
- multiPart->append(imagePart);
- //text paramter
- QHttpPart textPart;
- textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"uid\""));
- QSettings *qs = new QSettings("MyManager",QSettings::IniFormat,this);
- qs->beginGroup("user");
- QString id = qs->value("id").toString();
- textPart.setBody(id.toUtf8());
- multiPart->append(textPart);
- QHttpPart textPart2;
- textPart2.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"indate\""));
- QString indate = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
- textPart2.setBody(indate.toUtf8());
- multiPart->append(textPart2);
- // QString processList = getProcessList();
- // qDebug() << processList;
- // QHttpPart textPart3;
- // textPart3.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"processList\""));
- // textPart3.setBody(processList.toUtf8());
- // multiPart->append(textPart3);
- //add os infomation
- QString osType = "";
- #if defined(Q_OS_WIN)
- osType = "Q_OS_WIN";
- #endif
- #if defined(Q_OS_MAC)
- osType = "Q_OS_MAC";
- #endif
- #if defined(Q_OS_LINUX)
- osType = "Q_OS_LINUX";
- #endif
- qDebug() << "osType=="<<osType;
- QHttpPart textPart4;
- textPart4.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"osType\""));
- textPart4.setBody(osType.toUtf8());
- multiPart->append(textPart4);
- QNetworkAccessManager *m_pNetWorkManager = new QNetworkAccessManager();
- m_pNetWorkManager->post(request, multiPart);
- }
- QString Home::getProcessList() {
- QProcess process;
- #ifdef Q_OS_WIN
- qDebug() <<"This is Windows platform";
- process.start("tasklist.exe");
- #endif
- #ifdef Q_OS_MAC
- qDebug() <<"This is Mac platform, run ps aux";
- process.start("ps aux");
- #endif
- #ifdef Q_OS_LINUX
- qDebug() <<"This is Linux platform, run ps aux";
- process.start("ps aux");
- #endif
- if(process.waitForFinished())
- {
- QByteArray result = process.readAll();
- return result;
- }
- return NULL;
- }
- void Home::loadNavigate(QString sUrl)
- {
- webWidget->load(sUrl);
- }
- Home::~Home()
- {
- delete webWidget;
- delete ui;
- }
- void Home::closeEvent ( QCloseEvent * e )
- {
- if(QMessageBox::question(this,
- tr("退出"),
- tr("退出软件将无法统计到您的工时,影响绩效,您确定要退出吗?"),
- QMessageBox::Yes, QMessageBox::No )
- == QMessageBox::Yes){
- e->accept();//不会将事件传递给组件的父组件
- qDebug()<<"ok";
- }
- else
- e->ignore();
- }
|