/* 自定义头文件 */
|
#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;
|
}
|