loginwindow.cpp 9.7 KB

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