useUnit.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. import cacheManager, {useUnitTestTishi} from "@/utils/cacheManager.js"
  19. function useJifen() {
  20. const data = reactive({
  21. rightAnswer: 0, // 答对
  22. wrongAnswer: 0, // 答错
  23. jifen: 0, // 积分
  24. })
  25. function updateJifen({rightAnswer,wrongAnswer,jifen}) {
  26. data.rightAnswer = rightAnswer;
  27. data.wrongAnswer = wrongAnswer;
  28. data.jifen = jifen;
  29. }
  30. return {
  31. ...toRefs(data),
  32. updateJifen
  33. }
  34. }
  35. export function useExam() {
  36. // 缓存
  37. const { showTishi, handleCloseTishi, handleShowTishi} = useTishiLeftRight()
  38. const {rightAnswer,wrongAnswer,jifen, updateJifen} = useJifen();
  39. const data = reactive({
  40. count: 0, // 已答题数
  41. total: 0, // 总题数
  42. current: 0, // 当前试题序列
  43. list: [], // 试题列表
  44. jieId: null, // 节Id
  45. zhangId: null,
  46. nianji: null,
  47. xueke: null,
  48. haveFlag: false, // 是否有下一章
  49. })
  50. onLoad((options) => {
  51. const jieNumber = options.jieNumber
  52. if (!(cacheManager.get('zhangInfo') && options.jieNumber)) {
  53. toast('数据错误,缓存丢失/ number丢失')
  54. return false
  55. }
  56. const cacheZhangInfo = cacheManager.get('zhangInfo');
  57. let currentObject = cacheZhangInfo.jieList.find(item => item.number == options.jieNumber);
  58. const { cardId,zhangId,nianji } = cacheManager.get('auth');
  59. data.jieId = currentObject.jieId; // 需要路由参数 节Id
  60. data.zhangId = zhangId;// 需要路由参数 章Id
  61. data.nianji = nianji; // 需要年纪Id 来执行返回页面
  62. data.xueke = cardId; // 需要年纪Id 来执行返回页面
  63. data.haveFlag = cacheZhangInfo.haveFlag
  64. // 初始化页面数据
  65. initPage();
  66. })
  67. watch(() => data.list, (val) => {
  68. const list = data.list.filter(item => {
  69. if (item.type == 3) {
  70. // 填空题 所有试题答完
  71. return !item.reply.some(citem => citem.trim() == '');
  72. } else {
  73. return item.reply!==null
  74. }
  75. });
  76. data.count = list.length;
  77. },{deep: true})
  78. // 初始化页面数据
  79. async function initPage() {
  80. const [err, cdata] = await catchError(httpUnit.getExamData({
  81. jieId: data.jieId
  82. }));
  83. if (err) {
  84. toast("单元测试数据获取异常");
  85. return;
  86. }
  87. refreshExam(cdata);
  88. }
  89. function formatListToUse(list) {
  90. list.forEach((item, index) => {
  91. item.mta_show = false;
  92. item.reply = null;
  93. if (item.type == 3) {
  94. item.result = JSON.parse(item.result);
  95. item.placeholders = item.result.map((item,cindex) => `[bank${cindex}]`)
  96. item.reply = item.result.map(() => '');
  97. }
  98. })
  99. }
  100. // 数据赋值
  101. function refreshExam(list) {
  102. const cList = list;
  103. formatListToUse(cList)
  104. data.list = cList;
  105. data.total = cList.length;
  106. handleShowTishi();
  107. }
  108. // 交卷
  109. async function handleSubmit(dom) {
  110. const result = [];
  111. data.list.forEach(item => {
  112. result.push({reply: item.reply ? JSON.stringify(item.reply):'',stId: item.stId})
  113. })
  114. const [error, cdata] =await catchError(httpUnit.getExamSubmit({
  115. jieId: data.jieId,
  116. shitiList: result
  117. }));
  118. if (error) {
  119. toast("单元测试数据提交异常");
  120. return ;
  121. }
  122. dom.showPopup({
  123. right:cdata.dui,
  124. wrong:cdata.cuo,
  125. jifen:cdata.jifen
  126. });
  127. }
  128. return {
  129. ...toRefs(data),
  130. rightAnswer,
  131. wrongAnswer,
  132. jifen,
  133. showTishi,
  134. handleSubmit,
  135. initPage,
  136. handleCloseTishi,
  137. handleShowTishi
  138. }
  139. }
  140. // 提示信息显示隐藏
  141. function useTishiLeftRight() {
  142. const { updateTishi, getTishi} = useUnitTestTishi();
  143. const showTishi = ref(false);
  144. // 大鹅关闭追加缓存 --- 单独针对当前手机的缓存提示
  145. function handleCloseTishi() {
  146. updateTishi();
  147. showTishi.value = false;
  148. }
  149. // 大鹅显示追加缓存 --- 单独针对当前手机的缓存提示
  150. function handleShowTishi() {
  151. const isNotShow = Boolean(getTishi());
  152. showTishi.value = !isNotShow;
  153. }
  154. return {
  155. showTishi,
  156. handleCloseTishi,
  157. handleShowTishi
  158. }
  159. }