#include "loginwindow.h" #include "../mainwindow/mainwindow.h" #include "../utils/backendapimanager.h" #include "ui_loginwindow.h" LoginWindow::LoginWindow(QWidget *parent) : QWidget(parent), ui(new Ui::LoginWindow), manager(new QNetworkAccessManager(this)) { ui->setupUi(this); ui->username->lineEdit()->setPlaceholderText("请输入用户名"); QMovie *movie = new QMovie(this); movie->setFileName(":/icons/icons/hxgc.gif"); movie->start(); ui->label->setMovie(movie); setWindowFlags(Qt::FramelessWindowHint); initUesrCombox(); } void LoginWindow::initUesrCombox() { QStringList allUsernames = RegistryManager::instance()->getAllUsernames(); for (const QString &username : allUsernames) { ui->username->addItem(username); } ui->username->setCurrentIndex(-1); } LoginWindow::~LoginWindow() { delete ui; } void LoginWindow::on_btnLogin_clicked() { QString username = ui->username->currentText(); QString password = ui->password->text(); if (username.isEmpty() || password.isEmpty()) { QMessageBox::critical(nullptr, "输入错误", "用户名或密码不能为空,请重新输入。"); return; } try { this->ui->btnLogin->setStyleSheet( "QPushButton#btnLogin {" " background-color: rgb(33, 46, 224);" " color: white;" " border-radius: 5px;" " font: normal 18px 'Microsoft YaHei';" "}" "QPushButton#btnLogin:disabled {" " background-color: rgb(200, 200, 200);" " color: rgb(0, 0, 0);" "}"); this->ui->btnLogin->setEnabled(false); this->ui->btnLogin->setText("登录中..."); // 构造请求数据 QJsonObject jsonData; jsonData["username"] = username; jsonData["password"] = password; QJsonDocument doc(jsonData); QByteArray data = doc.toJson(); QUrl fullUrl = apiBackendUrl.resolved(QUrl("login/pc")); // 替换为实际的服务器接口地址 QNetworkRequest request(fullUrl); // 替换为实际的服务器接口地址 request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); // 发送 POST 请求 QNetworkReply *reply = manager->post(request, data); // 处理响应 auto handleLoginResponse = [this, username, password](QNetworkReply *reply) { try { if (reply->error() == QNetworkReply::NoError) { QByteArray responseData = reply->readAll(); QJsonDocument responseDoc = QJsonDocument::fromJson(responseData); if (!responseDoc.isNull() && responseDoc.isObject()) { QJsonObject responseObj = responseDoc.object(); if (responseObj.contains("code") && responseObj["code"].isDouble()) { int code = responseObj["code"].toInt(); if (code == 500) { QMessageBox *msgBox = new QMessageBox(this); msgBox->setAttribute(Qt::WA_TranslucentBackground, true); msgBox->setWindowTitle(""); msgBox->setText("用户名或密码错误,请重试。"); msgBox->setWindowFlags(msgBox->windowFlags() | Qt::FramelessWindowHint); msgBox->setStandardButtons(QMessageBox::NoButton); msgBox->setStyleSheet( "QMessageBox {" " border: none;" "}" "QLabel {" " color: #a94442;" " background-color: transparent;" " font-size: 20px;" "}" "QPushButton {" " visibility: hidden;" "}"); QTimer::singleShot(1500, msgBox, [msgBox]() { msgBox->close(); delete msgBox; }); msgBox->show(); } else if (code == 200) { QJsonObject userInfoObj = responseObj["userInfo"].toObject(); QJsonObject userObj = userInfoObj["user"].toObject(); int userId = userObj["userId"].toInt(); QString identity = userObj["identity"].toString(); QString userIdStr = QString::number(userId); QString certName = userObj["nickName"].toString(); RegistryManager::instance()->saveUserInfo(userIdStr, username, password, identity, certName); RegistryManager::instance()->setCurentLoginUserId(userIdStr); QString Authority = responseObj["currentAuthority"].toString(); globalAuthority = "Bearer " + Authority; backendAPIManager::setAuthToken(globalAuthority); this->close(); MainWindow *mainWindow = new MainWindow(); QScreen *screen = QGuiApplication::primaryScreen(); QRect screenGeometry = screen->geometry(); int screenWidth = screenGeometry.width(); int screenHeight = screenGeometry.height(); mainWindow->resize(screenWidth * 1, screenHeight * 0.95); int windowWidth = mainWindow->width(); int windowHeight = mainWindow->height(); int x = (screenWidth - windowWidth) / 2; int y = (screenHeight - windowHeight) / 2; mainWindow->move(x, y - 10); mainWindow->show(); } } } } else { QMessageBox::critical( nullptr, "无法登录,网络请求错误", QString("错误信息: %1\n错误代码: %2").arg(reply->errorString()).arg(reply->error())); } } catch (const std::exception &e) { qDebug() << "Exception in response handling: " << e.what(); } this->ui->btnLogin->setEnabled(true); this->ui->btnLogin->setText("登录"); reply->deleteLater(); }; QObject::connect(reply, &QNetworkReply::finished, [reply, handleLoginResponse]() { handleLoginResponse(reply); }); } catch (const std::exception &e) { qDebug() << "Exception in request sending: " << e.what(); } } void LoginWindow::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { m_dragPosition = event->globalPos() - frameGeometry().topLeft(); event->accept(); } } void LoginWindow::mouseMoveEvent(QMouseEvent *event) { if (event->buttons() & Qt::LeftButton) { move(event->globalPos() - m_dragPosition); event->accept(); } } void LoginWindow::on_btnClose_clicked() { this->close(); } void LoginWindow::on_btnMin_clicked() { this->showMinimized(); } void LoginWindow::on_username_activated(int index) { qDebug() << index; } void LoginWindow::on_username_currentIndexChanged(int index) { QString name = ui->username->currentText(); QString pass = RegistryManager::instance()->getPasswordByUsername(name); ui->password->setText(pass); }