unknown
2025-09-10 a4fc45393e242f15517f9347f95e55c77887b332
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
/* 自定义头文件 */
#include "login.h"
#include "ui_login.h"
#include "textvalidator.h"
 
/* 系统头文键 */
#include <QMessageBox>
 
/* 预处理指令·编译指示 */
#pragma execution_character_set("utf-8")
 
/* 构造函数1 */
Login::Login(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);
}
 
/* 构造函数2 */
Login::Login(QTcpSocket *client, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);
 
    /* 初始化错误信息提示 */
    ui->Label_error->setText("");
 
    /* 为登录窗口安装事件过滤器 */
    this->installEventFilter(this);
 
    /* 设置密码框禁用输入法切换,只能使用英文输入法 */
    ui->PasswordEdit->setAttribute(Qt::WA_InputMethodEnabled, false);
 
    /* 获得公告socket */
    m_client = client;
 
    m_register = new Register(m_client,this);
 
    connect(m_register,SIGNAL(goto_login_signal()),this,SLOT(show_login_slot()));
 
    //这里的“parent,SIGNAL(login_callback_signal(QString))”,没有提醒,但是能用【AI说,这个方法太旧】
    connect(parent,SIGNAL(login_callback_signal(QString)),this,SLOT(login_callback_slot(QString)));
}
 
/* 析构函数 */
Login::~Login()
{
    delete ui;
}
 
/* 事件过滤器· */
bool Login::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_LoginButton_clicked();
                return true;
            }
        }
    }
    return QObject::eventFilter(obj, event);
}
 
/* 槽·登录->注册 */
void Login::on_RegisterButton_clicked()
{
    this->hide();
    m_register->show();
 
    /* 切换时,一定要清空本界面的错误信息 */
    ui->Label_error->setText("");
    /* 切换时,也要清空本界面的输入信息 */
}
 
/* 槽·注册->登录 */
void Login::show_login_slot()
{
    this->show();
    m_register->hide();
}
 
/* 槽·点击登录 */
void Login::on_LoginButton_clicked()
{
    QString username = ui->UserNameEdit->text();
    QString password = ui->PasswordEdit->text();
 
    /* 用户名输入框不能为空 */
    if(username == ""){
        QString text = "用户名不能为空!";
        ui->Label_error->clear();
        ui->Label_error->setText(text);
        return;
    }
 
    /* 密码输入框不能为空 */
    if(password == ""){
        QString text = "密码不能为空!";
        ui->Label_error->clear();
        ui->Label_error->setText(text);
        return;
    }
 
    /* 检查用户名格式【汉字、数字、英文字符、下划线-长度(4~10)】 */
    if( !(TextValidator::isValidUsername(username)) ){
        QString text = "用户名格式错误 ";
        ui->Label_error->clear();
        ui->Label_error->setText(text);
        return;
    }
 
    /* 检查密码【英文字符、数字、下划线、点“.”-长度(5~20)】 */
    if( !(TextValidator::isValidPassword(password)) ){
        QString text = "密码格式错误 ";
        ui->Label_error->clear();
        ui->Label_error->setText(text);
        return;
    }
 
    /* 如果全部正常,清空错误信息 */
    ui->Label_error->setText("");
 
    /* 提取字符串后,网络构建请求结构体 */
    struct LoginReq req;
    strcpy(req.user_name,username.toLocal8Bit().data());
    strcpy(req.password,password.toLocal8Bit().data());
 
    //登录信息的内容检查+【加密】,发给服务端,我们再收报【解密】检查,做出相应的动作
 
    /* 如果是正常连接,立即发送登录请求包 */
    if(m_client){
        qDebug()<<"LoginReq len :"<<m_client->write((char *)&req,req.head.len);
    }
}
 
void Login::login_callback_slot(QString str)
{
    ui->Label_error->setText(str);
    QMessageBox::information(this,"Server",str);
}