最终版本2.0

main
LYQ 2 months ago
commit ab76ebf447

@ -6,6 +6,7 @@
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="72b0954a-217b-4c2e-9f54-fc09e16134d2" name="更改" comment=""> <list default="true" id="72b0954a-217b-4c2e-9f54-fc09e16134d2" name="更改" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<<<<<<< HEAD
<change beforePath="$PROJECT_DIR$/src/account/Account.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/account/Account.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/account/Account.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/account/Account.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/account/AccountService.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/account/AccountService.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/account/AccountService.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/account/AccountService.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/account/SimpleAccountService.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/account/SimpleAccountService.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/account/SimpleAccountService.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/account/SimpleAccountService.java" afterDir="false" />
@ -45,6 +46,14 @@
<change beforePath="$PROJECT_DIR$/src/question/PrimaryQuestionGenerator.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/question/PrimaryQuestionGenerator.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/question/PrimaryQuestionGenerator.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/question/PrimaryQuestionGenerator.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/question/Question.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/question/Question.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/question/Question.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/question/Question.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/question/QuestionGenerator.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/question/QuestionGenerator.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/question/QuestionGenerator.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/question/QuestionGenerator.java" afterDir="false" />
=======
<change beforePath="$PROJECT_DIR$/src/main/java/com/student/mathquiz/MainApp.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/student/mathquiz/MainApp.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/student/mathquiz/question/PrimaryQuestionGenerator.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/student/mathquiz/question/PrimaryQuestionGenerator.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/student/mathquiz/service/IUserService.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/student/mathquiz/service/IUserService.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/student/mathquiz/service/UserServiceImpl.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/student/mathquiz/service/UserServiceImpl.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/student/mathquiz/view/LoginViewController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/student/mathquiz/view/LoginViewController.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/user_data/users.json" beforeDir="false" afterPath="$PROJECT_DIR$/user_data/users.json" afterDir="false" />
>>>>>>> develop
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -70,6 +79,7 @@
<option name="hideEmptyMiddlePackages" value="true" /> <option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" /> <option name="showLibraryContents" value="true" />
</component> </component>
<<<<<<< HEAD
<component name="PropertiesComponent"><![CDATA[{ <component name="PropertiesComponent"><![CDATA[{
"keyToString": { "keyToString": {
"Maven.mathquiz [clean].executor": "Run", "Maven.mathquiz [clean].executor": "Run",
@ -88,8 +98,27 @@
"nodejs_package_manager_path": "npm", "nodejs_package_manager_path": "npm",
"settings.editor.selected.configurable": "preferences.sourceCode.Java", "settings.editor.selected.configurable": "preferences.sourceCode.Java",
"vue.rearranger.settings.migration": "true" "vue.rearranger.settings.migration": "true"
=======
<component name="PropertiesComponent">{
&quot;keyToString&quot;: {
&quot;Maven.mathquiz [clean].executor&quot;: &quot;Run&quot;,
&quot;Maven.mathquiz [compile].executor&quot;: &quot;Run&quot;,
&quot;Maven.mathquiz [org.openjfx:javafx-maven-plugin:0.0.8:run].executor&quot;: &quot;Run&quot;,
&quot;Maven.mathquiz [package].executor&quot;: &quot;Run&quot;,
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
&quot;git-widget-placeholder&quot;: &quot;develop&quot;,
&quot;ignore.virus.scanning.warn.message&quot;: &quot;true&quot;,
&quot;kotlin-language-version-configured&quot;: &quot;true&quot;,
&quot;last_opened_file_path&quot;: &quot;C:/Users/lyq20/OneDrive/桌面/Finally/MathQuiz&quot;,
&quot;node.js.detected.package.eslint&quot;: &quot;true&quot;,
&quot;node.js.detected.package.tslint&quot;: &quot;true&quot;,
&quot;node.js.selected.package.eslint&quot;: &quot;(autodetect)&quot;,
&quot;node.js.selected.package.tslint&quot;: &quot;(autodetect)&quot;,
&quot;nodejs_package_manager_path&quot;: &quot;npm&quot;,
&quot;vue.rearranger.settings.migration&quot;: &quot;true&quot;
>>>>>>> develop
} }
}]]></component> }</component>
<component name="SharedIndexes"> <component name="SharedIndexes">
<attachedChunks> <attachedChunks>
<set> <set>
@ -111,6 +140,7 @@
<workItem from="1760260471312" duration="41000" /> <workItem from="1760260471312" duration="41000" />
<workItem from="1760269808557" duration="303000" /> <workItem from="1760269808557" duration="303000" />
<workItem from="1760270394012" duration="2320000" /> <workItem from="1760270394012" duration="2320000" />
<workItem from="1760274631654" duration="784000" />
</task> </task>
<servers /> <servers />
</component> </component>

@ -89,6 +89,7 @@ public class MainApp extends Application {
} }
} }
<<<<<<< HEAD
// ... (所有其他方法保持不变) // ... (所有其他方法保持不变)
public void handleLoginSuccess(String email) { public void handleLoginSuccess(String email) {
this.currentUserEmail = email; this.currentUserEmail = email;
@ -106,6 +107,18 @@ public class MainApp extends Application {
public void showMainMenuView() { public void showMainMenuView() {
switchScene("MainMenuView.fxml"); switchScene("MainMenuView.fxml");
} }
=======
// ★★★ 修改这个方法的参数类型,从 String 改为 User ★★★
public void handleLoginSuccess(com.student.mathquiz.model.User user) {
// ★★★ 从 User 对象中,准确地获取并保存 email★★★
this.currentUserEmail = user.getEmail();
showMainMenuView();
}
public void showLoginView() { switchScene("LoginView.fxml"); }
public void showRegisterView() { switchScene("RegisterView.fxml"); }
public void showMainMenuView() { switchScene("MainMenuView.fxml"); }
>>>>>>> develop
// 在 MainApp.java 中 // 在 MainApp.java 中
public void showQuizView(String level, int count) { public void showQuizView(String level, int count) {

@ -3,12 +3,22 @@ package com.student.mathquiz.question;
import java.util.*; import java.util.*;
public class HighQuestionGenerator implements QuestionGenerator { public class HighQuestionGenerator implements QuestionGenerator {
<<<<<<< HEAD
private static final Random RANDOM = new Random(); private static final Random RANDOM = new Random();
private static final String[] TRIG_FUNCTIONS = {"sin", "cos", "tan"}; private static final String[] TRIG_FUNCTIONS = {"sin", "cos", "tan"};
private static final Map<String, String> TRIG_VALUES = new HashMap<>(); private static final Map<String, String> TRIG_VALUES = new HashMap<>();
=======
>>>>>>> develop
private static final Random RANDOM = new Random();
private static final String[] FUNCTIONS = {"sin", "cos", "tan"};
private static final Map<String, String> TRIG_EXACT_VALUES = new HashMap<>();
private final Set<String> seenQuestions = new HashSet<>();
// 静态初始化三角函数精确值表(特殊角)
static { static {
<<<<<<< HEAD
TRIG_VALUES.put("sin30", "1/2"); TRIG_VALUES.put("sin30", "1/2");
TRIG_VALUES.put("sin45", "√2/2"); TRIG_VALUES.put("sin45", "√2/2");
TRIG_VALUES.put("sin60", "√3/2"); TRIG_VALUES.put("sin60", "√3/2");
@ -18,33 +28,113 @@ public class HighQuestionGenerator implements QuestionGenerator {
TRIG_VALUES.put("tan30", "√3/3"); TRIG_VALUES.put("tan30", "√3/3");
TRIG_VALUES.put("tan45", "1"); TRIG_VALUES.put("tan45", "1");
TRIG_VALUES.put("tan60", "√3"); TRIG_VALUES.put("tan60", "√3");
=======
// 0°
TRIG_EXACT_VALUES.put("sin0", "0");
TRIG_EXACT_VALUES.put("cos0", "1");
TRIG_EXACT_VALUES.put("tan0", "0");
// 15°
TRIG_EXACT_VALUES.put("sin15", "(√6-√2)/4");
TRIG_EXACT_VALUES.put("cos15", "(√6+√2)/4");
TRIG_EXACT_VALUES.put("tan15", "2-√3");
// 30°
TRIG_EXACT_VALUES.put("sin30", "1/2");
TRIG_EXACT_VALUES.put("cos30", "√3/2");
TRIG_EXACT_VALUES.put("tan30", "√3/3");
// 45°
TRIG_EXACT_VALUES.put("sin45", "√2/2");
TRIG_EXACT_VALUES.put("cos45", "√2/2");
TRIG_EXACT_VALUES.put("tan45", "1");
// 60°
TRIG_EXACT_VALUES.put("sin60", "√3/2");
TRIG_EXACT_VALUES.put("cos60", "1/2");
TRIG_EXACT_VALUES.put("tan60", "√3");
// 75°
TRIG_EXACT_VALUES.put("sin75", "(√6+√2)/4");
TRIG_EXACT_VALUES.put("cos75", "(√6-√2)/4");
TRIG_EXACT_VALUES.put("tan75", "2+√3");
// 90°
TRIG_EXACT_VALUES.put("sin90", "1");
TRIG_EXACT_VALUES.put("cos90", "0");
TRIG_EXACT_VALUES.put("tan90", null); // 无定义
// 105°
TRIG_EXACT_VALUES.put("sin105", "(√6+√2)/4");
TRIG_EXACT_VALUES.put("cos105", "-(√6-√2)/4");
TRIG_EXACT_VALUES.put("tan105", "-2-√3");
// 120°
TRIG_EXACT_VALUES.put("sin120", "√3/2");
TRIG_EXACT_VALUES.put("cos120", "-1/2");
TRIG_EXACT_VALUES.put("tan120", "-√3");
// 135°
TRIG_EXACT_VALUES.put("sin135", "√2/2");
TRIG_EXACT_VALUES.put("cos135", "-√2/2");
TRIG_EXACT_VALUES.put("tan135", "-1");
// 150°
TRIG_EXACT_VALUES.put("sin150", "1/2");
TRIG_EXACT_VALUES.put("cos150", "-√3/2");
TRIG_EXACT_VALUES.put("tan150", "-√3/3");
// 165°
TRIG_EXACT_VALUES.put("sin165", "(√6-√2)/4");
TRIG_EXACT_VALUES.put("cos165", "-(√6+√2)/4");
TRIG_EXACT_VALUES.put("tan165", "-2+√3");
// 180°
TRIG_EXACT_VALUES.put("sin180", "0");
TRIG_EXACT_VALUES.put("cos180", "-1");
TRIG_EXACT_VALUES.put("tan180", "0");
// 其他角度可以继续补充...
>>>>>>> develop
} }
@Override @Override
public Question generateQuestion() { public Question generateQuestion() {
return generateTrigQuestion(); while (true) {
} String func = FUNCTIONS[RANDOM.nextInt(FUNCTIONS.length)];
int angle = 15 * RANDOM.nextInt(201); // 0° ~ 3000°步长15°
int normalizedAngle = angle % 360;
String key = func + normalizedAngle;
String correctAnswer = TRIG_EXACT_VALUES.get(key);
// 跳过无定义的角度(如 tan90°
if (correctAnswer == null) continue;
private Question generateTrigQuestion() { String content = func + "(" + angle + "°) = ?";
String func = TRIG_FUNCTIONS[RANDOM.nextInt(TRIG_FUNCTIONS.length)]; if (seenQuestions.contains(content)) continue;
String[] angles = {"30", "45", "60"}; seenQuestions.add(content);
String angle = angles[RANDOM.nextInt(angles.length)];
String content = func + "(" + angle + "°) = ?"; List<String> options = generateOptions(correctAnswer);
String correctAnswer = TRIG_VALUES.get(func + angle); int correctIndex = shuffleOptions(options, correctAnswer);
List<String> options = generateOptions(correctAnswer); return new Question(content, correctAnswer, options, correctIndex);
int correctIndex = shuffleOptions(options, correctAnswer); }
return new Question(content, correctAnswer, options, correctIndex);
} }
/**
* 3 + 1
*/
private List<String> generateOptions(String correctAnswer) { private List<String> generateOptions(String correctAnswer) {
Set<String> options = new HashSet<>(); Set<String> options = new HashSet<>();
options.add(correctAnswer); options.add(correctAnswer);
List<String> allValues = new ArrayList<>(TRIG_VALUES.values());
List<String> allValues = new ArrayList<>(TRIG_EXACT_VALUES.values());
while (options.size() < 4) { while (options.size() < 4) {
options.add(allValues.get(RANDOM.nextInt(allValues.size()))); String wrong = allValues.get(RANDOM.nextInt(allValues.size()));
if (wrong != null) options.add(wrong);
} }
return new ArrayList<>(options); return new ArrayList<>(options);
} }
@ -52,4 +142,15 @@ public class HighQuestionGenerator implements QuestionGenerator {
Collections.shuffle(options); Collections.shuffle(options);
return options.indexOf(correctAnswer); return options.indexOf(correctAnswer);
} }
}
/**
*
*/
public List<Question> generateQuestions(int count) {
List<Question> questions = new ArrayList<>();
for (int i = 0; i < count; i++) {
questions.add(generateQuestion());
}
return questions;
}
}

@ -9,19 +9,15 @@ public class PrimaryQuestionGenerator implements QuestionGenerator {
@Override @Override
public Question generateQuestion() { public Question generateQuestion() {
int operandCount = RANDOM.nextInt(2) + 2; // 操作数数量2-5个
int operandCount = RANDOM.nextInt(4) + 2; // nextInt(4)→0-3+2→2-5
List<Integer> operands = new ArrayList<>(); List<Integer> operands = new ArrayList<>();
List<Character> ops = new ArrayList<>(); List<Character> ops = new ArrayList<>();
for (int i = 0; i < operandCount; i++) { // 生成合法表达式(修复后)
operands.add(RANDOM.nextInt(100) + 1); buildValidExpression(operands, ops, operandCount);
}
for (int i = 0; i < operandCount - 1; i++) {
ops.add(OPERATORS[RANDOM.nextInt(OPERATORS.length)]);
}
handleDivision(operands, ops);
// 拼接题目内容
StringBuilder contentSb = new StringBuilder(); StringBuilder contentSb = new StringBuilder();
for (int i = 0; i < operandCount; i++) { for (int i = 0; i < operandCount; i++) {
contentSb.append(operands.get(i)); contentSb.append(operands.get(i));
@ -30,27 +26,135 @@ public class PrimaryQuestionGenerator implements QuestionGenerator {
} }
} }
String content = contentSb.toString() + " = ?"; String content = contentSb.toString() + " = ?";
// 计算正确答案
String correctAnswer = calculateAnswer(operands, ops); String correctAnswer = calculateAnswer(operands, ops);
// 生成选项
List<String> options = generateOptions(correctAnswer); List<String> options = generateOptions(correctAnswer);
// 打乱选项
int correctIndex = shuffleOptions(options, correctAnswer); int correctIndex = shuffleOptions(options, correctAnswer);
return new Question(content, correctAnswer, options, correctIndex); return new Question(content, correctAnswer, options, correctIndex);
} }
private void handleDivision(List<Integer> operands, List<Character> ops) { /**
for (int i = 0; i < ops.size(); i++) { * operandsopsoperands.size() = ops.size() + 1
if (ops.get(i) == '/') { * 1+1
int divisor = operands.get(i + 1); */
if (divisor == 0) { private void buildValidExpression(List<Integer> operands, List<Character> ops, int operandCount) {
divisor = 1; // 第一个操作数1-50
operands.set(i + 1, divisor); int firstOperand = RANDOM.nextInt(50) + 1;
operands.add(firstOperand);
// 循环生成“1个运算符+1个操作数”共operandCount-1组
for (int i = 0; i < operandCount - 1; i++) {
int nextOperand;
char op;
// 循环重试:直到生成能让中间结果非负的组合
while (true) {
// 1. 只生成1个运算符无重复
op = OPERATORS[RANDOM.nextInt(OPERATORS.length)];
// 2. 计算当前中间结果此时ops还没加新opoperands也没加新数长度匹配
int currentMidResult = tempCalculate(operands, ops);
// 3. 基于当前中间结果生成合法操作数
nextOperand = generateValidNextOperand(currentMidResult, op);
// 4. 临时添加运算符和操作数(此时才开始添加,确保成对)
ops.add(op);
operands.add(nextOperand);
// 5. 检查新的中间结果是否非负
int newMidResult = tempCalculate(operands, ops);
if (newMidResult >= 0) {
// 合格,保留当前组合
break;
} else {
// 不合格,回滚(移除刚加的运算符和操作数,长度恢复匹配)
ops.remove(ops.size() - 1);
operands.remove(operands.size() - 1);
} }
int quotient = RANDOM.nextInt(10) + 1;
operands.set(i, divisor * quotient);
} }
} }
} }
/**
*
*/
private int tempCalculate(List<Integer> tempOperands, List<Character> tempOps) {
// 防御性判断:避免极端情况下长度不匹配
if (tempOperands.size() != tempOps.size() + 1) {
return -1; // 触发重试
}
List<Integer> nums = new ArrayList<>(tempOperands);
List<Character> operators = new ArrayList<>(tempOps);
// 先处理乘除
int i = 0;
while (i < operators.size()) {
char op = operators.get(i);
if (op == '*' || op == '/') {
int left = nums.get(i);
int right = nums.get(i + 1); // 现在长度匹配,不会越界
int result = (op == '*') ? left * right : left / right;
nums.set(i, result);
nums.remove(i + 1);
operators.remove(i);
} else {
i++;
}
}
// 再处理加减
int midResult = nums.get(0);
for (i = 0; i < operators.size(); i++) {
int next = nums.get(i + 1);
midResult = (operators.get(i) == '+') ? midResult + next : midResult - next;
}
return midResult;
}
/**
*
*/
private int generateValidNextOperand(int currentMidResult, char op) {
switch (op) {
case '+':
return RANDOM.nextInt(30) + 1;
case '-':
// 基于中间结果生成,确保非负
return currentMidResult <= 0 ? 1 : RANDOM.nextInt(currentMidResult) + 1;
case '*':
return RANDOM.nextInt(5) + 1;
case '/':
List<Integer> divisors = getDivisors(currentMidResult);
return divisors.get(RANDOM.nextInt(divisors.size()));
default:
return 1;
}
}
/**
*
*/
private List<Integer> getDivisors(int num) {
List<Integer> divisors = new ArrayList<>();
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) {
divisors.add(i);
}
}
if (divisors.isEmpty()) {
divisors.add(1);
}
return divisors;
}
/**
*
*/
private String calculateAnswer(List<Integer> operands, List<Character> ops) { private String calculateAnswer(List<Integer> operands, List<Character> ops) {
<<<<<<< HEAD
// Simplified calculation for primary school (no operator precedence) // Simplified calculation for primary school (no operator precedence)
int result = operands.get(0); int result = operands.get(0);
for (int i = 0; i < ops.size(); i++) { for (int i = 0; i < ops.size(); i++) {
@ -68,39 +172,68 @@ public class PrimaryQuestionGenerator implements QuestionGenerator {
case '/': case '/':
result /= nextOperand; result /= nextOperand;
break; break;
=======
List<Integer> nums = new ArrayList<>(operands);
List<Character> operators = new ArrayList<>(ops);
// 先处理乘除
int i = 0;
while (i < operators.size()) {
char op = operators.get(i);
if (op == '*' || op == '/') {
int left = nums.get(i);
int right = nums.get(i + 1);
int result = (op == '*') ? left * right : left / right;
nums.set(i, result);
nums.remove(i + 1);
operators.remove(i);
} else {
i++;
>>>>>>> develop
}
}
// 再处理加减
int finalResult = nums.get(0);
for (i = 0; i < operators.size(); i++) {
int next = nums.get(i + 1);
switch (operators.get(i)) {
case '+':
finalResult += next;
break;
case '-':
finalResult -= next;
break;
} }
} }
return String.valueOf(result);
return String.valueOf(finalResult);
} }
// 在 PrimaryQuestionGenerator.java 中 /**
*
*/
private List<String> generateOptions(String correctAnswer) { private List<String> generateOptions(String correctAnswer) {
Set<String> options = new HashSet<>(); // 使用Set来自动处理重复问题 Set<String> options = new HashSet<>();
options.add(correctAnswer); options.add(correctAnswer);
int correctNum = Integer.parseInt(correctAnswer); int correctNum = Integer.parseInt(correctAnswer);
// ★★★ 修正后的逻辑 ★★★
while (options.size() < 4) { while (options.size() < 4) {
int offset = RANDOM.nextInt(20) + 1; // 偏移量 1-20 int offset = RANDOM.nextInt(20) + 1;
// 随机生成一个错误答案,确保它不等于正确答案 int wrongNum = RANDOM.nextBoolean() ? correctNum + offset : correctNum - offset;
int wrongNum = (RANDOM.nextBoolean()) ? correctNum + offset : correctNum - offset; if (wrongNum <= 0) {
wrongNum = correctNum + offset + 10;
// 确保错误答案是正数且不等于正确答案
if (wrongNum > 0 && wrongNum != correctNum) {
options.add(String.valueOf(wrongNum));
} else {
// 如果生成的不好,就再生成一个绝对不会重复的
options.add(String.valueOf(correctNum + options.size() + 1));
} }
options.add(String.valueOf(wrongNum));
} }
return new ArrayList<>(options);
List<String> result = new ArrayList<>(options);
return result; // 返回最终的选项列表
} }
/**
*
*/
private int shuffleOptions(List<String> options, String correctAnswer) { private int shuffleOptions(List<String> options, String correctAnswer) {
Collections.shuffle(options); Collections.shuffle(options);
return options.indexOf(correctAnswer); return options.indexOf(correctAnswer);
} }
} }

@ -1,8 +1,11 @@
package com.student.mathquiz.service; package com.student.mathquiz.service;
import com.student.mathquiz.model.User; // ★★★ 确保导入了这个类 ★★★
public interface IUserService { public interface IUserService {
boolean sendVerificationCode(String email); boolean sendVerificationCode(String email);
<<<<<<< HEAD
// ★★★ 修改 register 方法,加入 username 参数 ★★★ // ★★★ 修改 register 方法,加入 username 参数 ★★★
String register(String username, String email, String code, String password, String register(String username, String email, String code, String password,
@ -10,6 +13,12 @@ public interface IUserService {
// ★★★ 修改 login 方法,参数从 email 改为 identifier (标识符) ★★★ // ★★★ 修改 login 方法,参数从 email 改为 identifier (标识符) ★★★
boolean login(String identifier, String password); boolean login(String identifier, String password);
=======
String register(String username, String email, String code, String password, String confirmPassword);
// ★★★ 修改这里的返回类型 ★★★
User login(String identifier, String password);
>>>>>>> develop
String changePassword(String email, String oldPassword, String newPassword1, String newPassword2); String changePassword(String email, String oldPassword, String newPassword1, String newPassword2);
} }

@ -120,17 +120,16 @@ public class UserServiceImpl implements IUserService {
return null; return null;
} }
// ★★★ 用这个新版本替换掉旧的 login 方法 ★★★
// 在 UserServiceImpl.java 中
@Override @Override
public boolean login(String identifier, String password) { public User login(String identifier, String password) {
User user = null; User user = null;
// 智能判断是邮箱还是用户名 // 智能判断是邮箱还是用户名
if (identifier.contains("@")) { if (identifier.contains("@")) {
// 按邮箱登录
user = userDatabase.get(identifier); user = userDatabase.get(identifier);
} else { } else {
// 按用户名登录,需要遍历查找
for (User u : userDatabase.values()) { for (User u : userDatabase.values()) {
if (u.getUsername().equalsIgnoreCase(identifier)) { if (u.getUsername().equalsIgnoreCase(identifier)) {
user = u; user = u;
@ -139,17 +138,21 @@ public class UserServiceImpl implements IUserService {
} }
} }
// 如果找不到用户,直接返回失败
if (user == null) { if (user == null) {
return false; return null; // 找不到用户,返回 null
} }
// 找到用户后,验证密码(逻辑和以前一样) // 验证密码
String encryptedInputPwd = Base64.getEncoder().encodeToString(password.getBytes()); String encryptedInputPwd = Base64.getEncoder().encodeToString(password.getBytes());
return user.getEncryptedPwd().equals(encryptedInputPwd); if (user.getEncryptedPwd().equals(encryptedInputPwd)) {
return user; // ★★★ 密码正确,返回整个 User 对象! ★★★
} else {
return null; // 密码错误,返回 null
}
} }
@Override @Override
public String changePassword(String email, String oldPassword, String newPassword1, public String changePassword(String email, String oldPassword, String newPassword1,
String newPassword2) { String newPassword2) {

@ -24,17 +24,24 @@ public class LoginViewController {
this.userService = mainApp.getUserService(); this.userService = mainApp.getUserService();
} }
// 在 LoginViewController.java 中
@FXML @FXML
private void handleLogin() { private void handleLogin() {
String email = emailField.getText(); String identifier = emailField.getText(); // 我们把变量名改得更准确
String password = passwordField.getText(); String password = passwordField.getText();
if (userService.login(email, password)) {
mainApp.handleLoginSuccess(email); // ★★★ 调用新的 login 方法,它会返回一个 User 对象 ★★★
com.student.mathquiz.model.User loggedInUser = userService.login(identifier, password);
if (loggedInUser != null) { // 如果返回的不是 null说明登录成功
// ★★★ 把完整的 User 对象传给 MainApp ★★★
mainApp.handleLoginSuccess(loggedInUser);
} else { } else {
statusLabel.setText("邮箱或密码错误!"); statusLabel.setText("用户名/邮箱或密码错误!");
} }
} }
@FXML @FXML
private void handleGoToRegister() { private void handleGoToRegister() {
mainApp.showRegisterView(); mainApp.showRegisterView();

@ -2,7 +2,7 @@
{ {
"username": "清清", "username": "清清",
"email": "lyqqqq1214@163.com", "email": "lyqqqq1214@163.com",
"encryptedPwd": "NjY2NjY2bEw\u003d", "encryptedPwd": "MTIzNDU2bEw\u003d",
"userType": "PRIMARY" "userType": "PRIMARY"
} }
] ]
Loading…
Cancel
Save