#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; // 验证失败
|
}
|