| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- 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(handleSeeResult,handleSeeResultClose) {
- 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(httpApi.getShuxueChanpinShitiSave({
- jieId: data.jieId,
- shitiList: result
- }));
- uni.hideLoading()
- if (error) {
- toast("单元测试数据提交异常");
- return;
- }
- // 执行跳页
- uni.$emit('unitShuxueTest-submit', {
- rightAnswer: cdata.dui,
- wrongAnswer: cdata.cuo,
- })
- handleSeeResult()
- }
- onLoad((options) => {
- data.jieId = options.jieId
- initPage()
- })
- function resetStart() {
- data.list = [];
- data.total = 0;
- data.current= 0;
- data.rightAnswer = 0; // 答对
- data.wrongAnswer = 0; // 答错
- handleSeeResultClose();
- initPage();
- }
- return {
- data,
- handleSubmit,
- updateRightWrong,
- resetStart
- }
- }
|