|
|
|
|
@ -1,117 +1,122 @@
|
|
|
|
|
#ifndef DATA_STRUCTURES_H
|
|
|
|
|
#define DATA_STRUCTURES_H
|
|
|
|
|
#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"
|
|
|
|
|
|
|
|
|
|
// 用户信息结构体
|
|
|
|
|
struct UserInfo {
|
|
|
|
|
std::string email;
|
|
|
|
|
std::string password;
|
|
|
|
|
bool isLoggedIn = false;
|
|
|
|
|
};
|
|
|
|
|
namespace exam_system {
|
|
|
|
|
|
|
|
|
|
// 题目信息结构体
|
|
|
|
|
struct QuestionInfo {
|
|
|
|
|
int id;
|
|
|
|
|
std::string content;
|
|
|
|
|
std::vector<std::string> options;
|
|
|
|
|
int correctAnswer; // 0-3 对应ABCD
|
|
|
|
|
int correct_answer; // 0-3 对应ABCD
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 测试结果结构体
|
|
|
|
|
struct TestResult {
|
|
|
|
|
int totalQuestions; ///< 总问题数
|
|
|
|
|
int correctAnswers; ///< 正确问题数
|
|
|
|
|
double score; ///< 得分
|
|
|
|
|
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:
|
|
|
|
|
/**
|
|
|
|
|
* @brief 请求发送验证码
|
|
|
|
|
* @param email 邮箱地址
|
|
|
|
|
* @return 发送成功true
|
|
|
|
|
*/
|
|
|
|
|
virtual bool sendSecondCode(const std::string& email) = 0;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 用户注册
|
|
|
|
|
* @param user_name 用户名
|
|
|
|
|
* @param password 密码
|
|
|
|
|
* @param code 验证码
|
|
|
|
|
* @return 注册成功true
|
|
|
|
|
*/
|
|
|
|
|
virtual bool userRegitster(const std::string& user_name, const std::string& password, const std::string& code) = 0;
|
|
|
|
|
public:
|
|
|
|
|
virtual ~BackendInterface() = default;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 用户登陆
|
|
|
|
|
* @param user_name 用户名
|
|
|
|
|
* @param password 密码
|
|
|
|
|
* @return 登陆成功true
|
|
|
|
|
*/
|
|
|
|
|
virtual bool userLogin(const std::string& user_name, const std::string& password) = 0;
|
|
|
|
|
virtual void userLogout() = 0;
|
|
|
|
|
virtual bool changePassword(const std::string& oldPassword, const std::string& newPassword) = 0;
|
|
|
|
|
virtual void userExit() = 0;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 题目生成
|
|
|
|
|
* @param difficulty 题目难度(小学、初中、高中)
|
|
|
|
|
* @param count 题目数量
|
|
|
|
|
* @return 返回生成的题目信息(采用QuestionInfor存储题目)
|
|
|
|
|
*/
|
|
|
|
|
virtual std::vector<QuestionInfo> generateQuestions(const std::string& difficulty, int count) = 0;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief 提交答案
|
|
|
|
|
* @param userAnswers 0-3分别表示选项A-D
|
|
|
|
|
* @return 返回测试结果(采用TestResult存储测试结果)
|
|
|
|
|
*/
|
|
|
|
|
virtual TestResult submitAnswers(const std::vector<int>& userAnswers) = 0;
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
bool sendSecondCode(const std::string& email){ return true; };
|
|
|
|
|
bool userRegitster(const std::string& user_name, const std::string& password, const std::string& code){ return true; };
|
|
|
|
|
bool userLogin(const std::string& user_name, const std::string& password){ return true; };
|
|
|
|
|
bool changePassword(const std::string& old_password, const std::string& new_password){ return true; };
|
|
|
|
|
void userLogout(){return;};
|
|
|
|
|
void userExit(){return;};
|
|
|
|
|
std::vector<QuestionInfo> generateQuestions(const std::string& difficulty, int count) {
|
|
|
|
|
std::vector<QuestionInfo> questions = {
|
|
|
|
|
{1, "1 + 1 = ?", {"1", "2", "3", "4"}, 1},
|
|
|
|
|
{2, "3 * 4 = ?", {"10", "12", "14", "16"}, 1},
|
|
|
|
|
{3, "10 / 2 = ?", {"3", "4", "5", "6"}, 2},
|
|
|
|
|
{4, "7 - 3 = ?", {"2", "3", "4", "5"}, 2},
|
|
|
|
|
{5, "2 * 2 = ?", {"2", "3", "4", "5"}, 2},
|
|
|
|
|
{6, "9 / 3 = ?", {"2", "3", "4", "5"}, 1},
|
|
|
|
|
{7, "5 * 6 = ?", {"25", "30", "35", "40"}, 1},
|
|
|
|
|
{8, "8 + 7 = ?", {"14", "15", "16", "17"}, 1},
|
|
|
|
|
{9, "20 / 4 = ?", {"3", "4", "5", "6"}, 2},
|
|
|
|
|
{10, "6 * 7 = ?", {"40", "42", "44", "46"}, 1}
|
|
|
|
|
};
|
|
|
|
|
return questions;
|
|
|
|
|
}
|
|
|
|
|
public:
|
|
|
|
|
BackendImpl();
|
|
|
|
|
~BackendImpl() override = default;
|
|
|
|
|
|
|
|
|
|
TestResult submitAnswers(const std::vector<int>& userAnswers){
|
|
|
|
|
TestResult test_result = {
|
|
|
|
|
10,
|
|
|
|
|
8,
|
|
|
|
|
80.0,
|
|
|
|
|
"小学"
|
|
|
|
|
};
|
|
|
|
|
BackendImpl(const BackendImpl&) = delete;
|
|
|
|
|
BackendImpl& operator=(const BackendImpl&) = delete;
|
|
|
|
|
|
|
|
|
|
return test_result;
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
~BackendImpl() = default ;
|
|
|
|
|
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
|
|
|
|
|
#endif // BACKEND_INTERFACE_H
|