loginwindow.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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() | 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. QTimer::singleShot(1500, msgBox, [msgBox]() {
  85. msgBox->close();
  86. delete msgBox;
  87. });
  88. msgBox->show();
  89. } else if (code == 200) {
  90. QJsonObject userInfoObj = responseObj["userInfo"].toObject();
  91. QJsonObject userObj = userInfoObj["user"].toObject();
  92. int userId = userObj["userId"].toInt();
  93. QString identity = userObj["identity"].toString();
  94. QString userIdStr = QString::number(userId);
  95. QString certName = userObj["nickName"].toString();
  96. RegistryManager::instance()->saveUserInfo(userIdStr, username, password, identity,
  97. certName);
  98. RegistryManager::instance()->setCurentLoginUserId(userIdStr);
  99. QString Authority = responseObj["currentAuthority"].toString();
  100. globalAuthority = "Bearer " + Authority;
  101. backendAPIManager::setAuthToken(globalAuthority);
  102. this->close();
  103. MainWindow *mainWindow = new MainWindow();
  104. QScreen *screen = QGuiApplication::primaryScreen();
  105. QRect screenGeometry = screen->geometry();
  106. int screenWidth = screenGeometry.width();
  107. int screenHeight = screenGeometry.height();
  108. mainWindow->resize(screenWidth * 1, screenHeight * 0.95);
  109. int windowWidth = mainWindow->width();
  110. int windowHeight = mainWindow->height();
  111. int x = (screenWidth - windowWidth) / 2;
  112. int y = (screenHeight - windowHeight) / 2;
  113. mainWindow->move(x, y - 10);
  114. mainWindow->show();
  115. }
  116. }
  117. }
  118. } else {
  119. QMessageBox::critical(
  120. nullptr, "无法登录,网络请求错误",
  121. QString("错误信息: %1\n错误代码: %2").arg(reply->errorString()).arg(reply->error()));
  122. }
  123. } catch (const std::exception &e) {
  124. qDebug() << "Exception in response handling: " << e.what();
  125. }
  126. this->ui->btnLogin->setEnabled(true);
  127. this->ui->btnLogin->setText("登录");
  128. reply->deleteLater();
  129. };
  130. QObject::connect(reply, &QNetworkReply::finished,
  131. [reply, handleLoginResponse]() { handleLoginResponse(reply); });
  132. } catch (const std::exception &e) {
  133. qDebug() << "Exception in request sending: " << e.what();
  134. }
  135. }
  136. void LoginWindow::mousePressEvent(QMouseEvent *event) {
  137. if (event->button() == Qt::LeftButton) {
  138. m_dragPosition = event->globalPos() - frameGeometry().topLeft();
  139. event->accept();
  140. }
  141. }
  142. void LoginWindow::mouseMoveEvent(QMouseEvent *event) {
  143. if (event->buttons() & Qt::LeftButton) {
  144. move(event->globalPos() - m_dragPosition);
  145. event->accept();
  146. }
  147. }
  148. void LoginWindow::on_btnClose_clicked() { this->close(); }
  149. void LoginWindow::on_btnMin_clicked() { this->showMinimized(); }
  150. void LoginWindow::on_username_activated(int index) { qDebug() << index; }
  151. void LoginWindow::on_username_currentIndexChanged(int index) {
  152. QString name = ui->username->currentText();
  153. QString pass = RegistryManager::instance()->getPasswordByUsername(name);
  154. ui->password->setText(pass);
  155. }