useShuxueUnitTest.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. export function useShuxueTest(handleSeeResult, handleCheckAnswer) {
  14. const data = reactive({
  15. list: [],
  16. total: 0,
  17. current: 0,
  18. jieId: null,
  19. rightAnswer: 0, // 答对
  20. wrongAnswer: 0, // 答错
  21. })
  22. watch(() => data.list, (val) => {
  23. const list = data.list.filter(item => {
  24. if (item.type == 3) {
  25. // 填空题 所有试题答完
  26. return !item.reply.some(citem => citem.trim() == '');
  27. } else {
  28. return item.reply !== null
  29. }
  30. });
  31. data.count = list.length;
  32. }, {
  33. deep: true
  34. })
  35. // 更新试题对错
  36. function updateRightWrong({
  37. rightAnswer,
  38. wrongAnswer,
  39. }) {
  40. data.rightAnswer = rightAnswer;
  41. data.wrongAnswer = wrongAnswer;
  42. }
  43. // 初始化页面数据
  44. async function initPage() {
  45. const [err, cdata] = await catchError(httpApi.getShuxueChanpinShitiList({
  46. jieId: data.jieId
  47. }))
  48. if (err) {
  49. toast("单元测试数据获取异常");
  50. return;
  51. }
  52. refreshExam(cdata);
  53. }
  54. function formatListToUse(list) {
  55. list.forEach((item, index) => {
  56. item.mta_show = false;
  57. item.reply = null;
  58. if (item.type == 3) {
  59. item.result = JSON.parse(item.result);
  60. item.placeholders = item.result.map((item, cindex) => `[bank${cindex+1}]`)
  61. item.reply = item.reply ? JSON.parse(item.reply) : item.result.map(() => '');
  62. }
  63. })
  64. }
  65. // 数据赋值
  66. function refreshExam(list) {
  67. const cList = list;
  68. formatListToUse(cList)
  69. data.list = cList;
  70. data.total = cList.length;
  71. }
  72. // 交卷
  73. async function handleSubmit() {
  74. const result = [];
  75. data.list.forEach(item => {
  76. if (item.type == 1) {
  77. result.push({
  78. reply: item.reply,
  79. stId: item.stId
  80. })
  81. } else if (item.type == 2) {
  82. result.push({
  83. reply: item.reply,
  84. stId: item.stId
  85. })
  86. } else if (item.type == 3) {
  87. result.push({
  88. reply: item.reply ? JSON.stringify(item.reply) : '',
  89. stId: item.stId
  90. })
  91. } else if (item.type == 4) {
  92. result.push({
  93. reply: item.reply,
  94. stId: item.stId
  95. })
  96. }
  97. })
  98. uni.showLoading({
  99. title: '交卷中...'
  100. });
  101. const [error, cdata] = await catchError(httpUnit.getExamSubmit({
  102. jieId: data.jieId,
  103. shitiList: result
  104. }));
  105. uni.hideLoading()
  106. if (error) {
  107. toast("单元测试数据提交异常");
  108. return;
  109. }
  110. // 执行跳页
  111. uni.$emit('unitShuxueTest-submit', {
  112. right: cdata.dui,
  113. wrong: cdata.cuo,
  114. })
  115. handleSeeResult()
  116. }
  117. onLoad((options) => {
  118. data.jieId = options.jieId
  119. initPage()
  120. })
  121. return {
  122. data,
  123. handleSubmit,
  124. updateRightWrong
  125. }
  126. }