123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- import {
- onLoad,
- onReady,
- } from "@dcloudio/uni-app"
- import {
- reactive,
- ref,
- computed,
- toRefs,
- onMounted,
- watch
- } from "vue";
- import {
- catchError,
- toast
- } from "@/utils/common.js"
- import * as httpUnit from "@/api/unitTest.js"
- function useJifen() {
- const data = reactive({
- rightAnswer: 0, // 答对
- wrongAnswer: 0, // 答错
- jifen: 0, // 积分
- })
- function updateJifen({rightAnswer,wrongAnswer,jifen}) {
- data.rightAnswer = rightAnswer;
- data.wrongAnswer = wrongAnswer;
- data.jifen = jifen;
- }
-
- return {
- ...toRefs(data),
-
- updateJifen
- }
- }
- export function useExam() {
- const {rightAnswer,wrongAnswer,jifen, updateJifen} = useJifen();
-
- const data = reactive({
- count: 0, // 已答题数
- total: 0, // 总题数
- current: 0, // 当前试题序列
- list: [], // 试题列表
- jieId: null, // 节Id
- zhangId: null,
- nextZhangId: null,
- nianji: null,
- xueqi: null,
- })
-
- onLoad((options) => {
- const {
- jieId,zhangId,nextZhangId,nianji,xueqi
- } = options;
- data.jieId = jieId; // 需要路由参数 节Id
- data.zhangId = zhangId;// 需要路由参数 章Id
- data.nextZhangId = nextZhangId; // 需要下一张id 来执行返回页面
- data.nianji = nianji; // 需要年纪Id 来执行返回页面
- data.xueqi = xueqi; // 需要年纪Id 来执行返回页面
-
-
- // 初始化页面数据
- initPage();
- })
-
- // 当前试题
- const activeQa = computed(() => data.list.length ? data.list[data.current] : null);
- // 第一题
- const isFirst = computed(() => {
- if (!activeQa.value) return false;
- return activeQa.value.id === data.list[0].id;
- })
- // 最后一题
- const isLast = computed(() => {
- if (!activeQa.value) return false;
- const clength = data.list.length;
- return activeQa.value.id === data.list[clength - 1].id;
- })
- watch(() => data.list, (val) => {
- const list = data.list.filter(item => item.reply!==null);
- data.count = list.length;
- },{deep: true})
- // 下一题
- function nextQuestion() {
- data.current = data.current + 1;
- }
- // 前一题
- function prevQuestion() {
- data.current = data.current - 1;
- }
- // 初始化页面数据
- async function initPage() {
- const [err, cdata] = await catchError(httpUnit.getExamData({
- jieId: data.jieId
- }));
- if (err) {
- toast("单元测试数据获取异常");
- return;
- }
- refreshExam(cdata);
- }
- function formatListToUse(list) {
- list.forEach((item, index) => {
- item.mta_show = false;
- item.reply = null;
- })
- }
- // 数据赋值
- function refreshExam(list) {
- const cList = list;
- formatListToUse(cList)
- data.list = cList;
- data.total = cList.length;
- }
- // 交卷
- function handleSubmit(dom) {
- // const [error, data] =await catchError(httpUnit.getExamSubmit({}));
- dom.showPopup({
- right:1,
- wrong:0,
- jifen:20
- });
- }
- return {
- ...toRefs(data),
- activeQa,
- isFirst,
- isLast,
- rightAnswer,
- wrongAnswer,
- jifen,
- nextQuestion,
- prevQuestion,
- handleSubmit,
- initPage
- }
- }
|