lt18717377960
2025-09-09 1553eef7c835ddd9c273bc879711f49e2a09a803
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* 自定义头文件 */
#include "register.h"
#include "ui_register.h"
#include "textvalidator.h"
 
/* 系统头文键 */
#include <QMessageBox>
 
/* 预处理指令·编译指示 */
#pragma execution_character_set("utf-8")
 
/* 构造函数1 */
Register::Register(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Register)
{
    ui->setupUi(this);
}
 
 
/* 构造函数2 */
Register::Register(QTcpSocket *clinet,QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Register)
{
    ui->setupUi(this);
 
    /* 初始化错误信息 */
    ui->error1->setText("");
    ui->error2->setText("");
    ui->error3->setText("");
    ui->error4->setText("");
    ui->error5->setText("");
 
    /* 为注册窗口安装事件过滤器 */
    this->installEventFilter(this);
 
    /* 设置密码框禁用输入法切换,只能使用英文输入法 */
    ui->PasswordEdit->setAttribute(Qt::WA_InputMethodEnabled, false);
    ui->PasswordEdit2->setAttribute(Qt::WA_InputMethodEnabled, false);
    ui->EmailEdit->setAttribute(Qt::WA_InputMethodEnabled, false);
    ui->TelephoneEdit->setAttribute(Qt::WA_InputMethodEnabled, false);
 
    //这里的“parent,SIGNAL(login_callback_signal(QString))”,没有提醒,但是能用【AI说,这个方法太旧】
    connect(parent->parent(),SIGNAL(register_callback_signal(bool,QString,int)),this,SLOT(register_callback_slot(bool,QString,int)));
 
    m_client = clinet;
}
 
/* 析构函数 */
Register::~Register()
{
    delete ui;
}
 
/* 事件过滤器· */
bool Register::eventFilter(QObject *obj, QEvent *event)
{
    if(obj == this){//当触发事件来自于这个界面
        if(event->type() == QEvent::KeyPress){//当事件类型是按压按键
            QKeyEvent * keyEvent = (QKeyEvent *)event;
 
            /* 当按压下enter键【有两个enter】 */
            if((keyEvent->key() == Qt::Key_Return)||(keyEvent->key() == Qt::Key_Enter)){
                on_RegisterButton_clicked();
                return true;
            }
        }
    }
    return QObject::eventFilter(obj, event);
}
 
 
/* 槽·点击发送信号·注册->登录 */
void Register::on_GoBackButton_clicked()
{
    /* 切换时,一定要清空本界面的错误信息 */
    ui->error1->setText("");
    ui->error2->setText("");
    ui->error3->setText("");
    ui->error4->setText("");
    ui->error5->setText("");
    /* 切换时,也要清空本界面的输入信息 */
 
    emit goto_login_signal();
}
 
/* 槽·点击注册 */
void Register::on_RegisterButton_clicked()
{
 
    QString username = ui->UserNameEdit->text();
    QString password = ui->PasswordEdit->text();
    QString password2 = ui->PasswordEdit2->text();//第二次输入的密码
    QString email = ui->EmailEdit->text();
    QString telephone = ui->TelephoneEdit->text();
    QString department = ui->comboBox_department->currentText();
 
    /* 输入框不能为空 */
    if( !(disableEmpty(username,password,password2,email,telephone)) ){
        return;
    }
 
    /* 格式检查 */
    /* 检查用户名格式【汉字、数字、英文字符、下划线-长度(4~10)】 */
    if( !(TextValidator::isValidUsername(username)) ){
        QString text = "用户名格式错误 ";
        ui->error1->clear();
        ui->error1->setText(text);
        return;
    }
 
    /* 检查密码【英文字符、数字、下划线、点“.”-长度(5~20)】 */
    if( !(TextValidator::isValidPassword(password)) ){
        QString text = "密码格式错误 ";
        ui->error2->clear();
        ui->error2->setText(text);
        return;
    }
 
    /* 检查邮箱地址【···】 */
    if( !(TextValidator::isValidEmail(email)) ){
        QString text = "邮箱地址格式错误 ";
        ui->error4->clear();
        ui->error4->setText(text);
        return;
    }
 
    /* 检查电话号码【···】 */
    if( !(TextValidator::isValidTelephone(telephone)) ){
        QString text = "电话号码格式错误 ";
        ui->error5->clear();
        ui->error5->setText(text);
        return;
    }
 
    /* 密码二次一致检查 */
    if(password != password2) {
        ui->error3->setText("错误:两次密码输入不一致!");
        return;
    }
 
    /* 检验合格,清空错误信息 */
    ui->error1->setText("");
    ui->error2->setText("");
    ui->error3->setText("");
    ui->error4->setText("");
    ui->error5->setText("");
 
    /* 填充注册请求包 */
    struct RegisterReq req;
    strcpy(req.user_name,username.toLocal8Bit().data());
    strcpy(req.password,password.toLocal8Bit().data());
    strcpy(req.email,email.toLocal8Bit().data());
    strcpy(req.telephone,telephone.toLocal8Bit().data());
    strcpy(req.department,department.toLocal8Bit().data());
 
    //注册信息的内容检查+【加密】,发给服务端,我们再收报【解密】检查,做出相应的动作
 
    /* 如果是正常连接,立即发送注册请求包 */
    if(m_client){//如果连接正常,立即发送
        qDebug()<<"RegisterReq len :"<<m_client->write((char *)&req,req.head.len);
    }
 
    //测试用途
    qDebug()<<"用户名:"<<username;
    qDebug()<<"密码:"<<password;
    qDebug()<<"邮箱:"<<email;
    qDebug()<<"电话:"<<telephone;
    qDebug()<<"部门:"<<department;
}
 
/* 注册反馈 */
void Register::register_callback_slot(bool flag, QString str, int id)
{
    if(flag == false){//注册失败,弹窗提醒
        QMessageBox::critical(this,"Server",str);
        return;
    }else {
        QString str = " 注册成功!即将返回登录界面。。。";
        str = "用户ID:" + QString::number(id) + str;
        QMessageBox::information(this,"Server",str);
        //跳转界面
        emit goto_login_signal();
    }
}
 
/* 批量判空检查 */
bool Register::disableEmpty(QString username,QString password,QString password2,QString email,QString telephone)
{
    /* 清空错误信息 */
    ui->error1->setText("");
    ui->error2->setText("");
    ui->error3->setText("");
    ui->error4->setText("");
    ui->error5->setText("");
 
    if(username == ""){
        QString text = "用户名不能为空!";
        ui->error1->clear();
        ui->error1->setText(text);
        return false;
    }
    if(password == ""){
        QString text = "密码不能为空!";
        ui->error2->clear();
        ui->error2->setText(text);
        return false;
    }
    if(password2 == ""){
        QString text = "确认密码不能为空!";
        ui->error2->clear();
        ui->error2->setText(text);
        return false;
    }
    if(email == ""){
        QString text = "邮箱地址不能为空!";
        ui->error4->clear();
        ui->error4->setText(text);
        return false;
    }
    if(telephone == ""){
        QString text = "电话号码不能为空!";
        ui->error5->clear();
        ui->error5->setText(text);
        return false;
    }
    return true;
}