#include "registerwindow.h"
|
#include "ui_registerwindow.h"
|
#include "datastructures.h"
|
#include <QMessageBox>
|
#include <QRegExpValidator>
|
#include <QTextCodec>
|
|
RegisterWindow::RegisterWindow(QWidget *parent) :
|
QDialog(parent),
|
ui(new Ui::RegisterWindow)
|
{
|
ui->setupUi(this);
|
setWindowTitle("注册");
|
|
// 设置密码输入框为密码模式
|
ui->passwordLineEdit->setEchoMode(QLineEdit::Password);
|
ui->confirmPasswordLineEdit->setEchoMode(QLineEdit::Password);
|
}
|
|
RegisterWindow::~RegisterWindow()
|
{
|
delete ui;
|
}
|
|
void RegisterWindow::on_registerButton_clicked()
|
{
|
QString username = ui->usernameLineEdit->text().trimmed();
|
QString password = ui->passwordLineEdit->text().trimmed();
|
QString confirmPassword = ui->confirmPasswordLineEdit->text().trimmed();
|
QString email = ui->emailLineEdit->text().trimmed();
|
QString tel = ui->telLineEdit->text().trimmed();
|
QString dept = ui->deptLineEdit->text().trimmed();
|
|
if (validateInput(username, password, confirmPassword, email, tel, dept)) {
|
// 构造注册请求结构体
|
RegisterReq registerReq;
|
strncpy(registerReq.username, username.toUtf8().constData(), sizeof(registerReq.username)-1);
|
strncpy(registerReq.password, password.toUtf8().constData(), sizeof(registerReq.password)-1);
|
strncpy(registerReq.email, email.toUtf8().constData(), sizeof(registerReq.email)-1);
|
strncpy(registerReq.tel, tel.toUtf8().constData(), sizeof(registerReq.tel)-1);
|
strncpy(registerReq.dept, dept.toUtf8().constData(), sizeof(registerReq.dept)-1);
|
|
// 这里发注册包
|
|
|
QMessageBox::information(this, "成功", "注册成功,请登录");
|
emit registrationSuccess();
|
close();
|
}
|
}
|
|
void RegisterWindow::on_cancelButton_clicked()
|
{
|
close();
|
}
|
|
bool RegisterWindow::validateInput(const QString &username, const QString &password, const QString &confirmPassword,
|
const QString &email, const QString &tel, const QString &dept)
|
{
|
// 检查所有字段是否为空
|
if (username.isEmpty() || password.isEmpty() || confirmPassword.isEmpty() ||
|
email.isEmpty() || tel.isEmpty() || dept.isEmpty()) {
|
QMessageBox::warning(this, "错误", "所有字段不能为空");
|
return false;
|
}
|
|
// 验证用户名
|
if (!isUsernameValid(username)) {
|
QMessageBox::warning(this, "错误", "用户名只能包含中文、字母、数字和下划线,且长度不超过16位");
|
return false;
|
}
|
|
// 验证密码
|
if (!isPasswordValid(password)) {
|
QMessageBox::warning(this, "错误", "密码长度应在6-16位之间,且只能包含字母、数字和下划线");
|
return false;
|
}
|
|
// 验证两次密码是否一致
|
if (password != confirmPassword) {
|
QMessageBox::warning(this, "错误", "两次输入的密码不一致");
|
return false;
|
}
|
|
// 验证邮箱
|
if (!isEmailValid(email)) {
|
QMessageBox::warning(this, "错误", "邮箱格式不正确");
|
return false;
|
}
|
|
// 验证手机号
|
if (!isTelValid(tel)) {
|
QMessageBox::warning(this, "错误", "手机号格式不正确");
|
return false;
|
}
|
|
// 验证部门
|
if (!isDeptValid(dept)) {
|
QMessageBox::warning(this, "错误", "部门名称只能包含中文、字母、数字和下划线,且长度不超过16位");
|
return false;
|
}
|
|
return true;
|
}
|
|
bool RegisterWindow::isUsernameValid(const QString &username)
|
{
|
// 允许中文、字母、数字和下划线,长度1-16位
|
QRegExp regex("^[\u4e00-\u9fa5a-zA-Z0-9_]{1,16}$");
|
return regex.exactMatch(username);
|
}
|
|
bool RegisterWindow::isPasswordValid(const QString &password)
|
{
|
// 密码长度6-16位,包含字母、数字和下划线
|
QRegExp regex("^[a-zA-Z0-9_]{6,16}$");
|
return regex.exactMatch(password);
|
}
|
|
bool RegisterWindow::isEmailValid(const QString &email)
|
{
|
// 邮箱格式验证
|
QRegExp regex("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$");
|
return regex.exactMatch(email);
|
}
|
|
bool RegisterWindow::isTelValid(const QString &tel)
|
{
|
// 手机号格式验证(简单验证11位数字)
|
QRegExp regex("^1[3-9]\\d{9}$");
|
return regex.exactMatch(tel);
|
}
|
|
bool RegisterWindow::isDeptValid(const QString &dept)
|
{
|
// 部门名称允许中文、字母、数字和下划线,长度1-16位
|
QRegExp regex("^[\u4e00-\u9fa5a-zA-Z0-9_]{1,16}$");
|
return regex.exactMatch(dept);
|
}
|