loginwindow.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #include "loginwindow.h"
  2. #include "ui_loginwindow.h"
  3. #include "../backendapimanager.h"
  4. LoginWindow::LoginWindow(QWidget *parent) : QWidget(parent),
  5. ui(new Ui::LoginWindow),
  6. manager(new QNetworkAccessManager(this))
  7. {
  8. ui->setupUi(this);
  9. registryManager = new RegistryManager();
  10. ui->username->lineEdit()->setPlaceholderText("请输入用户名");
  11. QMovie *movie = new QMovie(this);
  12. movie->setFileName(":/icons/icons/hxgc.gif");
  13. movie->start();
  14. ui->label->setMovie(movie);
  15. setWindowFlags(Qt::FramelessWindowHint);
  16. initUesrCombox();
  17. }
  18. void LoginWindow::initUesrCombox()
  19. {
  20. QStringList allUsernames = registryManager->getAllUsernames();
  21. for (const QString &username : allUsernames)
  22. {
  23. ui->username->addItem(username);
  24. }
  25. ui->username->setCurrentIndex(-1);
  26. }
  27. LoginWindow::~LoginWindow()
  28. {
  29. delete ui;
  30. }
  31. void LoginWindow::on_btnLogin_clicked()
  32. {
  33. QString username = ui->username->currentText();
  34. QString password = ui->password->text();
  35. if (username.isEmpty() || password.isEmpty())
  36. {
  37. QMessageBox::critical(nullptr, "输入错误", "用户名或密码不能为空,请重新输入。");
  38. return;
  39. }
  40. try
  41. {
  42. // 构造请求数据
  43. QJsonObject jsonData;
  44. jsonData["username"] = username;
  45. jsonData["password"] = password;
  46. QJsonDocument doc(jsonData);
  47. QByteArray data = doc.toJson();
  48. QUrl localUrl("login/pc");
  49. QUrl fullUrl = g_url.resolved(localUrl);
  50. QNetworkRequest request(fullUrl); // 替换为实际的服务器接口地址
  51. request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
  52. // 发送 POST 请求
  53. QNetworkReply *reply = manager->post(request, data);
  54. // 处理响应
  55. QObject::connect(reply, &QNetworkReply::finished, [reply, this, username, password]()
  56. {
  57. try {
  58. if (reply->error() == QNetworkReply::NoError) {
  59. QByteArray responseData = reply->readAll();
  60. QJsonDocument responseDoc = QJsonDocument::fromJson(responseData);
  61. if (!responseDoc.isNull() && responseDoc.isObject()) {
  62. QJsonObject responseObj = responseDoc.object();
  63. if (responseObj.contains("code") && responseObj["code"].isDouble()) {
  64. int code = responseObj["code"].toInt();
  65. if (code == 500) {
  66. QMessageBox *msgBox = new QMessageBox(this);
  67. msgBox->setAttribute(Qt::WA_TranslucentBackground, true);
  68. msgBox->setWindowTitle("");
  69. msgBox->setText("用户名或密码错误,请重试。");
  70. msgBox->setWindowFlags(msgBox->windowFlags() | Qt::FramelessWindowHint);
  71. msgBox->setStandardButtons(QMessageBox::NoButton); // 不设置标准按钮
  72. msgBox->setStyleSheet(
  73. "QMessageBox {"
  74. " border: none; /* 去除边框 */"
  75. "}"
  76. "QLabel {"
  77. " color: #a94442; /* 标签文字颜色 */"
  78. " background-color: transparent; /* 标签背景透明 */"
  79. " font-size: 20px; /* 设置字体大小,可根据需要调整 */"
  80. "}"
  81. "QPushButton {"
  82. " visibility: hidden; /* 隐藏按钮 */"
  83. "}"
  84. );
  85. // 使用 QTimer 在 3 秒后自动关闭消息框
  86. QTimer::singleShot(1500, msgBox, [msgBox]() {
  87. msgBox->close();
  88. delete msgBox; // 释放内存
  89. });
  90. msgBox->show();
  91. }
  92. else if (code == 200) {
  93. QJsonObject userInfoObj = responseObj["userInfo"].toObject();
  94. QJsonObject userObj = userInfoObj["user"].toObject();
  95. int userId = userObj["userId"].toInt();
  96. QString identity = userObj["identity"].toString();
  97. QString userIdStr = QString::number(userId);
  98. registryManager->saveUserInfo(userIdStr,username,password,identity);
  99. QString Authority = responseObj["currentAuthority"].toString();
  100. globalAuthority = "Bearer " + Authority;
  101. this->close();
  102. MainWindow *mainWindow = new MainWindow();
  103. backendAPIManager::setAuthToken(globalAuthority);
  104. /*TODO: delete
  105. QString jsonString = "{\"app_version\":\"1.52\",\"blast_latitude\":\"30.21122186731856\",\"blast_longitude\":\"120.22146062951883\",\"blast_time\":\"2025-06-03 12:00:00\",\"company_code\":\"3701234300003\",\"equipment_sn\":\"F34A0000001\",\"error_deto_count\":\"0\",\"operator_identity\":\"330781198509079292\",\"operator_name\":\"栋工\",\"phone\":\"18611112222\",\"project_htid\":\"\",\"project_name\":\"sidf\",\"project_xmbh\":\"\",\"reg_deto_count\":\"2\",\"regs\":[{\"after_test_bus_i\":\"41\",\"after_test_bus_v\":\"8006\",\"before_blasting_i\":\"49\",\"before_blasting_v\":\"13492\",\"bus_leakage_current_i\":\"0\",\"dets\":[{\"delay_time\":\"0.0\",\"freq\":\"0\",\"in_code\":\"005AC8360A4C01A7\",\"out_code\":\"2411104F18000\",\"status\":\"0x00\",\"tag\":\"1-1-1\",\"uid\":\"24211104F18000\"},{\"delay_time\":\"80.0\",\"freq\":\"0\",\"in_code\":\"015AC8360A4C014E\",\"out_code\":\"2411104F18001\",\"status\":\"0x00\",\"tag\":\"1-2-1\",\"uid\":\"24211104F18001\"}],\"equipment_sn\":\"null\",\"equipment_version\":\"null\",\"error_deto_count\":\"0\",\"net_charged_i\":\"49\",\"net_charged_v\":\"13501\",\"reg_deto_count\":\"2\"}]}";
  106. // 使用 QJsonDocument::fromJson 直接解析 QByteArray 并获取 QJsonObject
  107. // 这是一个非常简洁的单行操作,假设你知道顶层是 JSON 对象且不关心详细的错误信息
  108. QJsonObject myJsonObject = QJsonDocument::fromJson(jsonString.toUtf8()).object();
  109. qDebug() << "debug" << QDateTime::fromString(myJsonObject["blast_time"].toString(), "yyyy-MM-dd hh:mm:ss").toString(Qt::ISODateWithMs);
  110. int row = 1;
  111. firingWidget *wt = new firingWidget(row, false, "111");
  112. wt->testonBlastSucess(myJsonObject);
  113. //<< Delete
  114. */
  115. QScreen *screen = QGuiApplication::primaryScreen();
  116. QRect screenGeometry = screen->geometry();
  117. int screenWidth = screenGeometry.width();
  118. int screenHeight = screenGeometry.height();
  119. mainWindow->resize(screenWidth * 1, screenHeight * 0.95);
  120. // 计算窗口居中时的左上角坐标
  121. int windowWidth = mainWindow->width();
  122. int windowHeight = mainWindow->height();
  123. int x = (screenWidth - windowWidth) / 2;
  124. int y = (screenHeight - windowHeight) / 2;
  125. // 移动窗口到居中位置
  126. mainWindow->move(x, y-10);
  127. mainWindow->show();
  128. }
  129. }
  130. }
  131. } else {
  132. QMessageBox::critical(nullptr, "网络请求错误",
  133. QString("错误信息: %1\n错误代码: %2").arg(reply->errorString()).arg(reply->error()));
  134. }
  135. } catch (const std::exception& e) {
  136. qDebug() << "Exception in response handling: " << e.what();
  137. }
  138. reply->deleteLater(); });
  139. }
  140. catch (const std::exception &e)
  141. {
  142. qDebug() << "Exception in request sending: " << e.what();
  143. }
  144. }
  145. void LoginWindow::mousePressEvent(QMouseEvent *event)
  146. {
  147. if (event->button() == Qt::LeftButton)
  148. {
  149. m_dragPosition = event->globalPos() - frameGeometry().topLeft();
  150. event->accept();
  151. }
  152. }
  153. void LoginWindow::mouseMoveEvent(QMouseEvent *event)
  154. {
  155. if (event->buttons() & Qt::LeftButton)
  156. {
  157. move(event->globalPos() - m_dragPosition);
  158. event->accept();
  159. }
  160. }
  161. void LoginWindow::on_btnClose_clicked()
  162. {
  163. this->close();
  164. }
  165. void LoginWindow::on_btnMin_clicked()
  166. {
  167. this->showMinimized();
  168. }
  169. void LoginWindow::on_username_activated(int index)
  170. {
  171. qDebug() << index;
  172. }
  173. void LoginWindow::on_username_currentIndexChanged(int index)
  174. {
  175. QString name = ui->username->currentText();
  176. QString pass = registryManager->getPasswordByUsername(name);
  177. ui->password->setText(pass);
  178. }