Files
QT_wuhan_YT/pages/bootpassword.cpp
T
2026-09-09 17:14:31 +08:00

162 lines
4.3 KiB
C++

#include "bootpassword.h"
#include "numpaddialog.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QLabel>
#include <QPushButton>
#include <QTimer>
#include "global.h"
BootPasswordPage::BootPasswordPage(AppContext* ctx, QWidget* parent)
: BasePage(ctx, parent)
{
QVBoxLayout* root = new QVBoxLayout(this);
root->setContentsMargins(0, 0, 0, 0);
root->addStretch(1);
QWidget* col = new QWidget(this);
col->setFixedWidth(800);
QVBoxLayout* lay = new QVBoxLayout(col);
lay->setSpacing(24);
lay->setAlignment(Qt::AlignHCenter);
// 品牌区
QLabel* brand = label("“玉兔”工业无线遥控器", 32, C::TEXT, true, col);
brand->setAlignment(Qt::AlignCenter);
QLabel* model = label("YTZT6K8EART2-aL25", 20, C::TEXT_SUB, false, col);
model->setAlignment(Qt::AlignCenter);
lay->addWidget(brand);
lay->addWidget(model);
lay->addSpacing(20);
// 标题
QLabel* title = label("请输入开机密码", 28, C::TEXT, true, col);
title->setAlignment(Qt::AlignCenter);
lay->addWidget(title);
// 密码输入框(只读回显,点击弹出数字键盘)
m_input = new QLineEdit(col);
m_input->setReadOnly(true);
m_input->setEchoMode(QLineEdit::Password);
m_input->setAlignment(Qt::AlignCenter);
m_input->setFixedSize(400, 88);
m_input->setPlaceholderText("点击输入密码");
m_input->setProperty("clickable", true);
lay->addWidget(m_input, 0, Qt::AlignHCenter);
m_input->installEventFilter(this);
m_input->setFocusPolicy(Qt::NoFocus);
// 错误提示(默认隐藏)
m_error = label("密码输入有误,请重新输入", 18, C::DANGER, true, col);
m_error->setAlignment(Qt::AlignCenter);
m_error->hide();
lay->addWidget(m_error);
// 确认 / 取消
QHBoxLayout* btnRow = new QHBoxLayout();
btnRow->setSpacing(40);
btnRow->addStretch(1);
QPushButton* ok = button("确认", "primary", col);
ok->setFixedSize(180, 72);
connect(ok, &QPushButton::clicked, this, &BootPasswordPage::onConfirm);
QPushButton* cancel = button("取消", "", col);
cancel->setFixedSize(180, 72);
connect(cancel, &QPushButton::clicked, this, &BootPasswordPage::onCancel);
btnRow->addWidget(ok);
btnRow->addWidget(cancel);
btnRow->addStretch(1);
lay->addLayout(btnRow);
// 忘记密码链接
QPushButton* forget = button("忘记密码/修改密码", "link", col);
forget->setFixedHeight(44);
forget->setCursor(Qt::PointingHandCursor);
connect(forget, &QPushButton::clicked, this, &BootPasswordPage::onForget);
lay->addWidget(forget);
root->addWidget(col, 0, Qt::AlignHCenter);
root->addStretch(1);
refreshInput();
}
// 点击密码输入框 → 弹出数字键盘(拦截鼠标点击)
bool BootPasswordPage::eventFilter(QObject* obj, QEvent* ev)
{
if (obj == m_input && ev->type() == QEvent::MouseButtonPress) {
onInputClicked();
return true;
}
return BasePage::eventFilter(obj, ev);
}
void BootPasswordPage::onInputClicked()
{
if (m_locked)
return;
NumPadDialog dlg(this);
dlg.setMaxLen(4);
dlg.setValue(m_value);
if (dlg.exec() == QDialog::Accepted) {
m_value = dlg.value();
m_error->hide();
refreshInput();
}
}
void BootPasswordPage::onConfirm()
{
if (m_locked)
return;
if (m_value == ctx()->password()) {
m_wrongCount = 0;
clearInput();
emit navigate(PageSelfCheck);
} else {
m_wrongCount++;
clearInput();
if (m_wrongCount >= 5) {
// 【设计建议】连续 5 次错误锁定 30s
m_locked = true;
m_error->setText("连续输入错误,已锁定 30 秒");
m_error->show();
QTimer::singleShot(30000, this, [this]() {
m_locked = false;
m_wrongCount = 0;
m_error->hide();
});
} else {
emit navigate(PageWarning);
}
}
}
void BootPasswordPage::onCancel()
{
clearInput();
}
void BootPasswordPage::onForget()
{
clearInput();
emit navigate(PageMgmtLogin);
}
void BootPasswordPage::clearInput()
{
m_value.clear();
refreshInput();
}
void BootPasswordPage::focusInput()
{
onInputClicked();
}
void BootPasswordPage::refreshInput()
{
m_input->setText(m_value);
}