useShuxueUnitTest.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import * as httpApi from "@/api/chanpinShuxue.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 useShuxueTest(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.getShuxueChanpinShitiList({
  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. })
  65. }
  66. // 数据赋值
  67. function refreshExam(list) {
  68. const cList = list;
  69. formatListToUse(cList)
  70. data.list = cList;
  71. data.total = cList.length;
  72. }
  73. // 交卷
  74. async function handleSubmit() {
  75. const result = [];
  76. data.list.forEach(item => {
  77. if (item.type == 1) {
  78. result.push({
  79. reply: item.reply,
  80. stId: item.stId
  81. })
  82. } else if (item.type == 2) {
  83. result.push({
  84. reply: item.reply,
  85. stId: item.stId
  86. })
  87. } else if (item.type == 3) {
  88. result.push({
  89. reply: item.reply ? JSON.stringify(item.reply) : '',
  90. stId: item.stId
  91. })
  92. } else if (item.type == 4) {
  93. result.push({
  94. reply: item.reply,
  95. stId: item.stId
  96. })
  97. }
  98. })
  99. uni.showLoading({
  100. title: '交卷中...'
  101. });
  102. const [error, cdata] = await catchError(httpApi.getShuxueChanpinShitiSave({
  103. jieId: data.jieId,
  104. shitiList: result
  105. }));
  106. uni.hideLoading()
  107. if (error) {
  108. toast("单元测试数据提交异常");
  109. return;
  110. }
  111. // 更新单元测试状态
  112. cacheManager.updateShuxueWanchengStatus(data.jieId)
  113. // 执行跳页
  114. uni.$emit('unitShuxueTest-submit', {
  115. rightAnswer: cdata.dui,
  116. wrongAnswer: cdata.cuo,
  117. })
  118. handleSeeResult()
  119. }
  120. onLoad((options) => {
  121. data.jieId = options.jieId
  122. initPage()
  123. })
  124. function resetStart() {
  125. data.list = [];
  126. data.total = 0;
  127. data.current= 0;
  128. data.rightAnswer = 0; // 答对
  129. data.wrongAnswer = 0; // 答错
  130. handleSeeResultClose();
  131. initPage();
  132. }
  133. return {
  134. data,
  135. handleSubmit,
  136. updateRightWrong,
  137. resetStart
  138. }
  139. }