loginwindow.cpp 11 KB

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