|
@@ -14,8 +14,12 @@
|
|
|
#include "qwidget.h"
|
|
#include "qwidget.h"
|
|
|
#include "utils/layoutbuilder.h"
|
|
#include "utils/layoutbuilder.h"
|
|
|
|
|
|
|
|
|
|
+#include "widgets/captchalabel.h"
|
|
|
|
|
+
|
|
|
#include "api/tloginapi.h"
|
|
#include "api/tloginapi.h"
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
TLogin::TLogin(QWidget *parent)
|
|
TLogin::TLogin(QWidget *parent)
|
|
|
: QDialog(parent)
|
|
: QDialog(parent)
|
|
|
{
|
|
{
|
|
@@ -26,38 +30,89 @@ TLogin::TLogin(QWidget *parent)
|
|
|
using namespace Layouting;
|
|
using namespace Layouting;
|
|
|
|
|
|
|
|
info = new QLabel;
|
|
info = new QLabel;
|
|
|
- info->setStyleSheet("color: white;");
|
|
|
|
|
- info->setText(tr("link failure"));
|
|
|
|
|
|
|
+ info->setStyleSheet("color: #333333;");
|
|
|
|
|
+ info->setText(tr("Welcome to the system"));
|
|
|
|
|
+
|
|
|
// 创建登录组件
|
|
// 创建登录组件
|
|
|
usernameEdit = new QLineEdit();
|
|
usernameEdit = new QLineEdit();
|
|
|
usernameEdit->setPlaceholderText(tr("Enter your user name"));
|
|
usernameEdit->setPlaceholderText(tr("Enter your user name"));
|
|
|
|
|
+ usernameEdit->setMinimumWidth(200);
|
|
|
|
|
+
|
|
|
passwordEdit = new QLineEdit();
|
|
passwordEdit = new QLineEdit();
|
|
|
- // passwordEdit->setEchoMode(QLineEdit::Password);
|
|
|
|
|
|
|
+ passwordEdit->setEchoMode(QLineEdit::Password);
|
|
|
passwordEdit->setPlaceholderText(tr("Enter your ExamNo"));
|
|
passwordEdit->setPlaceholderText(tr("Enter your ExamNo"));
|
|
|
- // QRegExp regExp("[a-zA-Z0-9]{6,12}");
|
|
|
|
|
- // QValidator *validator = new QRegExpValidator(regExp, passwordEdit);
|
|
|
|
|
- // passwordEdit->setValidator(validator);
|
|
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 添加验证码输入框
|
|
|
|
|
+ captchaEdit = new QLineEdit();
|
|
|
|
|
+ captchaEdit->setPlaceholderText(tr("Enter captcha"));
|
|
|
|
|
+ captchaEdit->setMaxLength(6);
|
|
|
|
|
+
|
|
|
|
|
+ // 验证码图片显示
|
|
|
|
|
+ captchaImage = new CaptchaLabel();
|
|
|
|
|
+ captchaImage->setFixedSize(120, 40);
|
|
|
|
|
+ captchaImage->setScaledContents(true);
|
|
|
|
|
+ captchaImage->setCursor(Qt::PointingHandCursor); // 设置鼠标指针为手型,提示可点击
|
|
|
|
|
+ captchaImage->setToolTip(tr("Click to refresh captcha")); // 添加提示文本
|
|
|
|
|
+
|
|
|
|
|
+ // 连接 clicked 信号到刷新验证码的槽函数
|
|
|
|
|
+ connect(captchaImage, &CaptchaLabel::clicked, this, &TLogin::getCaptcha);
|
|
|
|
|
+
|
|
|
|
|
+ // 创建表单布局,将captchaEdit和captchaImage放在同一行
|
|
|
QWidget *w = Group{Column{Row{st, tr("Login"), st},
|
|
QWidget *w = Group{Column{Row{st, tr("Login"), st},
|
|
|
- Form{tr("Username:"), usernameEdit, br, tr("ExamNo:"), passwordEdit},
|
|
|
|
|
|
|
+ Form{tr("Username:"), usernameEdit,
|
|
|
|
|
+ br, tr("Password:"), passwordEdit,
|
|
|
|
|
+ br, tr("Captcha:"), Row{captchaEdit, captchaImage}},
|
|
|
PushButton{bindTo(&loginPushButton),
|
|
PushButton{bindTo(&loginPushButton),
|
|
|
text(tr("Login")),
|
|
text(tr("Login")),
|
|
|
onClicked(std::bind(&TLogin::on_login_clicked, this))}}}
|
|
onClicked(std::bind(&TLogin::on_login_clicked, this))}}}
|
|
|
.emerge();
|
|
.emerge();
|
|
|
|
|
|
|
|
QPushButton *close;
|
|
QPushButton *close;
|
|
|
- auto h = Row{"", st, PushButton{bindTo(&close), "test"}};
|
|
|
|
|
|
|
+ auto h = Row{st, PushButton{bindTo(&close), ""}};
|
|
|
|
|
|
|
|
Column{h, st, Row{st, w, st}, st, info, noMargin}.attachTo(this);
|
|
Column{h, st, Row{st, w, st}, st, info, noMargin}.attachTo(this);
|
|
|
|
|
|
|
|
|
|
+ // 设置美观的样式表,不使用背景图片
|
|
|
QString styleSheet = R"(
|
|
QString styleSheet = R"(
|
|
|
QDialog#Login {
|
|
QDialog#Login {
|
|
|
- background-image: url(:/bg/bg1.png);
|
|
|
|
|
|
|
+ background-color: #2c3e50;
|
|
|
|
|
+ border: 1px solid #34495e;
|
|
|
|
|
+ border-radius: 5px;
|
|
|
}
|
|
}
|
|
|
QGroupBox {
|
|
QGroupBox {
|
|
|
- background-color: rgba(255, 255, 255, 128);
|
|
|
|
|
|
|
+ background-color: #ecf0f1;
|
|
|
|
|
+ border-radius: 5px;
|
|
|
|
|
+ border: 1px solid #bdc3c7;
|
|
|
|
|
+ padding: 10px;
|
|
|
|
|
+ }
|
|
|
|
|
+ QLineEdit {
|
|
|
|
|
+ border: 1px solid #bdc3c7;
|
|
|
|
|
+ border-radius: 3px;
|
|
|
|
|
+ padding: 5px;
|
|
|
|
|
+ background-color: white;
|
|
|
|
|
+ }
|
|
|
|
|
+ QLineEdit:focus {
|
|
|
|
|
+ border: 1px solid #3498db;
|
|
|
|
|
+ background-color: #f8f9fa;
|
|
|
|
|
+ }
|
|
|
|
|
+ QPushButton {
|
|
|
|
|
+ background-color: #3498db;
|
|
|
|
|
+ color: white;
|
|
|
|
|
+ border: none;
|
|
|
|
|
+ border-radius: 3px;
|
|
|
|
|
+ padding: 8px 16px;
|
|
|
|
|
+ font-weight: bold;
|
|
|
|
|
+ }
|
|
|
|
|
+ QPushButton:hover {
|
|
|
|
|
+ background-color: #2980b9;
|
|
|
|
|
+ }
|
|
|
|
|
+ QPushButton:disabled {
|
|
|
|
|
+ background-color: #95a5a6;
|
|
|
|
|
+ }
|
|
|
|
|
+ QLabel {
|
|
|
|
|
+ color: #333333;
|
|
|
}
|
|
}
|
|
|
-)";
|
|
|
|
|
|
|
+ )";
|
|
|
setStyleSheet(styleSheet);
|
|
setStyleSheet(styleSheet);
|
|
|
|
|
|
|
|
QPixmap closePix = style()->standardPixmap(QStyle::SP_TitleBarCloseButton);
|
|
QPixmap closePix = style()->standardPixmap(QStyle::SP_TitleBarCloseButton);
|
|
@@ -66,34 +121,48 @@ TLogin::TLogin(QWidget *parent)
|
|
|
close->setStyleSheet(R"__(
|
|
close->setStyleSheet(R"__(
|
|
|
QPushButton {
|
|
QPushButton {
|
|
|
border: none;
|
|
border: none;
|
|
|
- background-color: #ffffff; /* 红色背景 */
|
|
|
|
|
- color: white; /* 文字颜色 */
|
|
|
|
|
- padding: 10px 10px; /* 内边距 */
|
|
|
|
|
- text-align: center; /* 文字居中 */
|
|
|
|
|
- text-decoration: none; /* 去掉下划线 */
|
|
|
|
|
- font-size: 12px; /* 字体大小 */
|
|
|
|
|
- margin: 2px 2px; /* 外边距 */
|
|
|
|
|
- border-radius: 10px; /* 圆角 */
|
|
|
|
|
|
|
+ background-color: transparent;
|
|
|
|
|
+ padding: 5px;
|
|
|
}
|
|
}
|
|
|
QPushButton:hover {
|
|
QPushButton:hover {
|
|
|
- background-color: #d32f2f; /* 鼠标悬停时背景色 */
|
|
|
|
|
|
|
+ background-color: #e74c3c;
|
|
|
|
|
+ border-radius: 3px;
|
|
|
}
|
|
}
|
|
|
)__");
|
|
)__");
|
|
|
|
|
|
|
|
- resize({400, 400});
|
|
|
|
|
- // usernameEdit->setText("test");
|
|
|
|
|
- // passwordEdit->setText("SW1111112");
|
|
|
|
|
- loginPushButton->setDisabled(true);
|
|
|
|
|
|
|
+ resize({400, 450});
|
|
|
|
|
+ loginPushButton->setEnabled(true);
|
|
|
QObject::connect(close, &QPushButton::clicked, this, &TLogin::close);
|
|
QObject::connect(close, &QPushButton::clicked, this, &TLogin::close);
|
|
|
- // QObject::connect(AppEvent::instance(), &AppEvent::serverLinkSignals, this, [this](bool isok) {
|
|
|
|
|
- // if (isok) {
|
|
|
|
|
- // info->setText(tr("link succeeded"));
|
|
|
|
|
- // loginPushButton->setDisabled(false);
|
|
|
|
|
- // } else {
|
|
|
|
|
- // info->setText(tr("link failure"));
|
|
|
|
|
- // loginPushButton->setDisabled(true);
|
|
|
|
|
- // }
|
|
|
|
|
- // });
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 获取初始验证码
|
|
|
|
|
+ getCaptcha();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void TLogin::getCaptcha()
|
|
|
|
|
+{
|
|
|
|
|
+ bool ok = false;
|
|
|
|
|
+ TC::Captcha::Data captchaData = TC::Captcha().get(&ok);
|
|
|
|
|
+ if (ok) {
|
|
|
|
|
+ captchaId = captchaData.captchaId;
|
|
|
|
|
+
|
|
|
|
|
+ // 处理Base64图片数据
|
|
|
|
|
+ QString imgData = captchaData.imgPath;
|
|
|
|
|
+ if (imgData.startsWith("data:image/png;base64,")) {
|
|
|
|
|
+ imgData = imgData.mid(22); // 移除前缀
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ QByteArray imageBytes = QByteArray::fromBase64(imgData.toLatin1());
|
|
|
|
|
+ QPixmap pixmap;
|
|
|
|
|
+ pixmap.loadFromData(imageBytes);
|
|
|
|
|
+ captchaImage->setPixmap(pixmap);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ info->setText(tr("Failed to get captcha"));
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void TLogin::on_refresh_captcha_clicked()
|
|
|
|
|
+{
|
|
|
|
|
+ getCaptcha();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void TLogin::mousePressEvent(QMouseEvent *event)
|
|
void TLogin::mousePressEvent(QMouseEvent *event)
|
|
@@ -111,27 +180,46 @@ void TLogin::mouseMoveEvent(QMouseEvent *event)
|
|
|
event->accept();
|
|
event->accept();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
void TLogin::on_login_clicked()
|
|
void TLogin::on_login_clicked()
|
|
|
{
|
|
{
|
|
|
const QString userName = usernameEdit->text();
|
|
const QString userName = usernameEdit->text();
|
|
|
const QString password = passwordEdit->text();
|
|
const QString password = passwordEdit->text();
|
|
|
|
|
+ const QString captcha = captchaEdit->text();
|
|
|
|
|
|
|
|
if (userName.isEmpty() || password.isEmpty()) {
|
|
if (userName.isEmpty() || password.isEmpty()) {
|
|
|
QMessageBox::warning(this,
|
|
QMessageBox::warning(this,
|
|
|
tr("Login failed"),
|
|
tr("Login failed"),
|
|
|
- tr("User name or password error, please try again."));
|
|
|
|
|
|
|
+ tr("User name or password cannot be empty."));
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (captcha.isEmpty()) {
|
|
|
|
|
+ QMessageBox::warning(this,
|
|
|
|
|
+ tr("Login failed"),
|
|
|
|
|
+ tr("Captcha cannot be empty."));
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
- bool ok = TC::Login(userName, password).post();
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 这里需要修改Login API,添加验证码参数
|
|
|
|
|
+ // 由于我们没有看到Login类的完整实现,这里假设需要修改Login构造函数
|
|
|
|
|
+ // 实际实现时需要根据后端API的要求进行调整
|
|
|
|
|
+
|
|
|
|
|
+ // 修改Login类构造函数,添加captchaId和captcha参数
|
|
|
|
|
+ // 这里只是示例,实际实现可能需要调整
|
|
|
|
|
+ bool ok = TC::Login(userName, password, captchaId, captcha).post();
|
|
|
|
|
+
|
|
|
if (ok && AppEvent::instance()->isLogin()) {
|
|
if (ok && AppEvent::instance()->isLogin()) {
|
|
|
// 登录成功,发出信号
|
|
// 登录成功,发出信号
|
|
|
emit loginSuccessful();
|
|
emit loginSuccessful();
|
|
|
close();
|
|
close();
|
|
|
} else {
|
|
} else {
|
|
|
- // 登录失败,显示提示框
|
|
|
|
|
|
|
+ // 登录失败,显示提示框并刷新验证码
|
|
|
QMessageBox::warning(this,
|
|
QMessageBox::warning(this,
|
|
|
tr("Login failed"),
|
|
tr("Login failed"),
|
|
|
- tr("User name or password error, please try again."));
|
|
|
|
|
|
|
+ tr("User name, password or captcha error, please try again."));
|
|
|
|
|
+ getCaptcha(); // 刷新验证码
|
|
|
|
|
+ captchaEdit->clear(); // 清空验证码输入框
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|