#include "Widget_HttpSendJson.h" #include #include #include "QThreadHttpClient.h" #include "DataManager.h" #include #include #include #ifdef Q_OS_WIN //#pragma execution_character_set("utf-8") #endif Widget_HttpSendJson::Widget_HttpSendJson(QWidget *parent) : QWidget(parent) { ui.setupUi(this); InitUIData(); // 创建托盘图标和菜单 createTrayIcon(); } Widget_HttpSendJson::~Widget_HttpSendJson() { // 清理资源 if (trayIcon) { trayIcon->hide(); } } void Widget_HttpSendJson::InitUIData() { ui.tableWidget_List->setColumnCount(3); QStringList pTableHeadList; pTableHeadList << G2U("请求类型") << G2U("发送请求") << G2U("收到回复"); ui.tableWidget_List->setHorizontalHeaderLabels(pTableHeadList); //ui.tableWidget_List->setVisible(false); ui.tableWidget_List->setEditTriggers(QAbstractItemView::NoEditTriggers); ui.tableWidget_List->setSelectionBehavior(QAbstractItemView::SelectRows); ui.tableWidget_List->setSelectionMode(QAbstractItemView::SingleSelection); ui.tableWidget_List->setColumnWidth(0, 120); //AddToListData(LogJson("", "", SVR_API_NULL, false)); InitConnect(); } void Widget_HttpSendJson::InitConnect() { bool bConnectError = false; bConnectError = connect(ui.tableWidget_List, SIGNAL(cellClicked(int, int)), this, SLOT(slot_ClickListUpdate(int, int))); bConnectError = connect(ui.pushButton_Clear, SIGNAL(clicked()), this, SLOT(slot_ClearResponseBtn())); bConnectError = connect(ui.pushButton_jsonTest, SIGNAL(clicked()), this, SLOT(slot_JsonTestBtn())); bConnectError = connect(ui.pushButton_sendJson, SIGNAL(clicked()), this, SLOT(slot_SendRequestBtn())); bConnectError = connect(ui.pushButton_Reload, SIGNAL(clicked()), this, SLOT(slot_ReloadListBtn())); bConnectError = connect(ui.pushButton_ShowSend, SIGNAL(clicked()), this, SLOT(slot_ShowRequestBtn())); bConnectError = connect(ui.pushButton_ShowSendEnd, SIGNAL(clicked()), this, SLOT(slot_ShowRequestAndResponseBtn())); bConnectError = connect(ui.pushButton_Test, SIGNAL(clicked()), this, SLOT(slot_ADDTestDataToList())); bConnectError = connect(ui.checkBox, SIGNAL(stateChanged(int)), this, SLOT(slot_CheckState(int))); //pTimerList = new QTimer(); //connect(pTimerList, SIGNAL(timeout()), this, SLOT(slot_TimerUpdateList())); //pTimerList->start(1000); connect(&DataManager::instance(), &DataManager::dataChanged, this, &Widget_HttpSendJson::slot_dataChange); bConnectError = connect(ui.pb_Start, SIGNAL(clicked()), this, SLOT(slot_StartThread())); bConnectError = connect(ui.pb_End, SIGNAL(clicked()), this, SLOT(slot_EndThread())); bConnectError = connect(DataManager::instance().pRunThread, SIGNAL(signal_RunSuccess()), this, SLOT(slot_RunSuccess())); } void Widget_HttpSendJson::UpdateSendEdit(std::string strJson) { ui.textEdit_Send->clear(); ui.textEdit_Send->setPlainText(DataManager::string_To_UTF8(strJson).c_str()); } void Widget_HttpSendJson::UpdateJSONTestEdit(std::string strJson) { ui.lineEdit_Error->clear(); ui.lineEdit_Error->setText(DataManager::string_To_UTF8(strJson).c_str()); } void Widget_HttpSendJson::UpdateHTTPAddress(std::string strAddress) { ui.lineEdit_Address->clear(); ui.lineEdit_Address->setText(DataManager::string_To_UTF8(strAddress).c_str()); } void Widget_HttpSendJson::UpdateResponseEdit(std::string strJson) { ui.textEdit_Response->clear(); ui.textEdit_Response->setPlainText(DataManager::string_To_UTF8(strJson).c_str()); } void Widget_HttpSendJson::AddToListData(LogJson& pJsonData) { int nCount = ui.tableWidget_List->rowCount(); ui.tableWidget_List->insertRow(nCount); ui.tableWidget_List->setItem(nCount, 0, new QTableWidgetItem(pJsonData.m_nAPIID.c_str())); ui.tableWidget_List->setItem(nCount, 1, new QTableWidgetItem(DataManager::string_To_UTF8(pJsonData.m_jsonRequest).c_str())); QString strShowData=""; if (pJsonData.m_bStatus) { strShowData = G2U("发送成功"); } else{ strShowData = G2U("发送失败"); } ui.tableWidget_List->setItem(nCount, 2, new QTableWidgetItem(strShowData)); } void Widget_HttpSendJson::slot_StartThread() { if (!DataManager::instance().pRunThread->isRunning()) { DataManager::instance().pRunThread->SetRunData(1010001,OCC_SYS_DD_SVR,121111221,556); DataManager::instance().pRunThread->SetCMDType(TaskRunThread::server_Run); DataManager::instance().pRunThread->start(); } } void Widget_HttpSendJson::slot_EndThread() { if (!DataManager::instance().pRunThread->isRunning()) { DataManager::instance().pRunThread->SetRunData(1010001, OCC_SYS_DD_SVR, 121111221, 556); DataManager::instance().pRunThread->SetCMDType(TaskRunThread::server_Stop); DataManager::instance().pRunThread->start(); } } void Widget_HttpSendJson::slot_ClearResponseBtn() { ui.textEdit_Response->clear(); } void Widget_HttpSendJson::slot_SendRequestBtn() { QString strJson=ui.textEdit_Send->toPlainText(); QString strAddress = ui.lineEdit_Address->text(); std::string strResponse=""; Json::Reader reader; Json::Value root; if (!reader.parse(strJson.toStdString(), root)) { QString StrMessage = G2U("json解析失败是否继续发送HTTP"); QMessageBox::StandardButton confimBtn = QMessageBox::question(this, "错误", StrMessage, QMessageBox::Ok | QMessageBox::Cancel); if (confimBtn == QMessageBox::Cancel) { return; } } bool bReturn = QThreadHttpClient::instance().HttpPost(strAddress.toStdString(),strJson.toStdString(), strResponse); std::string strError=""; if (bReturn) { strError = DataManager::UTF8_to_string("HTTP发送成功"); } else { strError = DataManager::UTF8_to_string("HTTP发送失败"); } UpdateJSONTestEdit(strError); UpdateResponseEdit(strResponse); } void Widget_HttpSendJson::slot_ReloadListBtn() { ui.tableWidget_List->clearContents(); ui.tableWidget_List->setRowCount(0); QList* LogJson = DataManager::instance().GetLogJson(); LogJson->clear(); } void Widget_HttpSendJson::slot_ClickListUpdate(int nRow, int nColumn) { m_nSelectRow = nRow; QList* LogJson = DataManager::instance().GetLogJson(); if (nRow>=0&&!LogJson->empty()) { m_pSelectData = &(*LogJson)[nRow]; } else { m_pSelectData = NULL; } } void Widget_HttpSendJson::slot_ShowRequestBtn() { QString strShow = ui.textEdit_Send->toPlainText(); if (!strShow.isEmpty()) { QString strShow = G2U("确定清除Json条目吗?"); QMessageBox::StandardButton confimBtn = QMessageBox::question(this, G2U("提示"), strShow, QMessageBox::Ok | QMessageBox::Cancel); if (confimBtn == QMessageBox::Cancel) { return; } else { if (m_pSelectData) { UpdateSendEdit(m_pSelectData->m_jsonRequest); UpdateHTTPAddress(m_pSelectData->m_nAPIID); } } } else if (m_nSelectRow >= 0) { if (m_pSelectData) { UpdateSendEdit(m_pSelectData->m_jsonRequest); UpdateHTTPAddress(m_pSelectData->m_nAPIID); } } } void Widget_HttpSendJson::slot_ShowRequestAndResponseBtn() { QString strShow = ui.textEdit_Send->toPlainText(); if (!strShow.isEmpty()) { QString strShow = G2U("确定清除Json条目吗?"); QMessageBox::StandardButton confimBtn = QMessageBox::question(this, G2U("提示"), strShow, QMessageBox::Ok | QMessageBox::Cancel); if (confimBtn == QMessageBox::Cancel) { return; } else { if (m_pSelectData) { UpdateSendEdit(m_pSelectData->m_jsonRequest); UpdateHTTPAddress(m_pSelectData->m_nAPIID); UpdateResponseEdit(m_pSelectData->m_jsonResponse); } } } else if (m_nSelectRow>=0) { if (m_pSelectData) { UpdateSendEdit(m_pSelectData->m_jsonRequest); UpdateHTTPAddress(m_pSelectData->m_nAPIID); UpdateResponseEdit(m_pSelectData->m_jsonResponse); } } } void Widget_HttpSendJson::slot_JsonTestBtn() { Json::Reader reader; QString strEdit = ui.textEdit_Send->toPlainText(); std::string strJson = strEdit.toStdString(); Json::Value root; std::string strError=""; if (reader.parse(strJson, root)) { strError = DataManager::UTF8_to_string("JSON结构正常,读取正常"); } else { std::string strMSG=reader.getFormattedErrorMessages(); strError = strMSG; } UpdateJSONTestEdit(strError); } void Widget_HttpSendJson::slot_ADDTestDataToList() { QString strAddress="/courserun/eqpmtstatus"; QString strJson=""; std::string strResponse=""; Json::Reader reader; Json::FastWriter wJson; Json::Value rootEqpmt; rootEqpmt["SubSys"]=1001001; rootEqpmt["CtrlType"]=0; rootEqpmt["Status"]=0; rootEqpmt["Category"]=0; rootEqpmt["LineID"]=1001; rootEqpmt["StationID"]=0; rootEqpmt["ViewID"]=0; rootEqpmt["EqpmtID"]=""; rootEqpmt["CtrlLock"]=0; rootEqpmt["FaultLock"]=0; rootEqpmt["RemoteLock"]=0; rootEqpmt["CommunicationStaus"]=0; rootEqpmt["CtrlArea"]=0; strJson=QString::fromStdString(wJson.write(rootEqpmt)); bool bReturn = QThreadHttpClient::instance().HttpPost(strAddress.toStdString(),strJson.toStdString(), strResponse); std::string strError=""; strAddress="/courserun/add_ctrl_log"; Json::Value root; root["CtrlType"]=0; root["CtrlFrom"]=0; root["CtrlUser"]=""; root["Param1"]=""; root["Param2"]=""; root["Param3"]=""; root["Param4"]=""; root["Param5"]=""; strJson =QString::fromStdString(wJson.write(root)); bReturn = QThreadHttpClient::instance().HttpPost(strAddress.toStdString(),strJson.toStdString(), strResponse); } void Widget_HttpSendJson::slot_TimerUpdateList() { ui.tableWidget_List->clearContents(); ui.tableWidget_List->setRowCount(0); //xhNetWork::InStance()->MutexLogJson.lock(); QList* LogJson = DataManager::instance().GetLogJson(); for (auto iters = LogJson->begin(); iters != LogJson->end();iters++) { AddToListData(*iters); } //xhNetWork::InStance()->MutexLogJson.unlock(); ui.tableWidget_List->selectRow(m_nSelectRow); } void Widget_HttpSendJson::slot_CheckState(int nState) { if (nState==Qt::Checked) { hideHeart = false; } else { hideHeart = true; } } void Widget_HttpSendJson::slot_dataChange(int nDataType) { slot_TimerUpdateList(); } void Widget_HttpSendJson::slot_RunSuccess() { //QMessageBox::information(this, "提示", "执行成功"); string strData = ""; switch (TaskMNG::instance()->getTaskStatus()) { case TaskMNG::TaskStatus::Status_Run: { strData = G2U("课程开启").toStdString(); DataManager::instance().setEqpmtStatus(1001,2); } break; case TaskMNG::TaskStatus::Status_Null: { strData = G2U("课程结束").toStdString(); DataManager::instance().setEqpmtStatus(1001, 1); } break; default: break; }; strData.append(G2U("执行成功").toStdString()); LogJson pJsonData(G2U("执行成功").toStdString(),"--", strData,true); AddToListData(pJsonData); } void Widget_HttpSendJson::closeEvent(QCloseEvent* event) { // 当用户点击窗口的关闭按钮时,询问用户确认退出 QMessageBox::StandardButton reply; reply = QMessageBox::question(this, G2U("确认退出"), G2U("确定要退出程序吗?"), QMessageBox::Yes | QMessageBox::No); if (reply == QMessageBox::Yes) { // 用户确认退出,隐藏托盘图标并退出程序 if (trayIcon) { trayIcon->hide(); } event->accept(); } else { // 用户取消退出,隐藏窗口到托盘而不是退出 this->hide(); event->ignore(); } } void Widget_HttpSendJson::onTrayIconActivated(QSystemTrayIcon::ActivationReason reason) { switch (reason) { case QSystemTrayIcon::DoubleClick: // 双击托盘图标 onShowWindowTriggered(); break; case QSystemTrayIcon::Trigger: // 单击托盘图标(某些系统) // 可以根据需要处理单击事件 break; case QSystemTrayIcon::MiddleClick: // 中键点击 // 可以添加中键点击的功能 break; default: break; } } void Widget_HttpSendJson::onShowWindowTriggered() { // 显示窗口 this->show(); // 恢复正常状态(如果是最小化状态) this->showNormal(); // 激活窗口并将其置于前台 this->activateWindow(); this->raise(); } void Widget_HttpSendJson::onHideWindowTriggered() { // 隐藏窗口到托盘 this->hide(); // 可选:显示提示消息 // trayIcon->showMessage(tr("程序已隐藏"), // tr("程序已隐藏到系统托盘继续运行"), // QSystemTrayIcon::Information, 1500); } void Widget_HttpSendJson::onQuitTriggered() { // 退出程序 QMessageBox::StandardButton reply; reply = QMessageBox::question(this, G2U("确认退出"), G2U("确定要退出程序吗?"), QMessageBox::Yes | QMessageBox::No); if (reply == QMessageBox::Yes) { // 隐藏托盘图标 if (trayIcon) { trayIcon->hide(); } // 退出应用程序 qApp->quit(); } } void Widget_HttpSendJson::createTrayIcon() { // 创建系统托盘图标 trayIcon = new QSystemTrayIcon(this); // 设置托盘图标 - 请替换为你的图标路径 // 可以使用系统图标作为备选 QIcon icon = QIcon(QPixmap(QStringLiteral(":/new/prefix1/ElcIcon.png"))); if (icon.isNull()) { // 如果自定义图标加载失败,使用系统标准图标 icon = QApplication::style()->standardIcon(QStyle::SP_ComputerIcon); } trayIcon->setIcon(icon); // 设置鼠标悬停提示 trayIcon->setToolTip(G2U("电调服务")); // 设置托盘菜单 setupTrayMenu(); // 连接托盘图标的激活信号(单击、双击等) connect(trayIcon, &QSystemTrayIcon::activated, this, &Widget_HttpSendJson::onTrayIconActivated); // 显示托盘图标 trayIcon->show(); } void Widget_HttpSendJson::setupTrayMenu() { // 创建托盘菜单 trayMenu = new QMenu(this); // 创建菜单动作 showWindowAction = new QAction(G2U("显示窗口"), this); hideWindowAction = new QAction(G2U("隐藏窗口"), this); quitAction = new QAction(G2U("退出程序"), this); // 设置动作图标(可选) showWindowAction->setIcon(QApplication::style()->standardIcon(QStyle::SP_TitleBarNormalButton)); hideWindowAction->setIcon(QApplication::style()->standardIcon(QStyle::SP_TitleBarMinButton)); quitAction->setIcon(QApplication::style()->standardIcon(QStyle::SP_TitleBarCloseButton)); // 连接动作信号到槽函数 connect(showWindowAction, &QAction::triggered, this, &Widget_HttpSendJson::onShowWindowTriggered); connect(hideWindowAction, &QAction::triggered, this, &Widget_HttpSendJson::onHideWindowTriggered); connect(quitAction, &QAction::triggered, this, &Widget_HttpSendJson::onQuitTriggered); // 将动作添加到菜单 trayMenu->addAction(showWindowAction); trayMenu->addAction(hideWindowAction); trayMenu->addSeparator(); // 添加分隔线 trayMenu->addAction(quitAction); // 设置托盘图标的上下文菜单 trayIcon->setContextMenu(trayMenu); }