增加登录功能

pull/1/head
wubw1656 2023-09-28 20:29:34 +08:00
parent 7aff8b40d2
commit eb2836e5a3
19 changed files with 1202 additions and 96 deletions

View File

@ -17,6 +17,7 @@ FILE(GLOB SDK_SRC
find_package(catkin REQUIRED COMPONENTS find_package(catkin REQUIRED COMPONENTS
rviz roscpp std_msgs rviz roscpp std_msgs
rosconsole sensor_msgs # node.cpp rosconsole sensor_msgs # node.cpp
rosbag
) )
@ -57,6 +58,7 @@ set(SRC_FILES
src/myviz.cpp src/myviz.cpp
src/main.cpp src/main.cpp
tools/color.cpp tools/color.cpp
src/loginDialog.cpp
) )
## Add the "myviz" executable and specify the list of source files we ## Add the "myviz" executable and specify the list of source files we
@ -68,7 +70,7 @@ add_executable(myviz ${SRC_FILES} )
add_executable(rplidarNode src/node.cpp ${SDK_SRC}) # laser add_executable(rplidarNode src/node.cpp ${SDK_SRC}) # laser
target_link_libraries(rplidarNode ${catkin_LIBRARIES}) target_link_libraries(rplidarNode ${catkin_LIBRARIES})
target_link_libraries(myviz -lcrypto)
## Link the myviz executable with whatever Qt libraries have been defined by ## Link the myviz executable with whatever Qt libraries have been defined by

Binary file not shown.

After

Width:  |  Height:  |  Size: 191 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 188 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 188 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 157 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 155 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 157 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 154 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 154 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 157 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 152 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 155 B

View File

@ -0,0 +1,5 @@
root 123!@
wbw 1234
## 待删除,保存原始用户密码
##

View File

@ -0,0 +1,2 @@
root p<><70><EFBFBD><08><><EFBFBD><EFBFBD>w<>:<3A><>R<EFBFBD>wH<77>pD<02><>#<23><><EFBFBD>6
wbw <03>gB<16><>\v<1E><><EFBFBD>U<EFBFBD>g<EFBFBD>6#ȳ<><C8B3>E<EFBFBD><13>x<EFBFBD><78>F<EFBFBD>

View File

@ -0,0 +1,206 @@
#include "loginDialog.h"
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QMessageBox>
#include<QDebug>
LoginDialog::LoginDialog(QWidget *parent):QDialog(parent)
{
userLabel = new QLabel(this);
userLabel->move(80, 80);
userLabel->setText(tr("用户名"));
userEditLine = new QLineEdit(this);
userEditLine->move(150, 80);
userEditLine->setPlaceholderText(tr("请输入用户名"));
pwdLabel = new QLabel(this);
pwdLabel->move(80, 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 = {"laser_topic", "open_file", "bag_progress"};
// to do read from file
createUser();
// userPasswords["root"] = hashPassword("root123");
// userPasswords["wbw"] = hashPassword("123");
}
void LoginDialog::createUser(){
std::ifstream inputFile("../Rviz/librviz_ws/src/librviz_tutorial/sources/user/user.txt");
// 第一次读写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("../Rviz/librviz_ws/src/librviz_tutorial/sources/user/user.txt");
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(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();
// //判断用户名和密码是否正确 todo 函数判断
// if (userEditLine->text().trimmed() == tr("tom") &&
// pwdEditLine->text() == tr("123456"))
// {
// for(int i=0 ; i<setting_name.size(); i++){
// QWidget *foundChild = parent()->findChild<QWidget*>(setting_name[i]);
// if (foundChild) {
// qDebug()<<"111" ;
// // 找到了指定的子组件 在这里可以对子组件进行操作
// foundChild->setEnabled(true);
// }else{
// qDebug()<<"第" << i+1 <<"组件未找到" ;
// }
// }
// QMessageBox::information(this, tr("登录成功"), tr("已解锁"), QMessageBox::Yes);
// // landed = 1;
// accept();
// }
// else
// {
// QMessageBox::warning(this, tr("登录失败"), tr("用户名或者密码错误"), QMessageBox::Yes);
// }
// userEditLine->clear();
// pwdEditLine->clear();
// userEditLine->setFocus();
// QMessageBox::information(this, tr("提示"), tr("已经登陆,请勿重新登录"), QMessageBox::Yes);
}
LoginDialog::~LoginDialog()
{
}

View File

@ -0,0 +1,53 @@
#ifndef LOGINDIALOG_H
#define LOGINDIALOG_H
#include <QDialog>
#include<iostream>
#include<vector>
#include <map>
#include <openssl/sha.h>
#include <fstream>
#include<sstream>
class QLabel;
class QLineEdit;
class QPushButton;
class LoginDialog : public QDialog
{
Q_OBJECT
public:
explicit LoginDialog(QWidget *parent = 0);
~LoginDialog();
int checkUser(const QString username, const QString pwd);
QString hashPassword(const QString& pwd);
std::string hashPassword(const std::string& password); // 重载
void createUser();
void writeHash(const std::vector<std::string> usernames, const std::vector<std::string> pwds);
void writeHash(const std::map<QString, QString>);
// 待解锁组件名
std::vector<QString> setting_name;
//槽函数
private Q_SLOTS:
void login();
private:
QLabel *userLabel;
QLabel *pwdLabel;
QLineEdit *userEditLine;
QLineEdit *pwdEditLine;
QPushButton *loginBtn;
QPushButton *exitBtn;
// 待解锁组件名
// std::vector<QString> setting_name;
// 用户表
std::map<QString, QString> userPasswords;
};
#endif // LOGINDIALOG_H

View File

@ -37,6 +37,7 @@
#include <ros/ros.h> #include <ros/ros.h>
#include "myviz.h" #include "myviz.h"
#include<iostream> #include<iostream>
#include<QDebug>
int main(int argc, char **argv) int main(int argc, char **argv)
{ {

File diff suppressed because it is too large Load Diff

View File

@ -29,17 +29,32 @@
#ifndef MYVIZ_H #ifndef MYVIZ_H
#define MYVIZ_H #define MYVIZ_H
#include <QWidget> #include <QWidget>
#include<QString> #include<QString>
#include <QGridLayout> #include <QGridLayout>
#include<ros/ros.h> #include<ros/ros.h>
#include"std_msgs/String.h" #include"std_msgs/String.h"
#include<thread> #include<thread>
#include <visualization_msgs/Marker.h>
#include<QTimer>
#include <QMutex>
#include <QProcess>
#include <QThread>
#include<QSlider>
#include <rosbag/bag.h>
#include <sensor_msgs/LaserScan.h>
#include <rosbag/view.h>
#include"loginDialog.h"
namespace rviz namespace rviz
{ {
class Display; class Display;
class RenderPanel; class RenderPanel;
class VisualizationManager; class VisualizationManager;
class ToolManager;
class PropertyTreeWidget;
} }
// BEGIN_TUTORIAL // BEGIN_TUTORIAL
@ -52,6 +67,8 @@ public:
virtual ~MyViz(); virtual ~MyViz();
void subCallback(const std_msgs::String& msg); void subCallback(const std_msgs::String& msg);
void pubThread(); void pubThread();
void PlayCircle(int index); // 圆圈线程执行函数
rosbag::View *view;
private Q_SLOTS: // QT信号和槽 private Q_SLOTS: // QT信号和槽
void setThickness( int thickness_percent ); void setThickness( int thickness_percent );
void setCellSize( int cell_size_percent ); void setCellSize( int cell_size_percent );
@ -64,18 +81,78 @@ private Q_SLOTS: // QT信号和槽
void GridColorChanged(int index); // 格栅颜色 void GridColorChanged(int index); // 格栅颜色
void setFixedFrame(const QString &FixedFrame); void setFixedFrame(const QString &FixedFrame);
void setBackgroundColor(const QString& bg_color); // 背景颜色 void setBackgroundColor(const QString& bg_color); // 背景颜色
void AxesDisplayChanged(int index); // axe
void CircleDisplayChanged(int index); // circle
void Tree_Display(QGridLayout* layout, int index); void Tree_Display(QGridLayout* layout, int index);
void login_button_clicked();
void quit_login_button_clicked();
void slot_select(); // 切换至select模式
void slot_move_camera();
// off
double extractDuration(const QByteArray& output);
void selectFile();
void SpeedChanged(int index);
void openFile();
private: private:
rviz::VisualizationManager* manager_; // rviz::VisualizationManager* manager_; //
rviz::RenderPanel* render_panel_; rviz::RenderPanel* render_panel_;
rviz::ToolManager* tool_manager_;
rviz::Display* grid_; rviz::Display* grid_;
rviz::Display* cloud; rviz::Display* cloud;
rviz::Display* laser; rviz::Display* laser_;
rviz::Display* axes_display;
rviz::Display* marker_;
rviz::PropertyTreeWidget* tree_widget_ ;
ros::NodeHandle nh; ros::NodeHandle nh;
ros::Subscriber sub; ros::Subscriber sub;
ros::Publisher pub; ros::Publisher pub;
std::thread pub_thread; std::thread pub_thread;
std::vector<QString> color_name; // 颜色的名称集合 std::vector<QString> color_name; // 颜色的名称集合
QString color_icon_filename;
LoginDialog* dlg;
bool landed;
QString cur_fixed_frame;
// off
bool isclicked;// 用于判断play第几次按下play键
bool ispauseclick;// 用于判断暂停键第几次按下
int count;// 用于记录timeout的次数
bool sliderreleased;// 用于判断是否拖动了进度条
bool playagain;// 用于检测是否多次按下play按钮
bool isthreadplaying;// 用于标识线程是否在运行
bool abs;// 用于单独开线程?
bool stored;// 用于存储一次容器,只在第一次play的时候存储容器
bool is_pause = false;
bool is_reset = false;
bool is_end = false;
bool is_save = false;
bool is_saveMul = false;
bool is_bagClosed = true;
bool is_rateChanged = false;
bool is_openFile = false;
bool is_setFrameIndex = false;
bool is_saveMulFinish = false;
bool end = false;
bool threadout = false;
bool ischanged = false;
bool ismoved = false;
double speed_rate;
QSlider* progress;
QString filePath;
double duration;
int remain;
int prog_num = 0;
std::vector<QString> speed_name;
ros::Publisher scan_pub;
std::vector<sensor_msgs::LaserScan::ConstPtr> indx;// 存储节点信息
int countbag;// 存储播放的具体位置
double indxsize;
int test = 0;
}; };
// END_TUTORIAL // END_TUTORIAL
#endif // MYVIZ_H #endif // MYVIZ_H