loginwindow.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "loginwindow.h"
  2. #include "../mainwindow/mainwindow.h"
  3. #include "../utils/backendapimanager.h"
  4. #include "ui_loginwindow.h"
  5. LoginWindow::LoginWindow(QWidget *parent)
  6. : QWidget(parent), ui(new Ui::LoginWindow), manager(new QNetworkAccessManager(this)) {
  7. ui->setupUi(this);
  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::instance()->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() | 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(userIdStr, username, password, identity,
  98. certName);
  99. RegistryManager::instance()->setCurentLoginUserId(userIdStr);
  100. QString Authority = responseObj["currentAuthority"].toString();
  101. globalAuthority = "Bearer " + Authority;
  102. backendAPIManager::setAuthToken(globalAuthority);
  103. this->close();
  104. MainWindow *mainWindow = new MainWindow();
  105. QScreen *screen = QGuiApplication::primaryScreen();
  106. QRect screenGeometry = screen->geometry();
  107. int screenWidth = screenGeometry.width();
  108. int screenHeight = screenGeometry.height();
  109. mainWindow->resize(screenWidth * 1, screenHeight * 0.95);
  110. int windowWidth = mainWindow->width();
  111. int windowHeight = mainWindow->height();
  112. int x = (screenWidth - windowWidth) / 2;
  113. int y = (screenHeight - windowHeight) / 2;
  114. mainWindow->move(x, y - 10);
  115. mainWindow->show();
  116. }
  117. }
  118. }
  119. } else {
  120. QMessageBox::critical(
  121. nullptr, "无法登录,网络请求错误",
  122. QString("错误信息: %1\n错误代码: %2").arg(reply->errorString()).arg(reply->error()));
  123. }
  124. } catch (const std::exception &e) {
  125. qDebug() << "Exception in response handling: " << e.what();
  126. }
  127. this->ui->btnLogin->setEnabled(true);
  128. this->ui->btnLogin->setText("登录");
  129. reply->deleteLater();
  130. };
  131. QObject::connect(reply, &QNetworkReply::finished,
  132. [reply, handleLoginResponse]() { handleLoginResponse(reply); });
  133. } catch (const std::exception &e) {
  134. qDebug() << "Exception in request sending: " << e.what();
  135. }
  136. }
  137. void LoginWindow::mousePressEvent(QMouseEvent *event) {
  138. if (event->button() == Qt::LeftButton) {
  139. m_dragPosition = event->globalPos() - frameGeometry().topLeft();
  140. event->accept();
  141. }
  142. }
  143. void LoginWindow::mouseMoveEvent(QMouseEvent *event) {
  144. if (event->buttons() & Qt::LeftButton) {
  145. move(event->globalPos() - m_dragPosition);
  146. event->accept();
  147. }
  148. }
  149. void LoginWindow::on_btnClose_clicked() { this->close(); }
  150. void LoginWindow::on_btnMin_clicked() { this->showMinimized(); }
  151. void LoginWindow::on_username_activated(int index) { qDebug() << index; }
  152. void LoginWindow::on_username_currentIndexChanged(int index) {
  153. QString name = ui->username->currentText();
  154. QString pass = RegistryManager::instance()->getPasswordByUsername(name);
  155. ui->password->setText(pass);
  156. }