|
|
@@ -10,9 +10,14 @@ export const usePaperStorage = function (body) {
|
|
|
|
|
|
export const usePaperResolver = function () {
|
|
|
const paper = useStorage(key, null)
|
|
|
- const paperLocal = ref(JSON.parse(paper.value))
|
|
|
+ const paperLocal = ref(paper.value ? JSON.parse(paper.value) : null)
|
|
|
const groupedQuestions = ref([])
|
|
|
- const resolvePaper = (function () {
|
|
|
+
|
|
|
+ const resolvePaper = function () {
|
|
|
+ if (!paperLocal.value || !paperLocal.value.questions) {
|
|
|
+ groupedQuestions.value = []
|
|
|
+ return
|
|
|
+ }
|
|
|
const results = {}
|
|
|
paperLocal.value.questions.forEach(q => {
|
|
|
if (!results[q.qtpye]) results[q.qtpye] = []
|
|
|
@@ -22,9 +27,27 @@ export const usePaperResolver = function () {
|
|
|
title: g,
|
|
|
list: results[g],
|
|
|
num: results[g].length,
|
|
|
- score: results[g].reduce((acc, current) => acc + current.score, 0)
|
|
|
+ score: results[g].reduce((acc, current) => acc + (current.score || 0), 0)
|
|
|
}))
|
|
|
- })()
|
|
|
+ }
|
|
|
+
|
|
|
+ // 初始化时解析
|
|
|
+ resolvePaper()
|
|
|
+
|
|
|
+ // 监听 storage 变化,自动更新数据
|
|
|
+ watch(paper, (newVal) => {
|
|
|
+ if (newVal) {
|
|
|
+ try {
|
|
|
+ paperLocal.value = JSON.parse(newVal)
|
|
|
+ resolvePaper()
|
|
|
+ } catch (e) {
|
|
|
+ console.error('解析试卷数据失败:', e)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ paperLocal.value = null
|
|
|
+ groupedQuestions.value = []
|
|
|
+ }
|
|
|
+ }, { immediate: false })
|
|
|
|
|
|
const toCommitPaper = function () {
|
|
|
const questions = []
|