loginwindow.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. this->ui->btnLogin->setStyleSheet(
  33. "QPushButton#btnLogin {"
  34. " background-color: rgb(33, 46, 224);"
  35. " color: white;"
  36. " border-radius: 5px;"
  37. " font: normal 18px 'Microsoft YaHei';"
  38. "}"
  39. "QPushButton#btnLogin:disabled {"
  40. " background-color: rgb(200, 200, 200);"
  41. " color: rgb(0, 0, 0);"
  42. "}");
  43. this->ui->btnLogin->setEnabled(false);
  44. this->ui->btnLogin->setText("登录中...");
  45. // 构造请求数据
  46. QJsonObject jsonData;
  47. jsonData["username"] = username;
  48. jsonData["password"] = password;
  49. QJsonDocument doc(jsonData);
  50. QByteArray data = doc.toJson();
  51. QUrl fullUrl = apiBackendUrl.resolved(QUrl("login/pc")); // 替换为实际的服务器接口地址
  52. QNetworkRequest request(fullUrl); // 替换为实际的服务器接口地址
  53. request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
  54. // 发送 POST 请求
  55. QNetworkReply *reply = manager->post(request, data);
  56. // 处理响应
  57. auto handleLoginResponse = [this, username, password](QNetworkReply *reply) {
  58. try {
  59. if (reply->error() == QNetworkReply::NoError) {
  60. QByteArray responseData = reply->readAll();
  61. QJsonDocument responseDoc = QJsonDocument::fromJson(responseData);
  62. if (!responseDoc.isNull() && responseDoc.isObject()) {
  63. QJsonObject responseObj = responseDoc.object();
  64. if (responseObj.contains("code") && responseObj["code"].isDouble()) {
  65. int code = responseObj["code"].toInt();
  66. if (code == 500) {
  67. QMessageBox *msgBox = new QMessageBox(this);
  68. msgBox->setAttribute(Qt::WA_TranslucentBackground, true);
  69. msgBox->setWindowTitle("");
  70. msgBox->setText("用户名或密码错误,请重试。");
  71. msgBox->setWindowFlags(msgBox->windowFlags() |
  72. Qt::FramelessWindowHint);
  73. msgBox->setStandardButtons(QMessageBox::NoButton);
  74. msgBox->setStyleSheet(
  75. "QMessageBox {"
  76. " border: none;"
  77. "}"
  78. "QLabel {"
  79. " color: #a94442;"
  80. " background-color: transparent;"
  81. " font-size: 20px;"
  82. "}"
  83. "QPushButton {"
  84. " visibility: hidden;"
  85. "}");
  86. QTimer::singleShot(1500, msgBox, [msgBox]() {
  87. msgBox->close();
  88. delete msgBox;
  89. });
  90. msgBox->show();
  91. } else if (code == 200) {
  92. QJsonObject userInfoObj = responseObj["userInfo"].toObject();
  93. QJsonObject userObj = userInfoObj["user"].toObject();
  94. int userId = userObj["userId"].toInt();
  95. QString identity = userObj["identity"].toString();
  96. QString userIdStr = QString::number(userId);
  97. registryManager->saveUserInfo(userIdStr, username, password,
  98. 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 test code
  105. QString jsonString =
  106. "{\"app_version\":\"1.52\",\"blast_latitude\":\"30."
  107. "21122186731856\",\"blast_longitude\":\"120.22146062951883\","
  108. "\"blast_time\":\"2025-06-03 "
  109. "12:00:00\",\"company_code\":\"3701234300003\",\"equipment_"
  110. "sn\":\"F34A0000001\",\"error_deto_count\":\"0\",\"operator_"
  111. "identity\":\"330781198509079292\",\"operator_name\":\"栋工\","
  112. "\"phone\":\"18611112222\",\"project_htid\":\"\",\"project_"
  113. "name\":\"sidf\",\"project_xmbh\":\"\",\"reg_deto_count\":"
  114. "\"2\",\"regs\":[{\"after_test_bus_i\":\"41\",\"after_test_bus_"
  115. "v\":\"8006\",\"before_blasting_i\":\"49\",\"before_blasting_"
  116. "v\":\"13492\",\"bus_leakage_current_i\":\"0\",\"dets\":[{"
  117. "\"delay_time\":\"0.0\",\"freq\":\"0\",\"in_code\":"
  118. "\"005AC8360A4C01A7\",\"out_code\":\"2411104F18000\","
  119. "\"status\":\"0x00\",\"tag\":\"1-1-1\",\"uid\":"
  120. "\"24211104F18000\"},{\"delay_time\":\"80.0\",\"freq\":\"0\","
  121. "\"in_code\":\"015AC8360A4C014E\",\"out_code\":"
  122. "\"2411104F18001\",\"status\":\"0x00\",\"tag\":\"1-2-1\","
  123. "\"uid\":\"24211104F18001\"}],\"equipment_sn\":\"null\","
  124. "\"equipment_version\":\"null\",\"error_deto_count\":\"0\","
  125. "\"net_charged_i\":\"49\",\"net_charged_v\":\"13501\",\"reg_"
  126. "deto_count\":\"2\"}]}";
  127. QJsonObject myJsonObject =
  128. QJsonDocument::fromJson(jsonString.toUtf8()).object();
  129. qDebug()
  130. << "debug"
  131. << QDateTime::fromString(myJsonObject["blast_time"].toString(),
  132. "yyyy-MM-dd hh:mm:ss")
  133. .toString(Qt::ISODateWithMs);
  134. int row = 1;
  135. firingWidget *wt = new firingWidget(row, false, "111");
  136. wt->testonBlastSucess(myJsonObject);
  137. //<< Delete
  138. */
  139. QScreen *screen = QGuiApplication::primaryScreen();
  140. QRect screenGeometry = screen->geometry();
  141. int screenWidth = screenGeometry.width();
  142. int screenHeight = screenGeometry.height();
  143. mainWindow->resize(screenWidth * 1, screenHeight * 0.95);
  144. int windowWidth = mainWindow->width();
  145. int windowHeight = mainWindow->height();
  146. int x = (screenWidth - windowWidth) / 2;
  147. int y = (screenHeight - windowHeight) / 2;
  148. mainWindow->move(x, y - 10);
  149. mainWindow->show();
  150. }
  151. }
  152. }
  153. } else {
  154. QMessageBox::critical(nullptr, "无法登录,网络请求错误",
  155. QString("错误信息: %1\n错误代码: %2")
  156. .arg(reply->errorString())
  157. .arg(reply->error()));
  158. }
  159. } catch (const std::exception &e) {
  160. qDebug() << "Exception in response handling: " << e.what();
  161. }
  162. this->ui->btnLogin->setEnabled(true);
  163. this->ui->btnLogin->setText("登录");
  164. reply->deleteLater();
  165. };
  166. QObject::connect(reply, &QNetworkReply::finished,
  167. [reply, handleLoginResponse]() { handleLoginResponse(reply); });
  168. } catch (const std::exception &e) {
  169. qDebug() << "Exception in request sending: " << e.what();
  170. }
  171. }
  172. void LoginWindow::mousePressEvent(QMouseEvent *event) {
  173. if (event->button() == Qt::LeftButton) {
  174. m_dragPosition = event->globalPos() - frameGeometry().topLeft();
  175. event->accept();
  176. }
  177. }
  178. void LoginWindow::mouseMoveEvent(QMouseEvent *event) {
  179. if (event->buttons() & Qt::LeftButton) {
  180. move(event->globalPos() - m_dragPosition);
  181. event->accept();
  182. }
  183. }
  184. void LoginWindow::on_btnClose_clicked() { this->close(); }
  185. void LoginWindow::on_btnMin_clicked() { this->showMinimized(); }
  186. void LoginWindow::on_username_activated(int index) { qDebug() << index; }
  187. void LoginWindow::on_username_currentIndexChanged(int index) {
  188. QString name = ui->username->currentText();
  189. QString pass = registryManager->getPasswordByUsername(name);
  190. ui->password->setText(pass);
  191. }