Compare commits

..

No commits in common. 'main' and 'bojingchi_branch' have entirely different histories.

@ -1 +1,2 @@
#MathTestGeneratorPro
# MathTestGeneratorPro

@ -1,217 +0,0 @@
#include "EmailSender.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <windows.h>
bool EmailSender::sendVerificationCode(const std::string& email, const std::string& code) {
// 首先尝试PowerShell方案如果失败则尝试其他方案
std::cout << "=== 开始发送验证码邮件 ===" << std::endl;
std::cout << "收件人: " << email << std::endl;
std::cout << "验证码: " << code << std::endl;
// 尝试PowerShell方案
if (sendByPowerShell(email, code)) {
return true;
}
std::cout << "PowerShell方案失败尝试CURL方案..." << std::endl;
// 如果PowerShell失败尝试CURL方案
if (sendByCurl(email, code)) {
return true;
}
std::cout << "所有邮件发送方案均失败" << std::endl;
return false;
}
bool EmailSender::sendByPowerShell(const std::string& email, const std::string& code) {
// 配置发送方邮箱信息 - 修改为你的真实信息
const std::string sender_email = "3675658020@qq.com"; // 修改为你的QQ邮箱
const std::string sender_password = "lsagdbxwfolhcheb"; // 修改为QQ邮箱授权码
std::cout << "尝试使用PowerShell发送邮件..." << std::endl;
std::cout << "发件人: " << sender_email << std::endl;
// 创建PowerShell脚本
std::string psScript =
"# 数学学习软件邮件发送脚本\n"
"# 发件人配置\n"
"$SMTPServer = \"smtp.qq.com\"\n"
"$SMTPPort = \"587\"\n"
"$Username = \"" + sender_email + "\"\n"
"$Password = \"" + sender_password + "\"\n"
"$EmailFrom = \"" + sender_email + "\"\n"
"$EmailTo = \"" + email + "\"\n"
"$Subject = \"数学学习软件 - 注册验证码\"\n"
"$Body = @\"\n"
"欢迎注册数学学习软件!\n"
"\n"
"您的注册验证码是:" + code + "\n"
"\n"
"验证码有效期为10分钟请尽快使用。\n"
"\n"
"如果您没有请求此验证码,请忽略此邮件。\n"
"\n"
"谢谢!\n"
"数学学习软件团队\n"
"\"@\n"
"\n"
"Write-Host \"正在发送邮件到: $EmailTo\"\n"
"Write-Host \"验证码: " + code + "\"\n"
"\n"
"try {\n"
" # 创建邮件消息\n"
" $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom, $EmailTo, $Subject, $Body)\n"
" \n"
" # 创建SMTP客户端\n"
" $SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer, $SMTPPort)\n"
" $SMTPClient.EnableSsl = $true\n"
" $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Username, $Password)\n"
" \n"
" # 发送邮件\n"
" $SMTPClient.Send($SMTPMessage)\n"
" \n"
" Write-Host \"邮件发送成功!\" -ForegroundColor Green\n"
" Write-Output \"SUCCESS\"\n"
" \n"
"} catch {\n"
" Write-Host \"邮件发送失败: $($_.Exception.Message)\" -ForegroundColor Red\n"
" Write-Output \"ERROR: $($_.Exception.Message)\"\n"
"}";
// 保存PowerShell脚本
std::ofstream scriptFile("send_email.ps1");
if (!scriptFile) {
std::cout << "创建PowerShell脚本文件失败" << std::endl;
return false;
}
scriptFile << psScript;
scriptFile.close();
std::cout << "正在执行PowerShell脚本..." << std::endl;
// 执行PowerShell脚本
std::string command = "powershell -ExecutionPolicy Bypass -File send_email.ps1";
FILE* pipe = _popen(command.c_str(), "r");
if (!pipe) {
std::cout << "执行PowerShell命令失败" << std::endl;
return false;
}
char buffer[256];
std::string result = "";
while (!feof(pipe)) {
if (fgets(buffer, sizeof(buffer), pipe) != NULL)
result += buffer;
}
_pclose(pipe);
// 记录发送日志
std::ofstream logFile("email_send_log.txt", std::ios::app);
if (logFile) {
logFile << "=== 邮件发送记录 ===" << std::endl;
logFile << "时间: " << __DATE__ << " " << __TIME__ << std::endl;
logFile << "方案: PowerShell" << std::endl;
logFile << "发件人: " << sender_email << std::endl;
logFile << "收件人: " << email << std::endl;
logFile << "验证码: " << code << std::endl;
logFile << "执行结果: " << result << std::endl;
logFile << "=========================" << std::endl << std::endl;
logFile.close();
}
// 检查执行结果
if (result.find("SUCCESS") != std::string::npos) {
std::cout << "? 邮件发送成功!" << std::endl;
std::cout << "验证码已发送到: " << email << std::endl;
return true;
} else {
std::cout << "? PowerShell发送失败: " << result << std::endl;
return false;
}
}
bool EmailSender::sendByCurl(const std::string& email, const std::string& code) {
// 配置发送方邮箱信息 - 修改为你的真实信息
const std::string sender_email = "3675658020@qq.com"; // 修改为你的QQ邮箱
const std::string sender_password = "lsagdbxwfolhcheb"; // 修改为QQ邮箱授权码
std::cout << "尝试使用CURL发送邮件..." << std::endl;
// 检查系统是否安装curl
int curlCheck = system("curl --version > nul 2>&1");
if (curlCheck != 0) {
std::cout << "系统未安装curl跳过此方案" << std::endl;
return false;
}
std::cout << "检测到curl开始发送..." << std::endl;
// 创建邮件内容文件
std::ofstream mailFile("email_content.txt");
if (!mailFile) {
std::cout << "创建邮件内容文件失败" << std::endl;
return false;
}
mailFile << "From: 数学学习软件 <" << sender_email << ">\n";
mailFile << "To: " << email << "\n";
mailFile << "Subject: 数学学习软件 - 注册验证码\n";
mailFile << "Content-Type: text/plain; charset=\"utf-8\"\n";
mailFile << "\n";
mailFile << "欢迎注册数学学习软件!\n";
mailFile << "\n";
mailFile << "您的注册验证码是:" << code << "\n";
mailFile << "\n";
mailFile << "验证码有效期为10分钟请尽快使用。\n";
mailFile << "\n";
mailFile << "如果您没有请求此验证码,请忽略此邮件。\n";
mailFile << "\n";
mailFile << "谢谢!\n";
mailFile << "数学学习软件团队\n";
mailFile.close();
// 使用curl发送邮件
std::string curlCommand =
"curl -s --url \"smtp://smtp.qq.com:587\" "
"--ssl-reqd "
"--mail-from \"" + sender_email + "\" "
"--mail-rcpt \"" + email + "\" "
"--user \"" + sender_email + ":" + sender_password + "\" "
"--upload-file email_content.txt "
"> curl_output.txt 2>&1";
std::cout << "执行CURL命令..." << std::endl;
int result = system(curlCommand.c_str());
// 记录发送日志
std::ofstream logFile("email_send_log.txt", std::ios::app);
if (logFile) {
logFile << "=== CURL邮件发送记录 ===" << std::endl;
logFile << "时间: " << __DATE__ << " " << __TIME__ << std::endl;
logFile << "方案: CURL" << std::endl;
logFile << "发件人: " << sender_email << std::endl;
logFile << "收件人: " << email << std::endl;
logFile << "验证码: " << code << std::endl;
logFile << "命令返回码: " << result << std::endl;
logFile << "=========================" << std::endl << std::endl;
logFile.close();
}
if (result == 0) {
std::cout << "? CURL邮件发送成功" << std::endl;
return true;
} else {
std::cout << "? CURL邮件发送失败返回码: " << result << std::endl;
return false;
}
}
bool EmailSender::sendBySMTP(const std::string& email, const std::string& code) {
// 这个函数保留作为备用方案,当前不使用
std::cout << "SMTP方案暂不可用" << std::endl;
return false;
}

@ -1,16 +0,0 @@
#ifndef EMAILSENDER_H
#define EMAILSENDER_H
#include <string>
class EmailSender {
public:
static bool sendVerificationCode(const std::string& email, const std::string& code);
private:
static bool sendByPowerShell(const std::string& email, const std::string& code);
static bool sendByCurl(const std::string& email, const std::string& code);
static bool sendBySMTP(const std::string& email, const std::string& code);
};
#endif

Binary file not shown.

@ -1,135 +0,0 @@
#include "LoginWindow.h"
#include "UserManager.h"
#include "RegisterWindow.h"
#include "SelectionWindow.h"
#include <string>
#include <sstream>
HWND LoginWindow::hwnd = NULL;
HWND LoginWindow::hEmail = NULL;
HWND LoginWindow::hPassword = NULL;
HWND LoginWindow::hMessage = NULL;
void LoginWindow::Show() {
// 如果窗口已存在,先销毁
if (hwnd != NULL) {
DestroyWindow(hwnd);
hwnd = NULL;
}
WNDCLASSEX wc;
memset(&wc, 0, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.hInstance = GetModuleHandle(NULL);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = "LoginWindow";
RegisterClassEx(&wc);
hwnd = CreateWindowEx(0, "LoginWindow", "数学学习软件 - 登录",
WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
NULL, NULL, GetModuleHandle(NULL), NULL);
if (!hwnd) {
MessageBox(NULL, "创建登录窗口失败", "错误", MB_OK | MB_ICONERROR);
return;
}
CreateControls();
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
}
void LoginWindow::CreateControls() {
// 标题
CreateWindow("STATIC", "数学学习软件", WS_VISIBLE | WS_CHILD | SS_CENTER,
10, 20, 360, 30, hwnd, NULL, GetModuleHandle(NULL), NULL);
// 邮箱标签和输入框
CreateWindow("STATIC", "邮箱:", WS_VISIBLE | WS_CHILD,
50, 70, 50, 25, hwnd, NULL, GetModuleHandle(NULL), NULL);
hEmail = CreateWindow("EDIT", "", WS_VISIBLE | WS_CHILD | WS_BORDER,
100, 70, 200, 25, hwnd, NULL, GetModuleHandle(NULL), NULL);
// 密码标签和输入框
CreateWindow("STATIC", "密码:", WS_VISIBLE | WS_CHILD,
50, 110, 50, 25, hwnd, NULL, GetModuleHandle(NULL), NULL);
hPassword = CreateWindow("EDIT", "", WS_VISIBLE | WS_CHILD | WS_BORDER | ES_PASSWORD,
100, 110, 200, 25, hwnd, NULL, GetModuleHandle(NULL), NULL);
// 登录按钮
CreateWindow("BUTTON", "登录", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
100, 150, 80, 30, hwnd, (HMENU)1, GetModuleHandle(NULL), NULL);
// 注册按钮
CreateWindow("BUTTON", "注册", WS_VISIBLE | WS_CHILD,
200, 150, 80, 30, hwnd, (HMENU)2, GetModuleHandle(NULL), NULL);
// 消息标签 - 修复这里:将 WM_CHILD 改为 WS_CHILD
hMessage = CreateWindow("STATIC", "", WS_VISIBLE | WS_CHILD | SS_CENTER,
50, 190, 300, 25, hwnd, NULL, GetModuleHandle(NULL), NULL);
}
LRESULT CALLBACK LoginWindow::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_COMMAND: {
int cmd = LOWORD(wParam);
if (cmd == 1) { // 登录按钮
OnLogin();
} else if (cmd == 2) { // 注册按钮
OnRegister();
}
break;
}
case WM_CLOSE:
DestroyWindow(hwnd);
PostQuitMessage(0); // 只有关闭登录窗口时才退出程序
break;
case WM_DESTROY:
// 不在WM_DESTROY中调用PostQuitMessage避免程序退出
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
void LoginWindow::OnLogin() {
char email[100], password[100];
GetWindowText(hEmail, email, sizeof(email));
GetWindowText(hPassword, password, sizeof(password));
std::string strEmail(email);
std::string strPassword(password);
if (strEmail.empty() || strPassword.empty()) {
SetWindowText(hMessage, "请输入邮箱和密码");
return;
}
UserManager& userManager = UserManager::getInstance();
if (userManager.login(strEmail, strPassword)) {
SetWindowText(hMessage, "登录成功!");
// 延迟后切换到选择界面
Sleep(800);
DestroyWindow(hwnd);
hwnd = NULL; // 重置窗口句柄
// 显示选择界面
SelectionWindow::Show();
} else {
SetWindowText(hMessage, "登录失败,请检查邮箱和密码");
}
}
void LoginWindow::OnRegister() {
DestroyWindow(hwnd);
hwnd = NULL;
RegisterWindow::Show();
}

@ -1,23 +0,0 @@
#ifndef LOGINWINDOW_H
#define LOGINWINDOW_H
#include <windows.h>
class LoginWindow {
public:
static void Show();
static HWND GetHandle() { return hwnd; }
private:
static HWND hwnd;
static HWND hEmail;
static HWND hPassword;
static HWND hMessage;
static void CreateControls();
static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
static void OnLogin();
static void OnRegister();
};
#endif

Binary file not shown.

@ -1,225 +0,0 @@
#include "RegisterWindow.h"
#include "UserManager.h"
#include "LoginWindow.h"
#include <string>
#include <sstream>
#include <windows.h>
HWND RegisterWindow::hwnd = NULL;
HWND RegisterWindow::hEmail = NULL;
HWND RegisterWindow::hCode = NULL;
HWND RegisterWindow::hPassword = NULL;
HWND RegisterWindow::hConfirmPassword = NULL;
HWND RegisterWindow::hMessage = NULL;
std::string RegisterWindow::generatedCode = "";
void RegisterWindow::Show() {
// 如果窗口已存在,先销毁
if (hwnd != NULL) {
DestroyWindow(hwnd);
hwnd = NULL;
}
WNDCLASSEX wc;
memset(&wc, 0, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.hInstance = GetModuleHandle(NULL);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = "RegisterWindow";
RegisterClassEx(&wc);
hwnd = CreateWindowEx(0, "RegisterWindow", "数学学习软件 - 注册",
WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT, 400, 450,
NULL, NULL, GetModuleHandle(NULL), NULL);
if (!hwnd) {
MessageBox(NULL, "创建注册窗口失败", "错误", MB_OK | MB_ICONERROR);
return;
}
CreateControls();
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
}
void RegisterWindow::CreateControls() {
// 标题
CreateWindow("STATIC", "用户注册", WS_VISIBLE | WS_CHILD | SS_CENTER,
10, 20, 360, 30, hwnd, NULL, GetModuleHandle(NULL), NULL);
// 邮箱标签和输入框
CreateWindow("STATIC", "邮箱:", WS_VISIBLE | WS_CHILD,
50, 70, 50, 25, hwnd, NULL, GetModuleHandle(NULL), NULL);
hEmail = CreateWindow("EDIT", "", WS_VISIBLE | WS_CHILD | WS_BORDER,
100, 70, 200, 25, hwnd, NULL, GetModuleHandle(NULL), NULL);
// 获取验证码按钮
CreateWindow("BUTTON", "获取验证码", WS_VISIBLE | WS_CHILD,
100, 110, 200, 30, hwnd, (HMENU)1, GetModuleHandle(NULL), NULL);
// 验证码标签和输入框
CreateWindow("STATIC", "验证码:", WS_VISIBLE | WS_CHILD,
50, 160, 50, 25, hwnd, NULL, GetModuleHandle(NULL), NULL);
hCode = CreateWindow("EDIT", "", WS_VISIBLE | WS_CHILD | WS_BORDER,
100, 160, 200, 25, hwnd, NULL, GetModuleHandle(NULL), NULL);
// 密码标签和输入框
CreateWindow("STATIC", "密码:", WS_VISIBLE | WS_CHILD,
50, 200, 50, 25, hwnd, NULL, GetModuleHandle(NULL), NULL);
hPassword = CreateWindow("EDIT", "", WS_VISIBLE | WS_CHILD | WS_BORDER | ES_PASSWORD,
100, 200, 200, 25, hwnd, NULL, GetModuleHandle(NULL), NULL);
// 确认密码标签和输入框
CreateWindow("STATIC", "确认密码:", WS_VISIBLE | WS_CHILD,
30, 230, 70, 25, hwnd, NULL, GetModuleHandle(NULL), NULL);
hConfirmPassword = CreateWindow("EDIT", "", WS_VISIBLE | WS_CHILD | WS_BORDER | ES_PASSWORD,
100, 230, 200, 25, hwnd, NULL, GetModuleHandle(NULL), NULL);
// 密码要求提示
CreateWindow("STATIC", "密码要求: 6-10位包含大小写字母和数字",
WS_VISIBLE | WS_CHILD | SS_CENTER,
50, 260, 300, 25, hwnd, NULL, GetModuleHandle(NULL), NULL);
// 注册按钮
CreateWindow("BUTTON", "注册", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
100, 300, 80, 30, hwnd, (HMENU)2, GetModuleHandle(NULL), NULL);
// 返回按钮
CreateWindow("BUTTON", "返回", WS_VISIBLE | WS_CHILD,
200, 300, 80, 30, hwnd, (HMENU)3, GetModuleHandle(NULL), NULL);
// 消息标签
hMessage = CreateWindow("STATIC", "", WS_VISIBLE | WS_CHILD | SS_CENTER,
50, 340, 300, 25, hwnd, NULL, GetModuleHandle(NULL), NULL);
}
LRESULT CALLBACK RegisterWindow::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_COMMAND: {
int cmd = LOWORD(wParam);
if (cmd == 1) { // 获取验证码
OnGetCode();
} else if (cmd == 2) { // 注册按钮
OnRegister();
} else if (cmd == 3) { // 返回按钮
OnBack();
}
break;
}
case WM_CLOSE:
DestroyWindow(hwnd);
LoginWindow::Show();
break;
case WM_DESTROY:
// 不在WM_DESTROY中调用PostQuitMessage
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
void RegisterWindow::OnGetCode() {
char email[100];
GetWindowText(hEmail, email, sizeof(email));
std::string strEmail(email);
if (strEmail.empty()) {
SetWindowText(hMessage, "请输入邮箱地址");
return;
}
UserManager& userManager = UserManager::getInstance();
if (!userManager.validateEmailFormat(strEmail)) {
SetWindowText(hMessage, "邮箱格式不正确");
return;
}
if (userManager.isEmailRegistered(strEmail)) {
SetWindowText(hMessage, "该邮箱已注册");
return;
}
// 显示发送中提示
SetWindowText(hMessage, "正在发送验证码,请稍候...");
// 生成并发送验证码(不显示在界面上)
generatedCode = userManager.generateRegistrationCode(strEmail);
if (!generatedCode.empty()) {
SetWindowText(hMessage, "验证码已发送到您的邮箱,请查收!");
// 清空验证码输入框,让用户手动输入
SetWindowText(hCode, "");
// 设置焦点到验证码输入框
SetFocus(hCode);
} else {
SetWindowText(hMessage, "邮件发送失败,请检查邮箱地址或重试");
generatedCode = ""; // 清空验证码
}
}
void RegisterWindow::OnRegister() {
char email[100], code[100], password[100], confirmPassword[100];
GetWindowText(hEmail, email, sizeof(email));
GetWindowText(hCode, code, sizeof(code));
GetWindowText(hPassword, password, sizeof(password));
GetWindowText(hConfirmPassword, confirmPassword, sizeof(confirmPassword));
std::string strEmail(email);
std::string strCode(code);
std::string strPassword(password);
std::string strConfirmPassword(confirmPassword);
// 验证输入
if (strEmail.empty() || strCode.empty() || strPassword.empty() || strConfirmPassword.empty()) {
SetWindowText(hMessage, "请填写所有字段");
return;
}
// 验证验证码
if (strCode != generatedCode) {
SetWindowText(hMessage, "验证码不正确");
return;
}
// 验证密码一致性
if (strPassword != strConfirmPassword) {
SetWindowText(hMessage, "两次输入的密码不一致");
return;
}
UserManager& userManager = UserManager::getInstance();
// 验证密码格式
if (!userManager.validatePasswordFormat(strPassword)) {
SetWindowText(hMessage, "密码格式不符合要求");
return;
}
// 注册用户
if (registerUserWithPassword(strEmail, strPassword)) {
SetWindowText(hMessage, "注册成功!正在跳转到登录页面...");
Sleep(2000); // 等待2秒显示成功消息
OnBack();
} else {
SetWindowText(hMessage, "注册失败,请重试");
}
}
bool RegisterWindow::registerUserWithPassword(const std::string& email, const std::string& password) {
UserManager& userManager = UserManager::getInstance();
return userManager.registerUserWithPassword(email, password);
}
void RegisterWindow::OnBack() {
DestroyWindow(hwnd);
hwnd = NULL; // 重置窗口句柄
LoginWindow::Show();
}

@ -1,30 +0,0 @@
#ifndef REGISTERWINDOW_H
#define REGISTERWINDOW_H
#include <windows.h>
#include <string>
class RegisterWindow {
public:
static void Show();
static HWND GetHandle() { return hwnd; }
private:
static HWND hwnd;
static HWND hEmail;
static HWND hCode;
static HWND hPassword;
static HWND hConfirmPassword;
static HWND hMessage;
static std::string generatedCode;
static void CreateControls();
static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
static void OnGetCode();
static void OnRegister();
static void OnBack();
static bool registerUserWithPassword(const std::string& email, const std::string& password);
};
#endif

Binary file not shown.

@ -1,24 +0,0 @@
#include "User.h"
User::User(const std::string& email, const std::string& password, const std::string& userType)
: email(email), password(password), userType(userType) {}
std::string User::getEmail() const {
return email;
}
std::string User::getPassword() const {
return password;
}
std::string User::getUserType() const {
return userType;
}
void User::setPassword(const std::string& newPassword) {
password = newPassword;
}
bool User::validatePassword(const std::string& password) const {
return this->password == password;
}

@ -1,23 +0,0 @@
#ifndef USER_H
#define USER_H
#include <string>
class User {
public:
User(const std::string& email, const std::string& password, const std::string& userType);
std::string getEmail() const;
std::string getPassword() const;
std::string getUserType() const;
void setPassword(const std::string& newPassword);
bool validatePassword(const std::string& password) const;
private:
std::string email;
std::string password;
std::string userType;
};
#endif

Binary file not shown.

@ -1,164 +0,0 @@
#include "UserManager.h"
#include "User.h"
#include "EmailSender.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cctype>
#include <cstdlib>
#include <ctime>
#include <windows.h>
#include <sstream>
UserManager::UserManager() {
currentUser = NULL;
srand(static_cast<unsigned int>(time(NULL)));
loadUsers();
}
UserManager& UserManager::getInstance() {
static UserManager instance;
return instance;
}
bool UserManager::validateEmailFormat(const std::string& email) {
size_t atPos = email.find('@');
size_t dotPos = email.find('.');
return atPos != std::string::npos && dotPos != std::string::npos && atPos < dotPos;
}
bool UserManager::validatePasswordFormat(const std::string& password) {
if (password.length() < 6 || password.length() > 10) return false;
bool hasUpper = false, hasLower = false, hasDigit = false;
for (size_t i = 0; i < password.length(); ++i) {
char c = password[i];
if (isupper(c)) hasUpper = true;
if (islower(c)) hasLower = true;
if (isdigit(c)) hasDigit = true;
}
return hasUpper && hasLower && hasDigit;
}
std::string UserManager::generateRandomCode(int length) {
const std::string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::string code;
for (int i = 0; i < length; ++i) {
code += chars[rand() % chars.size()];
}
return code;
}
bool UserManager::sendEmail(const std::string& email, const std::string& code) {
// 使用EmailSender类发送邮件
return EmailSender::sendVerificationCode(email, code);
}
std::string UserManager::generateRegistrationCode(const std::string& email) {
std::string code = generateRandomCode(6); // 6位验证码
// 发送邮件
if (sendEmail(email, code)) {
return code;
} else {
return ""; // 发送失败返回空字符串
}
}
bool UserManager::registerUser(const std::string& email) {
if (isEmailRegistered(email)) return false;
std::string defaultPassword = "Temp123";
users.push_back(User(email, defaultPassword, "elementary"));
saveUsers();
return true;
}
bool UserManager::registerUserWithPassword(const std::string& email, const std::string& password) {
if (isEmailRegistered(email)) return false;
// 验证密码格式
if (!validatePasswordFormat(password)) {
return false;
}
users.push_back(User(email, password, "elementary"));
saveUsers();
return true;
}
bool UserManager::login(const std::string& email, const std::string& password) {
for (size_t i = 0; i < users.size(); ++i) {
User& user = users[i];
if (user.getEmail() == email && user.validatePassword(password)) {
currentUser = &user;
return true;
}
}
return false;
}
bool UserManager::changePassword(const std::string& email, const std::string& oldPassword, const std::string& newPassword) {
for (size_t i = 0; i < users.size(); ++i) {
User& user = users[i];
if (user.getEmail() == email && user.validatePassword(oldPassword)) {
if (validatePasswordFormat(newPassword)) {
user.setPassword(newPassword);
saveUsers();
return true;
}
}
}
return false;
}
bool UserManager::isEmailRegistered(const std::string& email) {
for (size_t i = 0; i < users.size(); ++i) {
const User& user = users[i];
if (user.getEmail() == email) {
return true;
}
}
return false;
}
void UserManager::loadUsers() {
std::ifstream file("data/users.txt");
if (!file) return;
std::string line;
while (std::getline(file, line)) {
size_t pos1 = line.find(',');
size_t pos2 = line.find(',', pos1 + 1);
if (pos1 != std::string::npos && pos2 != std::string::npos) {
std::string email = line.substr(0, pos1);
std::string password = line.substr(pos1 + 1, pos2 - pos1 - 1);
std::string userType = line.substr(pos2 + 1);
users.push_back(User(email, password, userType));
}
}
}
void UserManager::saveUsers() {
// 创建数据目录
CreateDirectory("data", NULL);
std::ofstream file("data/users.txt");
for (size_t i = 0; i < users.size(); ++i) {
const User& user = users[i];
file << user.getEmail() << "," << user.getPassword() << "," << user.getUserType() << "\n";
}
}
User* UserManager::getCurrentUser() {
return currentUser;
}
void UserManager::logout() {
currentUser = NULL;
}

@ -1,36 +0,0 @@
#ifndef USERMANAGER_H
#define USERMANAGER_H
#include "User.h"
#include <vector>
#include <string>
class UserManager {
public:
static UserManager& getInstance();
bool registerUser(const std::string& email);
bool registerUserWithPassword(const std::string& email, const std::string& password);
bool login(const std::string& email, const std::string& password);
bool changePassword(const std::string& email, const std::string& oldPassword, const std::string& newPassword);
User* getCurrentUser();
void logout();
// 修改方法签名添加email参数
std::string generateRegistrationCode(const std::string& email);
std::string generateRandomCode(int length);
bool isEmailRegistered(const std::string& email);
bool validatePasswordFormat(const std::string& password);
bool validateEmailFormat(const std::string& email);
void loadUsers();
void saveUsers();
private:
UserManager();
std::vector<User> users;
User* currentUser;
bool sendEmail(const std::string& email, const std::string& code);
};
#endif

Binary file not shown.

@ -1,3 +0,0 @@
? 成功发送验证码到: 3675658020@qq.com | 验证码: OP9R0N | 时间: 22:45:10
? 成功发送验证码到: 3675658020@qq.com | 验证码: B3XMEI | 时间: 22:45:10
? 成功发送验证码到: 3675658020@qq.com | 验证码: BG5ZSN | 时间: 22:45:10

@ -1,13 +0,0 @@
=== 邮件发送记录 ===
时间: Oct 7 2025 23:35:05
方案: PowerShell
发件人: 3675658020@qq.com
收件人: 2470975401@qq.com
验证码: ZH4RXI
执行结果: 正在发送邮件到: 2470975401@qq.com
验证码: ZH4RXI
邮件发送成功!
SUCCESS
=========================

@ -1,44 +0,0 @@
# 数学学习软件邮件发送脚本
# 发件人配置
$SMTPServer = "smtp.qq.com"
$SMTPPort = "587"
$Username = "3675658020@qq.com"
$Password = "lsagdbxwfolhcheb"
$EmailFrom = "3675658020@qq.com"
$EmailTo = "2470975401@qq.com"
$Subject = "数学学习软件 - 注册验证码"
$Body = @"
ZH4RXI
10使
"@
Write-Host "正在发送邮件到: $EmailTo"
Write-Host "验证码: ZH4RXI"
try {
# 创建邮件消息
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom, $EmailTo, $Subject, $Body)
# 创建SMTP客户端
$SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer, $SMTPPort)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Username, $Password)
# 发送邮件
$SMTPClient.Send($SMTPMessage)
Write-Host "邮件发送成功!" -ForegroundColor Green
Write-Output "SUCCESS"
} catch {
Write-Host "邮件发送失败: $($_.Exception.Message)" -ForegroundColor Red
Write-Output "ERROR: $($_.Exception.Message)"
}
Loading…
Cancel
Save