Skip to content

Commit

Permalink
修复了不能记住密码的bug,暂时不能在注册的时候就记住密码,需要在登陆之后才能记住
Browse files Browse the repository at this point in the history
  • Loading branch information
RaidenShogunShadow committed Jul 9, 2024
1 parent 253dcfe commit 2601f57
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 28 deletions.
31 changes: 6 additions & 25 deletions databasemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ bool DatabaseManager::initializeDatabase()

QSqlQuery query;

query.prepare("SELECT account, hashedPassword, avatar FROM Users");
query.prepare("SELECT account, hashedPassword FROM Users");
if(query.exec())
{
while (query.next()) {
Expand All @@ -36,21 +36,20 @@ bool DatabaseManager::initializeDatabase()
qWarning() << "Failed to get all accounts:" << query.lastError();
}

if (!query.exec("CREATE TABLE IF NOT EXISTS Users (id INTEGER PRIMARY KEY AUTOINCREMENT, account TEXT UNIQUE, hashedPassword TEXT, avatar TEXT)")) {
if (!query.exec("CREATE TABLE IF NOT EXISTS Users (id INTEGER PRIMARY KEY AUTOINCREMENT, account TEXT UNIQUE, hashedPassword TEXT)")) {
qWarning() << "Failed to create table:" << query.lastError();
return false;
}

return true;
}

bool DatabaseManager::insertUser(const QString &account, const QString &hashedPassword, const QString &avatar)
bool DatabaseManager::insertUser(const QString &account, const QString &hashedPassword)
{
QSqlQuery query;
query.prepare("INSERT INTO Users (account, hashedPassword, avatar) VALUES (:account, :hashedPassword, :avatar)");
query.prepare("INSERT INTO Users (account, hashedPassword) VALUES (:account, :hashedPassword )");
query.bindValue(":account", account);
query.bindValue(":hashedPassword", hashedPassword);
query.bindValue(":avatar", avatar);

if (!query.exec()) {
qWarning() << "Failed to insert user:" << query.lastError();
Expand All @@ -60,12 +59,11 @@ bool DatabaseManager::insertUser(const QString &account, const QString &hashedPa
return true;
}

bool DatabaseManager::updateUserInfo(const QString &account,const QString &newHashedPassword, const QString &newAvatar)
bool DatabaseManager::updateUserInfo(const QString &account,const QString &newHashedPassword)
{
QSqlQuery query;
query.prepare("UPDATE Users SET hashedPassword = :newHashedPassword, avatar = :newAvatar WHERE account = :account");
query.prepare("UPDATE Users SET hashedPassword = :newHashedPassword WHERE account = :account");
query.bindValue(":newHashedPassword", newHashedPassword);
query.bindValue(":newAvatar", newAvatar);
query.bindValue(":account", account);

if (!query.exec()) {
Expand Down Expand Up @@ -110,20 +108,3 @@ QPair<QString, QString> DatabaseManager::getUserPassword(const QString &account)
// 如果没有找到匹配的账号,返回空值
return userPassword;
}

QString DatabaseManager::getUserAvatar(const QString &account)
{
QSqlQuery query;
query.prepare("SELECT avatar FROM Users WHERE account = :account");
query.bindValue(":account", account);

if (query.exec()) {
if (query.next()) {
return query.value(0).toString();
}
} else {
qWarning() << "Failed to get user avatar:" << query.lastError();
}

return QString();
}
5 changes: 2 additions & 3 deletions databasemanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ class DatabaseManager : public QObject
QMap<QString,QString>accountPasswordMap_;
public:
explicit DatabaseManager(QObject *parent = nullptr);
bool updateUserInfo(const QString &account, const QString &newHashedPassword, const QString &newAvatar);
bool updateUserInfo(const QString &account, const QString &newHashedPassword);
bool initializeDatabase();
bool insertUser(const QString &account, const QString &hashedPassword, const QString &avatar = "E:\24SummerTraining\avatar_placeholder.png");
bool insertUser(const QString &account, const QString &hashedPassword);
QList<QString>getAllAccounts();
QPair<QString, QString>getUserPassword(const QString &account);
QString getUserAvatar(const QString &account);
};

#endif // DATABASEMANAGER_H

0 comments on commit 2601f57

Please sign in to comment.