Browse Source

Login & config

chengxr 1 year ago
parent
commit
968c92f392
6 changed files with 84 additions and 18 deletions
  1. 2 0
      QFD/QFD.pro
  2. 6 0
      QFD/common/QFDConfig.cpp
  3. 36 0
      QFD/common/QFDConfig.h
  4. 24 11
      QFD/dbService/ClassSet.cpp
  5. 9 3
      QFD/dbService/ClassSet.h
  6. 7 4
      QFD/view/LoginView.cpp

+ 2 - 0
QFD/QFD.pro

@@ -60,6 +60,7 @@ INCLUDEPATH += $$PWD/widgets
 INCLUDEPATH += $$PWD/common
 
 SOURCES += \
+    common/QFDConfig.cpp \
     common/QFDIcon.cpp \
     main.cpp \
     view/AboutView.cpp \
@@ -88,6 +89,7 @@ SOURCES += \
     widgets/SchemeWidget.cpp
 
 HEADERS += \
+    common/QFDConfig.h \
     common/QFDIcon.h \
     view/AboutView.h \
     view/EvaluateView.h \

+ 6 - 0
QFD/common/QFDConfig.cpp

@@ -0,0 +1,6 @@
+#include "QFDConfig.h"
+
+QFDConfig::QFDConfig(QObject *parent) : QObject(parent)
+{
+
+}

+ 36 - 0
QFD/common/QFDConfig.h

@@ -0,0 +1,36 @@
+#ifndef QFDCONFIG_H
+#define QFDCONFIG_H
+
+#include <QObject>
+
+class QFDConfig : public QObject
+{
+    Q_OBJECT
+public:
+    explicit QFDConfig(QObject *parent = nullptr);
+
+    static const QString username();
+    static void setUsername(QString const username);
+
+    static const QString password();
+    static void setPassword(QString const password);
+
+    static const QString rememberPassword();
+    static void setRememberPassword(QString const rememberPassword);
+
+    static const QString roleType();
+    static void setRoleType(QString const roleType);
+
+    static const QString techMessaureConfig();
+    static void setTechMessaureConfig(QString const techMessaureConfig);
+
+    static const QString dbPath();
+    static void setDbPath(QString const dbPath);
+
+    static const QString project();
+    static void setProject(QString const project);
+
+signals:
+};
+
+#endif  // QFDCONFIG_H

+ 24 - 11
QFD/dbService/ClassSet.cpp

@@ -3,6 +3,7 @@
 #include "DBServiceSet.h"
 
 #include <QCryptographicHash>
+#include <QSettings>
 
 #include <QDebug>
 
@@ -111,37 +112,49 @@ ClassSet::ClassSet()
     datas.append(testData11);
 }
 
-static QFUser m_currentUser;
+static QFUser *m_currentUser = nullptr;
 
 QFUser::QFUser() { }
 
-QFUser::QFUser(const QString userId, const QString password) : userNo(userId), password(password) { }
+QFUser::QFUser(const QString userId, const QString password) : userNo(userId), rawPassword(password) { }
 
-QFUser QFUser::currentUser()
+QFUser *QFUser::currentUser()
 {
     return m_currentUser;
 }
 
+int QFUser::logout()
+{
+    if (m_currentUser == nullptr) {
+        return QF_CODE_NOT_LOGIN;
+    }
+
+    delete m_currentUser;
+    m_currentUser = nullptr;
+    return QF_CODE_COMPLETED;
+}
+
 int QFUser::login()
 {
+    if (m_currentUser != nullptr) {
+        return QF_CODE_ALREADY_LOGIN;
+    }
+
     if (!DBServiceSet().QueryUserByNo(this, userNo)) {
         return QF_CODE_USER_NOT_EXISTS;
     }
 
     QCryptographicHash ch(QCryptographicHash::Md5);
     QString md5str;
-    QByteArray md5bytes = QCryptographicHash::hash(password.toLatin1(), QCryptographicHash::Md5);
+    QByteArray md5bytes = QCryptographicHash::hash(rawPassword.toLatin1(), QCryptographicHash::Md5);
     md5str.prepend(md5bytes.toHex());
 
-    if (!DBServiceSet().QueryUserByNoAndPassword(this, userNo, md5str)) {
+    m_currentUser = new QFUser(userNo, rawPassword);
+    if (!DBServiceSet().QueryUserByNoAndPassword(m_currentUser, userNo, md5str)) {
+        delete m_currentUser;
+        m_currentUser = nullptr;
         return QF_CODE_WRONG_PASSWORD;
     }
 
-    m_currentUser = *this;
-    return QF_CODE_COMPLETED;
-}
-
-int QFUser::logout()
-{
     return QF_CODE_COMPLETED;
 }

+ 9 - 3
QFD/dbService/ClassSet.h

@@ -5,10 +5,13 @@
 
 #include <QDate>
 
-const int QF_CODE_COMPLETED       = 1000;
+const int QF_CODE_COMPLETED     = 1000;
+const int QF_CODE_ALREADY_LOGIN = 1001;
+
 const int QF_CODE_FAILED          = 2000;
 const int QF_CODE_USER_NOT_EXISTS = 2001;
 const int QF_CODE_WRONG_PASSWORD  = 2002;
+const int QF_CODE_NOT_LOGIN       = 2003;
 
 /**
  * @projectName   QFD
@@ -94,11 +97,14 @@ public:
 
     QFUser(const QString userId, const QString password);
 
-    static QFUser currentUser();
+    static QFUser *currentUser();
 
     int login();
 
-    int logout();
+    static int logout();
+
+private:
+    QString rawPassword;
 };
 
 /**

+ 7 - 4
QFD/view/LoginView.cpp

@@ -86,10 +86,13 @@ void LoginView::slotLogin()
 {
     if (verifyLoginInfo()) {
 
-        QFUser *user = new QFUser(m_loginWidget->account(), m_loginWidget->password());
-        int code     = user->login();
-        qDebug() << __FUNCTION__ << m_loginWidget->account() << m_loginWidget->password() << __LINE__ << code;
-        delete user;
+        QFUser user(m_loginWidget->account(), m_loginWidget->password());
+        int code = user.login();
+        qDebug() << __FUNCTION__ << __LINE__ << code;
+
+        if (QFUser::currentUser() != nullptr) {
+            qDebug() << __FUNCTION__ << __LINE__ << QFUser::currentUser()->userName;
+        }
 
         //        setStackIndex(m_stackedWidget->currentIndex() + 1);
         //        emit signalLogin();