240717班级,工业化控制系统,煤矿相关行业,昆仑系统
小驴在此and
2024-10-30 459fd941045fc145b515e1ddd6f0d145f99bcfa2
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //登录
    ui->usernameLineEdit->setPlaceholderText("请输入工号");
    ui->passwordLineEdit->setPlaceholderText("请输入密码");
    ui->passwordLineEdit->setEchoMode(QLineEdit::Password);
 
 
    //注册
    ui->usernameLineEdit_2->setPlaceholderText("请输入用户名");
    ui->passwordLineEdit_2->setPlaceholderText("请输入新密码");
    ui->repasswordLineEdit->setPlaceholderText("再次确认密码");
    ui->passwordLineEdit_2->setEchoMode(QLineEdit::Password);
    ui->repasswordLineEdit->setEchoMode(QLineEdit::Password);
    ui->emailLineEdit->setPlaceholderText("请输入电子邮箱");
    ui->telephoneLineEdit->setPlaceholderText("请输入手机号");
    //设置输入长度限制为11
    QIntValidator *inValidator = new QIntValidator(1,11,this);
    ui->telephoneLineEdit->setValidator(inValidator);
    //只允许输入数字
    QRegExp rx("^[0-9]*$");
    QRegExpValidator *regExpValidator = new QRegExpValidator(rx,this);
    ui->telephoneLineEdit->setValidator(regExpValidator);
 
    //重置密码
    ui->email->setPlaceholderText("请输入电子邮箱");
    ui->CAPTCHI->setPlaceholderText("请输入验证码");
    ui->password_2->setPlaceholderText("请输入密码");
    ui->password_3->setPlaceholderText("请确认密码");
    ui->password_2->setEchoMode(QLineEdit::Password);
    ui->password_3->setEchoMode(QLineEdit::Password);
 
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
 
void MainWindow::on_loginPushButton_clicked()//登录界面登录按钮
{
    QString username = ui->usernameLineEdit->text();
    QString password = ui->passwordLineEdit->text();
    if(username==0||password==0){
        QMessageBox::warning(this, "输入错误", "用户名或密码不能为空!");//显示错误消息
    }else if(validateInput(username, password)){/* 输入校验逻辑 */
        QByteArray hashedPassword = hashPassword(password);
        // 从数据库或文件获取存储的用户名和加密密码
        if (verifyUserData(username, hashedPassword)) {/* 输入校验逻辑 */
            QMessageBox::information(this, "登录成功", "欢迎回来," + username + "!");
            // 根据用户权限加载不同界面
        } else {
            QMessageBox::warning(this, "登录失败", "用户名或密码错误!");//显示错误消息
        }
    }else{
         QMessageBox::warning(this, "输入错误", "用户名或密码不合法!");//显示错误消息
    }
}
 
void MainWindow::on_registerPushButton_2_clicked()//注册界面注册按钮
{
    QString username_2 = ui->usernameLineEdit_2->text();
    QString password_2 = ui->passwordLineEdit_2->text();
 
    if (validateInput(username_2, password_2)) {/*输入校验逻辑*/
        QByteArray hashedPassword = hashPassword(password_2);
        //存储用户名和加密密码到数据库或文件
        saveUserData(username_2, hashedPassword);
        QMessageBox::information(this, "注册成功", "用户注册成功!");
    } else {
        QMessageBox::warning(this, "输入错误", "用户名或密码不合法!");//显示错误信息
    }
}
 
void MainWindow::on_registerPushButton_clicked()//登录界面注册按钮
{
    ui->stackedWidget->setCurrentIndex(1);
}
 
void MainWindow::on_forgotPushButton_clicked()//登录界面忘记密码按钮
{
    ui->stackedWidget->setCurrentIndex(2);
}
 
void MainWindow::on_toolButton_clicked()//注册界面返回登录按钮
{
    ui->stackedWidget->setCurrentIndex(0);
}
 
void MainWindow::on_toolButton_2_clicked()//重设密码界面返回登录按钮
{
    ui->stackedWidget->setCurrentIndex(0);
}
 
void MainWindow::on_repasswordPushButton_clicked()//重设密码按钮
{
 
}
 
QByteArray MainWindow::hashPassword(const QString &password)
{
    return QCryptographicHash::hash(password.toUtf8(), QCryptographicHash::Sha256);
}
 
bool MainWindow::validateInput(const QString &username, const QString &password)
{
    // 校验用户名和密码的长度和合法性
    QRegExp usernameRegex("^[a-zA-Z0-9_]{3,15}$"); // 用户名范围3-15个字符
    QRegExp passwordRegex("^[a-zA-Z0-9@#$%^&+=]{6,20}$"); // 密码范围6-20个字符
 
    return usernameRegex.exactMatch(username) && passwordRegex.exactMatch(password);
 
}
 
void MainWindow::saveUserData(const QString &username, const QByteArray &hashedPassword)
{
    QFile file("userdata.txt");
    if (file.open(QIODevice::Append | QIODevice::Text)) {
        QTextStream out(&file);
        out << username << "\n" << hashedPassword.toHex() << "\n"; // 存储用户名和哈希密码
        file.close();
    }
}
 
bool MainWindow::verifyUserData(const QString &username, const QByteArray &hashedPassword)
{
    QFile file("userdata.txt");
    if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QTextStream in(&file);
        while (!in.atEnd()) {
            QString storedUsername = in.readLine();
            QString storedPassword = in.readLine();
 
            if (storedUsername == username && storedPassword == hashedPassword.toHex()) {
                file.close();
                return true; // 验证成功
            }
        }
        file.close();
    }
    return false; // 验证失败
}