point-cloud-visualization/point_visual/src/loginDialog.cpp

182 lines
5.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "loginDialog.h"
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QMessageBox>
#include<QDebug>
LoginDialog::LoginDialog(QWidget *parent):QDialog(parent)
{
this->setFixedSize(380, 300);
userLabel = new QLabel(this);
userLabel->move(70, 80);
userLabel->setText(tr("用户名"));
userEditLine = new QLineEdit(this);
userEditLine->move(150, 80);
userEditLine->setPlaceholderText(tr("请输入用户名"));
pwdLabel = new QLabel(this);
pwdLabel->move(70, 130);
pwdLabel->setText(tr("密码"));
pwdEditLine = new QLineEdit(this);
pwdEditLine->move(150, 130);
pwdEditLine->setPlaceholderText(tr("请输入密码"));
pwdEditLine->setEchoMode(QLineEdit::Password);
loginBtn = new QPushButton(this);
loginBtn->move(60, 200);
loginBtn->setText(tr("登录"));
exitBtn = new QPushButton(this);
exitBtn->move(220, 200);
exitBtn->setText(tr("退出"));
connect(loginBtn, &QPushButton::clicked, this, &LoginDialog::login);
connect(exitBtn, &QPushButton::clicked, this, &LoginDialog::close);
// 待解锁的组件名称
setting_name = {"cloud_topic", "laser_topic", "open_file", "bag_progress", "fixd_frame_topic"};
// 账号路径
user_file_name = "../PointVisual/src/point_visual/sources/user/user.txt";
// 创建用户表存下
createUser();
// userPasswords["root"] = hashPassword("root123");
// userPasswords["wbw"] = hashPassword("123");
}
void LoginDialog::createUser(){
std::ifstream inputFile(user_file_name);
// 第一次读写user.txt将user加密后建立userPasswords
if(inputFile.is_open()){
std::string line;
while(std::getline(inputFile, line)){
std::istringstream iss(line);
std::string username, pwd;
if(iss>> username && iss>> pwd){
if(pwd.size() <= 8){
// 根据长度判断是否已经被加密
pwd = hashPassword(pwd);
}
// 创建map/用户表
userPasswords[QString::fromStdString(username)] = QString::fromStdString(pwd);
}
}
inputFile.close();
}else{
qDebug() << "Unable to open the user.txt.";
}
writeHash(userPasswords);
}
// 加密后的账户密码写入user.txt
void LoginDialog::writeHash(const std::map<QString, QString> user){
std::ofstream file(user_file_name);
if(file.is_open()){
for(auto iter = user.begin(); iter != user.end() ; iter++){
file << (iter->first).toStdString() << " ";
file << (iter->second).toStdString() << std::endl;
}
file.close();
}else{
qDebug()<<"Unable to write hashcode!!!";
}
}
// hash加密函数
QString LoginDialog::hashPassword(const QString& pwd) {
std::string password = pwd.toStdString();
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, password.c_str(), password.size());
SHA256_Final(hash, &sha256);
std::string hashedPassword;
for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) {
hashedPassword += hash[i];
}
return QString(hashedPassword.c_str());
}
// 重载加密函数
std::string LoginDialog::hashPassword(const std::string& password) {
// std::string password = pwd.toStdString();
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, password.c_str(), password.size());
SHA256_Final(hash, &sha256);
std::string hashedPassword;
for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) {
hashedPassword += hash[i];
}
return hashedPassword;
}
// 判断用户账号返回不同code
int LoginDialog::checkUser(const QString username, const QString pwd){
auto iter = userPasswords.find(username);
if(iter != userPasswords.end()){
if(pwd.size() > 8) { return 1; }
if(iter->second == hashPassword(pwd)){
// 用户密码都对
return 2;
}else{
// 用户对,密码不对
return 1;
}
}else {
// 用户不对
return 0;
}
}
// 槽函数
void LoginDialog::login()
{
int flag = checkUser(userEditLine->text().trimmed(), pwdEditLine->text());
switch(flag){
case 0: {
QMessageBox::warning(this, tr("登录失败"), tr("用户名错误"), QMessageBox::Ok);
break;
}
case 1: {
QMessageBox::warning(this, tr("登录失败"), tr("密码错误"), QMessageBox::Ok);
break;
}
case 2:{
for(int i=0 ; i<setting_name.size(); i++){
QWidget *foundChild = parent()->findChild<QWidget*>(setting_name[i]);
if (foundChild) {
// 找到了指定的子组件 在这里可以对子组件进行操作
foundChild->setEnabled(true);
}else{
qDebug()<<"" << i+1 <<"组件未找到" ;
}
}
QMessageBox::information(this, tr("登录成功"), tr("已解锁"), QMessageBox::Ok);
accept();
}
}
userEditLine->clear();
pwdEditLine->clear();
userEditLine->setFocus();
}
LoginDialog::~LoginDialog()
{
}