#include "loginwindow.h"
|
#include "ui_loginwindow.h"
|
#include "registerwindow.h"
|
#include "datastructures.h"
|
#include <QMessageBox>
|
#include <QRegExpValidator>
|
#include <QTextCodec>
|
|
LoginWindow::LoginWindow(QWidget *parent) :
|
QWidget(parent),
|
ui(new Ui::LoginWindow)
|
{
|
ui->setupUi(this);
|
setWindowTitle("登录");
|
|
// 设置输入框验证
|
ui->passwordLineEdit->setEchoMode(QLineEdit::Password);
|
}
|
|
LoginWindow::~LoginWindow()
|
{
|
delete ui;
|
}
|
|
void LoginWindow::on_loginButton_clicked()
|
{
|
QString username = ui->usernameLineEdit->text().trimmed();
|
QString password = ui->passwordLineEdit->text().trimmed();
|
|
if (validateInput(username, password)) {
|
// 构造登录请求结构体
|
LoginReq loginReq;
|
strncpy(loginReq.username, username.toUtf8().constData(), sizeof(loginReq.username)-1);
|
strncpy(loginReq.password, password.toUtf8().constData(), sizeof(loginReq.password)-1);
|
|
// 这里发送登录请求
|
|
emit loginSuccess();
|
}
|
}
|
|
void LoginWindow::on_registerButton_clicked()
|
{
|
RegisterWindow registerWindow(this);
|
registerWindow.setWindowModality(Qt::ApplicationModal);
|
registerWindow.exec();
|
}
|
|
bool LoginWindow::validateInput(const QString &username, const QString &password)
|
{
|
if (username.isEmpty() || password.isEmpty()) {
|
QMessageBox::warning(this, "错误", "用户名和密码不能为空");
|
return false;
|
}
|
|
if (!isUsernameValid(username)) {
|
QMessageBox::warning(this, "错误", "用户名包含非法字符");
|
return false;
|
}
|
|
if (!isPasswordValid(password)) {
|
QMessageBox::warning(this, "错误", "密码长度应在6-16位之间,且只能包含字母、数字和下划线");
|
return false;
|
}
|
|
return true;
|
}
|
|
bool LoginWindow::isUsernameValid(const QString &username)
|
{
|
// 允许中文、字母、数字和下划线
|
QRegExp regex("^[\u4e00-\u9fa5a-zA-Z0-9_]{1,16}$");
|
return regex.exactMatch(username);
|
}
|
|
bool LoginWindow::isPasswordValid(const QString &password)
|
{
|
// 密码长度6-16位,包含字母、数字和下划线
|
QRegExp regex("^[a-zA-Z0-9_]{6,16}$");
|
return regex.exactMatch(password);
|
}
|