useUnit.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import {
  2. onLoad,
  3. onReady,
  4. } from "@dcloudio/uni-app"
  5. import {
  6. reactive,
  7. ref,
  8. computed,
  9. toRefs,
  10. onMounted,
  11. watch
  12. } from "vue";
  13. import {
  14. catchError,
  15. toast
  16. } from "@/utils/common.js"
  17. import * as httpUnit from "@/api/unitTest.js"
  18. function useJifen() {
  19. const data = reactive({
  20. rightAnswer: 0, // 答对
  21. wrongAnswer: 0, // 答错
  22. jifen: 0, // 积分
  23. })
  24. function updateJifen({rightAnswer,wrongAnswer,jifen}) {
  25. data.rightAnswer = rightAnswer;
  26. data.wrongAnswer = wrongAnswer;
  27. data.jifen = jifen;
  28. }
  29. return {
  30. ...toRefs(data),
  31. updateJifen
  32. }
  33. }
  34. export function useExam() {
  35. const {rightAnswer,wrongAnswer,jifen, updateJifen} = useJifen();
  36. const data = reactive({
  37. count: 0, // 已答题数
  38. total: 0, // 总题数
  39. current: 0, // 当前试题序列
  40. list: [], // 试题列表
  41. jieId: null, // 节Id
  42. zhangId: null,
  43. nextZhangId: null,
  44. nianji: null,
  45. xueqi: null,
  46. })
  47. onLoad((options) => {
  48. const {
  49. jieId,zhangId,nextZhangId,nianji,xueqi
  50. } = options;
  51. data.jieId = jieId; // 需要路由参数 节Id
  52. data.zhangId = zhangId;// 需要路由参数 章Id
  53. data.nianji = nianji; // 需要年纪Id 来执行返回页面
  54. data.xueqi = xueqi; // 需要年纪Id 来执行返回页面
  55. // 初始化页面数据
  56. initPage();
  57. })
  58. // 当前试题
  59. const activeQa = computed(() => data.list.length ? data.list[data.current] : null);
  60. // 第一题
  61. const isFirst = computed(() => {
  62. if (!activeQa.value) return false;
  63. return activeQa.value.id === data.list[0].id;
  64. })
  65. // 最后一题
  66. const isLast = computed(() => {
  67. if (!activeQa.value) return false;
  68. const clength = data.list.length;
  69. return activeQa.value.id === data.list[clength - 1].id;
  70. })
  71. watch(() => data.list, (val) => {
  72. const list = data.list.filter(item => item.reply!==null);
  73. data.count = list.length;
  74. },{deep: true})
  75. // 下一题
  76. function nextQuestion() {
  77. data.current = data.current + 1;
  78. }
  79. // 前一题
  80. function prevQuestion() {
  81. data.current = data.current - 1;
  82. }
  83. // 初始化页面数据
  84. async function initPage() {
  85. const [err, cdata] = await catchError(httpUnit.getExamData({
  86. jieId: data.jieId
  87. }));
  88. if (err) {
  89. toast("单元测试数据获取异常");
  90. return;
  91. }
  92. refreshExam(cdata);
  93. }
  94. function formatListToUse(list) {
  95. list.forEach((item, index) => {
  96. item.mta_show = false;
  97. item.reply = null;
  98. })
  99. }
  100. // 数据赋值
  101. function refreshExam(list) {
  102. const cList = list;
  103. formatListToUse(cList)
  104. data.list = cList;
  105. data.total = cList.length;
  106. }
  107. // 交卷
  108. async function handleSubmit(dom) {
  109. const result = [];
  110. data.list.forEach(item => {
  111. result.push({reply: item.reply,stId: item.stId})
  112. })
  113. const [error, cdata] =await catchError(httpUnit.getExamSubmit(result));
  114. if (error) {
  115. toast("单元测试数据提交异常");
  116. return ;
  117. }
  118. dom.showPopup({
  119. right:cdata.dui,
  120. wrong:cdata.cuo,
  121. jifen:cdata.jifen
  122. });
  123. }
  124. return {
  125. ...toRefs(data),
  126. activeQa,
  127. isFirst,
  128. isLast,
  129. rightAnswer,
  130. wrongAnswer,
  131. jifen,
  132. nextQuestion,
  133. prevQuestion,
  134. handleSubmit,
  135. initPage
  136. }
  137. }