Browse Source

修改管理员账号

chengxr 1 year ago
parent
commit
660157d003

+ 28 - 3
QFD/dbService/ClassSet.cpp

@@ -131,7 +131,32 @@ int QFUser::logout()
 
     delete m_currentUser;
     m_currentUser = nullptr;
-    return QF_CODE_COMPLETED;
+    return QF_CODE_SUCCEEDED;
+}
+
+int QFUser::resetAdmin(QString account, QString password, QString repeatPassword)
+{
+    qDebug() << account << password << repeatPassword << (password == repeatPassword);
+    if (account.isEmpty()) {
+        return QF_CODE_EMPTY_ACCOUNT;
+    } else if (password.isEmpty()) {
+        return QF_CODE_EMPTY_PASSWORD;
+    } else if (password != repeatPassword) {
+        return QF_CODE_PASSWORD_NOT_SAME;
+    } else if (password == rawPassword) {
+        return QF_CODE_PASSWORD_UNCHANGED;
+    }
+
+    QString md5str;
+    QByteArray md5bytes = QCryptographicHash::hash(password.toLatin1(), QCryptographicHash::Md5);
+    md5str.prepend(md5bytes.toHex());
+
+    bool ret = DBServiceSet().UpdateUserById(account, md5str, id);
+    if (ret) {
+        return QF_CODE_SUCCEEDED;
+    } else {
+        return QF_CODE_DATA_ERROR;
+    }
 }
 
 int QFUser::login()
@@ -141,7 +166,7 @@ int QFUser::login()
     }
 
     if (userNo.isEmpty()) {
-        return QF_CODE_EMPTY_USERNAME;
+        return QF_CODE_EMPTY_ACCOUNT;
     }
 
     if (rawPassword.isEmpty()) {
@@ -164,5 +189,5 @@ int QFUser::login()
         return QF_CODE_WRONG_PASSWORD;
     }
 
-    return QF_CODE_COMPLETED;
+    return QF_CODE_SUCCEEDED;
 }

+ 14 - 9
QFD/dbService/ClassSet.h

@@ -5,15 +5,18 @@
 
 #include <QDate>
 
-const int QF_CODE_COMPLETED     = 1000;
+const int QF_CODE_SUCCEEDED     = 1000;
 const int QF_CODE_ALREADY_LOGIN = 1001;
 
-const int QF_CODE_FAILED          = 2000;
-const int QF_CODE_EMPTY_USERNAME  = 2001;
-const int QF_CODE_EMPTY_PASSWORD  = 2002;
-const int QF_CODE_USER_NOT_EXISTS = 2003;
-const int QF_CODE_WRONG_PASSWORD  = 2004;
-const int QF_CODE_NOT_LOGIN       = 2005;
+const int QF_CODE_FAILED             = 2000;
+const int QF_CODE_EMPTY_ACCOUNT      = 2001;
+const int QF_CODE_EMPTY_PASSWORD     = 2002;
+const int QF_CODE_USER_NOT_EXISTS    = 2003;
+const int QF_CODE_WRONG_PASSWORD     = 2004;
+const int QF_CODE_NOT_LOGIN          = 2005;
+const int QF_CODE_PASSWORD_NOT_SAME  = 2006;
+const int QF_CODE_PASSWORD_UNCHANGED = 2007;
+const int QF_CODE_DATA_ERROR         = 2008;
 
 /**
  * @projectName   QFD
@@ -101,9 +104,11 @@ public:
 
     static QFUser *currentUser();
 
-    int login();
+    int login();  // 登录
 
-    static int logout();
+    static int logout();  // 退出
+
+    int resetAdmin(QString account, QString password, QString repeatPassword);  // 修改管理员账号
 
 private:
     QString rawPassword;

+ 21 - 12
QFD/view/LoginView.cpp

@@ -57,21 +57,21 @@ void LoginView::connectSignalsAndSlots()
 {
     connect(m_loginWidget, &LoginWidget::signalLogin, this, &LoginView::slotLogin);
     connect(m_loginWidget, &LoginWidget::signalCancel, this, &LoginView::slotCancelLogin);
-    connect(m_registerWidget, &RegisterWidget::signalConfirm, this, &LoginView::slotConfirmAcc);
-    connect(m_registerWidget, &RegisterWidget::signalCancel, this, &LoginView::slotCancelAcc);
+    connect(m_registerWidget, &RegisterWidget::signalConfirm, this, &LoginView::slotConfirmAccount);
+    connect(m_registerWidget, &RegisterWidget::signalCancel, this, &LoginView::slotCancelAccount);
     connect(m_stackedWidget, &QStackedWidget::currentChanged, this, &LoginView::slotStackIndexChanged);
 }
 
 /// 提示登录错误信息
 void LoginView::showAlertWithCode(int code) const
 {
-    if (code == QF_CODE_COMPLETED) {
+    if (code == QF_CODE_SUCCEEDED) {
         return;
     }
 
     QString msg;
 
-    if (code == QF_CODE_EMPTY_USERNAME) {
+    if (code == QF_CODE_EMPTY_ACCOUNT) {
         msg = "账号不能为空";
     } else if (code == QF_CODE_EMPTY_PASSWORD) {
         msg = "密码不能为空";
@@ -79,6 +79,12 @@ void LoginView::showAlertWithCode(int code) const
         msg = "用户不存在";
     } else if (code == QF_CODE_WRONG_PASSWORD) {
         msg = "密码错误";
+    } else if (code == QF_CODE_PASSWORD_NOT_SAME) {
+        msg = "两次输入密码不一致,请修改后重试";
+    } else if (code == QF_CODE_PASSWORD_UNCHANGED) {
+        msg = "不能与旧密码相同,请修改后重试";
+    } else if (code == QF_CODE_DATA_ERROR) {
+        msg = "数据错误";
     }
 
     if (!msg.isEmpty()) {
@@ -96,7 +102,7 @@ void LoginView::slotLogin()
 
     qDebug() << __FUNCTION__ << __LINE__ << "login code:" << code;
 
-    if ((code == QF_CODE_COMPLETED || code == QF_CODE_ALREADY_LOGIN) && QFUser::currentUser() != nullptr) {
+    if ((code == QF_CODE_SUCCEEDED || code == QF_CODE_ALREADY_LOGIN) && QFUser::currentUser() != nullptr) {
         m_loginWidget->saveAccountConfig();  // 登录成功后存储登录设置
 
         if (QFDConfig::roleType() == QFUser::SuperAdmin) {
@@ -113,20 +119,23 @@ void LoginView::slotLogin()
 
 void LoginView::slotCancelLogin()
 {
-    qDebug() << __FUNCTION__ << __LINE__;
-    setStackIndex(m_stackedWidget->currentIndex() + 1);
     emit signalCancelLogin();
 }
 
-void LoginView::slotConfirmAcc()
+void LoginView::slotConfirmAccount()
 {
-    qDebug() << __FUNCTION__ << __LINE__;
-    setStackIndex(m_stackedWidget->currentIndex() + 1);
+    QString account        = m_registerWidget->account();
+    QString password       = m_registerWidget->password();
+    QString repeatPassword = m_registerWidget->repeatPassword();
+    QFUser *user           = QFUser::currentUser();
+
+    int code = user->resetAdmin(account, password, repeatPassword);
+    qDebug() << __FUNCTION__ << __LINE__ << "reset code:" << code;
+    showAlertWithCode(code);
 }
 
-void LoginView::slotCancelAcc()
+void LoginView::slotCancelAccount()
 {
-    qDebug() << __FUNCTION__ << __LINE__;
     setStackIndex(m_stackedWidget->currentIndex() + 1);
 }
 

+ 2 - 2
QFD/view/LoginView.h

@@ -36,8 +36,8 @@ private:
 private slots:
     void slotLogin();
     void slotCancelLogin();
-    void slotConfirmAcc();
-    void slotCancelAcc();
+    void slotConfirmAccount();
+    void slotCancelAccount();
     void slotStackIndexChanged();
 
 private:

+ 15 - 0
QFD/widgets/RegisterWidget.cpp

@@ -13,6 +13,21 @@ RegisterWidget::RegisterWidget(QWidget *parent) : QWidget(parent)
     connectSignalsAndSlots();
 }
 
+const QString RegisterWidget::account() const
+{
+    return m_accLineEdit->text();
+}
+
+const QString RegisterWidget::password() const
+{
+    return m_pwLineEdit->text();
+}
+
+const QString RegisterWidget::repeatPassword() const
+{
+    return m_pw2LineEdit->text();
+}
+
 void RegisterWidget::initialize()
 {
     m_vBoxLayout = new QVBoxLayout(this);

+ 4 - 0
QFD/widgets/RegisterWidget.h

@@ -17,6 +17,10 @@ class RegisterWidget : public QWidget
 public:
     explicit RegisterWidget(QWidget *parent = nullptr);
 
+    const QString account() const;
+    const QString password() const;
+    const QString repeatPassword() const;
+
 private:
     void initialize();
     void initLayout();