import * as httpApi from "@/api/chanpinShuxue.js" import { reactive, watch } from "vue" import { onLoad } from "@dcloudio/uni-app" import { catchError, toast } from "@/utils/common.js" export function useShuxueTest() { const data = reactive({ list: [], total: 0, current: 0, jieId: null, rightAnswer: 0, // 答对 wrongAnswer: 0, // 答错 }) watch(() => data.list, (val) => { const list = data.list.filter(item => { if (item.type == 3) { // 填空题 所有试题答完 return !item.reply.some(citem => citem.trim() == ''); } else { return item.reply !== null } }); data.count = list.length; }, { deep: true }) // 更新试题对错 function updateRightWrong({ rightAnswer, wrongAnswer, }) { data.rightAnswer = rightAnswer; data.wrongAnswer = wrongAnswer; } // 初始化页面数据 async function initPage() { const [err, cdata] = await catchError(httpApi.getShuxueChanpinShitiList({ jieId: data.jieId })) if (err) { toast("单元测试数据获取异常"); return; } refreshExam(cdata); } function formatListToUse(list) { list.forEach((item, index) => { item.mta_show = false; item.reply = null; if (item.type == 3) { item.result = JSON.parse(item.result); item.placeholders = item.result.map((item, cindex) => `[bank${cindex+1}]`) item.reply = item.reply ? JSON.parse(item.reply) : item.result.map(() => ''); } }) } // 数据赋值 function refreshExam(list) { const cList = list; formatListToUse(cList) data.list = cList; data.total = cList.length; } // 交卷 async function handleSubmit() { const result = []; data.list.forEach(item => { if (item.type == 1) { result.push({ reply: item.reply, stId: item.stId }) } else if (item.type == 2) { result.push({ reply: item.reply, stId: item.stId }) } else if (item.type == 3) { result.push({ reply: item.reply ? JSON.stringify(item.reply) : '', stId: item.stId }) } else if (item.type == 4) { result.push({ reply: item.reply, stId: item.stId }) } }) uni.showLoading({ title: '交卷中...' }); const [error, cdata] = await catchError(httpUnit.getExamSubmit({ jieId: data.jieId, shitiList: result })); uni.hideLoading() if (error) { toast("单元测试数据提交异常"); return; } // 执行跳页 uni.$emit('unitShuxueTest-submit', { right: cdata.dui, wrong: cdata.cuo, }) uni.redirectTo({ url: `/pages/chanpinXuanze/unitTest?rigth=${cdata.dui}&wrong=${cdata.cuo}&jieId=${data.jieId}` }) } onLoad((options) => { data.jieId = options.jieId initPage() }) return { data, handleSubmit, updateRightWrong } }