usePinduUnitTest.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import * as httpApi from "@/api/chanpinZiRanPinDu.js"
  2. import {
  3. reactive,
  4. watch
  5. } from "vue"
  6. import {
  7. onLoad
  8. } from "@dcloudio/uni-app"
  9. import {
  10. catchError,
  11. toast
  12. } from "@/utils/common.js"
  13. import cacheManager from "@/utils/cacheManager.js";
  14. export function usePinduTest(handleSeeResult,handleSeeResultClose) {
  15. const data = reactive({
  16. list: [],
  17. total: 0,
  18. current: 0,
  19. jieId: null,
  20. rightAnswer: 0, // 答对
  21. wrongAnswer: 0, // 答错
  22. })
  23. watch(() => data.list, (val) => {
  24. const list = data.list.filter(item => {
  25. if (item.type == 3) {
  26. // 填空题 所有试题答完
  27. return !item.reply.some(citem => citem.trim() == '');
  28. } else {
  29. return item.reply !== null
  30. }
  31. });
  32. data.count = list.length;
  33. }, {
  34. deep: true
  35. })
  36. // 更新试题对错
  37. function updateRightWrong({
  38. rightAnswer,
  39. wrongAnswer,
  40. }) {
  41. data.rightAnswer = rightAnswer;
  42. data.wrongAnswer = wrongAnswer;
  43. }
  44. // 初始化页面数据
  45. async function initPage() {
  46. const [err, cdata] = await catchError(httpApi.getPinduChanpinShitiList({
  47. jieId: data.jieId
  48. }))
  49. if (err) {
  50. toast("单元测试数据获取异常");
  51. return;
  52. }
  53. refreshExam(cdata);
  54. }
  55. function formatListToUse(list) {
  56. list.forEach((item, index) => {
  57. item.mta_show = false;
  58. item.reply = null;
  59. if (item.type == 3) {
  60. item.result = JSON.parse(item.result);
  61. item.placeholders = item.result.map((item, cindex) => `[bank${cindex+1}]`)
  62. item.reply = item.reply ? JSON.parse(item.reply) : item.result.map(() => '');
  63. }
  64. if (item.type == 4) {
  65. // 特殊题型英语题
  66. const audioList = item.audios ? item.audios.split(',') : [];
  67. item.placeholders = audioList.map((item, cindex) => `[yingyu${cindex+1}]`)
  68. item.audioList = audioList;
  69. }
  70. })
  71. }
  72. // 数据赋值
  73. function refreshExam(list) {
  74. const cList = list;
  75. formatListToUse(cList)
  76. data.list = cList;
  77. data.total = cList.length;
  78. }
  79. // 交卷
  80. async function handleSubmit() {
  81. const result = [];
  82. data.list.forEach(item => {
  83. if (item.type == 1) {
  84. result.push({
  85. reply: item.reply,
  86. stId: item.stId
  87. })
  88. } else if (item.type == 2) {
  89. result.push({
  90. reply: item.reply,
  91. stId: item.stId
  92. })
  93. } else if (item.type == 3) {
  94. result.push({
  95. reply: item.reply ? JSON.stringify(item.reply) : '',
  96. stId: item.stId
  97. })
  98. } else if (item.type == 4) {
  99. result.push({
  100. reply: item.reply,
  101. stId: item.stId
  102. })
  103. }
  104. })
  105. uni.showLoading({
  106. title: '交卷中...'
  107. });
  108. const [error, cdata] = await catchError(httpApi.getPinduChanpinShitiSave({
  109. jieId: data.jieId,
  110. shitiList: result
  111. }));
  112. uni.hideLoading()
  113. if (error) {
  114. toast("单元测试数据提交异常");
  115. return;
  116. }
  117. // 更新单元测试状态
  118. cacheManager.updatePinduWanchengStatus(data.jieId)
  119. // 执行跳页
  120. uni.$emit('unitPinduTest-submit', {
  121. rightAnswer: cdata.dui,
  122. wrongAnswer: cdata.cuo,
  123. })
  124. handleSeeResult()
  125. }
  126. onLoad((options) => {
  127. data.jieId = options.jieId
  128. initPage()
  129. })
  130. function resetStart() {
  131. data.list = [];
  132. data.total = 0;
  133. data.current= 0;
  134. data.rightAnswer = 0; // 答对
  135. data.wrongAnswer = 0; // 答错
  136. handleSeeResultClose();
  137. initPage();
  138. }
  139. return {
  140. data,
  141. handleSubmit,
  142. updateRightWrong,
  143. resetStart
  144. }
  145. }