ywl
2025-07-02 c7df7556e073d5e3953d17f94c35517f1ee48410
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#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);
}