dedsudiyu 11 hours ago
parent
commit
3475bc330d

+ 19 - 11
pages_safetyEducationExamination/views/exam/mockExamPlay.vue

@@ -292,10 +292,15 @@
 						snapshotQuestionId: snapshotQuestionId,
 						examScene: 2,
 					})
-					this.$set(this, 'currentQuestion', response.data.data)
-					this.$set(this, 'viewAnswerShowType', false)
+					if (response.data && response.data.data) {
+						this.$set(this, 'currentQuestion', response.data.data)
+						this.$set(this, 'viewAnswerShowType', false)
+					} else {
+						throw new Error((response.data && response.data.message) || '加载题目失败')
+					}
 				} catch (e) {
-					uni.showToast({ title: '题目加载失败', icon: 'none' })
+					console.error('加载题目失败', e)
+					throw e
 				}
 			},
 			// 同步剩余时间并启动倒计时
@@ -358,14 +363,17 @@
 				if (!q || !q.snapshotQuestionId) return Promise.resolve()
 				const tags = Array.isArray(q.answerTags) ? q.answerTags.filter(Boolean) : []
 				if (tags.length === 0) return Promise.resolve()
+				// 快照当前 index,避免 .then 回调时 index 已切换导致回写到错误的题
+				const gi = this.groupIndex
+				const qi = this.questionIndex
 				return examUserExamAnswerSave({
 					attemptId: this.attemptId,
 					snapshotQuestionId: q.snapshotQuestionId,
 					userAnswer: tags.join(','),
 				}).then(response => {
 					const groups = this.questionTypeGroups
-					if (groups[this.groupIndex] && groups[this.groupIndex].questions[this.questionIndex]) {
-						this.$set(groups[this.groupIndex].questions[this.questionIndex], 'isCorrect', response.data.data)
+					if (groups[gi] && groups[gi].questions[qi]) {
+						this.$set(groups[gi].questions[qi], 'isCorrect', response.data.data)
 					}
 				}).catch(e => {
 					console.error('保存答案失败', e)
@@ -377,20 +385,20 @@
 				if (targetGi === this.groupIndex && targetQi === this.questionIndex) return
 				const group = this.questionTypeGroups[targetGi]
 				if (!group || !group.questions || !group.questions[targetQi]) return
-				this.isSaving = true
-				await this.saveCurrentAnswer()
-				this.isSaving = false
 				const prevGi = this.groupIndex
 				const prevQi = this.questionIndex
-				this.groupIndex = targetGi
-				this.questionIndex = targetQi
+				this.isSaving = true
 				try {
+					await this.saveCurrentAnswer()
+					this.groupIndex = targetGi
+					this.questionIndex = targetQi
 					await this.loadQuestion(group.questions[targetQi].snapshotQuestionId)
 				} catch (e) {
-					// 加载失败回滚 index,避免与 currentQuestion 错位
 					this.groupIndex = prevGi
 					this.questionIndex = prevQi
 					uni.showToast({ title: '题目加载失败,请重试', icon: 'none' })
+				} finally {
+					this.isSaving = false
 				}
 			},
 			selectQuestion(gi, qi) {

+ 14 - 8
pages_safetyEducationExamination/views/exam/onlineExamPlay.vue

@@ -292,9 +292,12 @@
 					});
 					if (data.code === 200 && data.data) {
 						this.$set(this, 'currentQuestion', data.data);
+					} else {
+						throw new Error(data.message || '加载题目失败');
 					}
 				} catch (e) {
 					console.error('加载题目失败', e);
+					throw e;
 				}
 			},
 			async syncRemainingTime() {
@@ -360,6 +363,9 @@
 				if (!q || !q.snapshotQuestionId) return Promise.resolve();
 				const tags = Array.isArray(q.answerTags) ? q.answerTags.filter(Boolean) : [];
 				if (tags.length === 0) return Promise.resolve();
+				// 保存时记录当前 index,.then 回调里用快照避免 index 已切换导致回写错题
+				const gi = this.groupIndex;
+				const qi = this.questionIndex;
 				return examUserExamAnswerSave({
 					attemptId: this.attemptId,
 					snapshotQuestionId: q.snapshotQuestionId,
@@ -367,8 +373,8 @@
 				}).then(res => {
 					if (res.data && res.data.code === 200) {
 						const groups = this.questionTypeGroups;
-						if (groups[this.groupIndex] && groups[this.groupIndex].questions[this.questionIndex]) {
-							this.$set(groups[this.groupIndex].questions[this.questionIndex], 'answered', 1);
+						if (groups[gi] && groups[gi].questions[qi]) {
+							this.$set(groups[gi].questions[qi], 'answered', 1);
 						}
 					}
 				}).catch(e => {
@@ -380,20 +386,20 @@
 				if (targetGi === this.groupIndex && targetQi === this.questionIndex) return;
 				const group = this.questionTypeGroups[targetGi];
 				if (!group || !group.questions || !group.questions[targetQi]) return;
-				this.isSaving = true;
-				await this.saveCurrentAnswer();
-				this.isSaving = false;
 				const prevGi = this.groupIndex;
 				const prevQi = this.questionIndex;
-				this.groupIndex = targetGi;
-				this.questionIndex = targetQi;
+				this.isSaving = true;
 				try {
+					await this.saveCurrentAnswer();
+					this.groupIndex = targetGi;
+					this.questionIndex = targetQi;
 					await this.loadQuestion(group.questions[targetQi].snapshotQuestionId);
 				} catch (e) {
-					// 加载失败则回滚 index,避免 index 与 currentQuestion 错位
 					this.groupIndex = prevGi;
 					this.questionIndex = prevQi;
 					uni.showToast({ title: '题目加载失败,请重试', icon: 'none' });
+				} finally {
+					this.isSaving = false;
 				}
 			},
 			selectQuestion(gi, qi) {