You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
4.0 KiB
122 lines
4.0 KiB
#ifndef BACKEND_INTERFACE_H
|
|
#define BACKEND_INTERFACE_H
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
#include <memory>
|
|
#include <map>
|
|
#include <ctime>
|
|
#include "../../src/exam_system/exam_system.h"
|
|
|
|
namespace exam_system {
|
|
|
|
// 题目信息结构体
|
|
struct QuestionInfo {
|
|
int id;
|
|
std::string content;
|
|
std::vector<std::string> options;
|
|
int correct_answer; // 0-3 对应ABCD
|
|
};
|
|
|
|
// 测试结果结构体
|
|
struct TestResult {
|
|
int total_questions; ///< 总问题数
|
|
int correct_answers; ///< 正确问题数
|
|
double score; ///< 得分
|
|
std::string difficulty;
|
|
};
|
|
|
|
// 验证码信息结构体
|
|
struct VerificationCode {
|
|
std::string code;
|
|
std::time_t generate_time;
|
|
std::string email;
|
|
};
|
|
|
|
// 邮箱配置结构体
|
|
struct EmailConfig {
|
|
std::string smtp_server = "smtp.163.com";
|
|
int smtp_port = 25;
|
|
std::string username = "your_email@163.com";
|
|
std::string password = "your_password";
|
|
bool use_curl = true; // 使用libcurl发送邮件
|
|
};
|
|
|
|
// 前后端通信接口
|
|
class BackendInterface {
|
|
public:
|
|
virtual ~BackendInterface() = default;
|
|
|
|
virtual bool SendVerificationCode(const std::string& email) = 0;
|
|
virtual bool UserRegister(const std::string& username,
|
|
const std::string& password,
|
|
const std::string& email,
|
|
const std::string& code) = 0;
|
|
virtual bool UserLogin(const std::string& username,
|
|
const std::string& password) = 0;
|
|
virtual void UserLogout() = 0;
|
|
virtual bool ChangePassword(const std::string& old_password,
|
|
const std::string& new_password) = 0;
|
|
virtual void UserExit() = 0;
|
|
virtual std::vector<QuestionInfo> GenerateQuestions(
|
|
const std::string& difficulty, int count) = 0;
|
|
virtual TestResult SubmitAnswers(const std::vector<int>& user_answers) = 0;
|
|
virtual std::string GetCurrentUser() const = 0;
|
|
};
|
|
|
|
class BackendImpl : public BackendInterface {
|
|
public:
|
|
BackendImpl();
|
|
~BackendImpl() override = default;
|
|
|
|
BackendImpl(const BackendImpl&) = delete;
|
|
BackendImpl& operator=(const BackendImpl&) = delete;
|
|
|
|
bool SendVerificationCode(const std::string& email) override;
|
|
bool UserRegister(const std::string& username,
|
|
const std::string& password,
|
|
const std::string& email,
|
|
const std::string& code) override;
|
|
bool UserLogin(const std::string& username,
|
|
const std::string& password) override;
|
|
void UserLogout() override;
|
|
bool ChangePassword(const std::string& old_password,
|
|
const std::string& new_password) override;
|
|
void UserExit() override;
|
|
std::vector<QuestionInfo> GenerateQuestions(
|
|
const std::string& difficulty, int count) override;
|
|
TestResult SubmitAnswers(const std::vector<int>& user_answers) override;
|
|
std::string GetCurrentUser() const override;
|
|
|
|
private:
|
|
// 邮件发送方法
|
|
bool SendEmail(const std::string& recipient,
|
|
const std::string& subject,
|
|
const std::string& body);
|
|
bool SendEmailViaCurl(const std::string& recipient,
|
|
const std::string& subject,
|
|
const std::string& body);
|
|
|
|
// 验证码管理
|
|
std::string GenerateVerificationCode();
|
|
bool ValidateVerificationCode(const std::string& email,
|
|
const std::string& code);
|
|
void CleanExpiredVerificationCodes();
|
|
|
|
// 题目生成辅助方法
|
|
std::string CalculateSimpleAnswer(const std::string& problem);
|
|
std::vector<std::string> GenerateOptions(const std::string& correct_answer);
|
|
double CalculateScore(int correct_count, int total_count);
|
|
|
|
// 成员变量
|
|
std::map<std::string, VerificationCode> verification_codes_;
|
|
std::map<std::string, std::vector<QuestionInfo>> user_questions_;
|
|
std::string current_user_;
|
|
std::vector<std::string> current_correct_answers_;
|
|
std::unique_ptr<ExamSystem> exam_system_;
|
|
EmailConfig email_config_;
|
|
};
|
|
|
|
} // namespace exam_system
|
|
|
|
#endif // BACKEND_INTERFACE_H
|