useUnit.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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, {useXuekeNianji,useUnitTestTishi} from "@/utils/cacheManager.js"
  19. function useJifen() {
  20. const data = reactive({
  21. rightAnswer: 0, // 答对
  22. wrongAnswer: 0, // 答错
  23. jifen: 0, // 积分
  24. zhangType: '' // 当前章类型 'last' 代表最后一章,最后一章无继续
  25. })
  26. function updateJifen({rightAnswer,wrongAnswer,jifen}) {
  27. data.rightAnswer = rightAnswer;
  28. data.wrongAnswer = wrongAnswer;
  29. data.jifen = jifen;
  30. }
  31. return {
  32. ...toRefs(data),
  33. updateJifen
  34. }
  35. }
  36. export function useExam() {
  37. // 缓存
  38. const {getXueke} = useXuekeNianji();
  39. const { updateTishi, getTishi} = useUnitTestTishi();
  40. const { showTishi, handleCloseTishi, handleShowTishi} = useTishiLeftRight()
  41. const {rightAnswer,wrongAnswer,jifen, updateJifen} = useJifen();
  42. const data = reactive({
  43. count: 0, // 已答题数
  44. total: 0, // 总题数
  45. current: 0, // 当前试题序列
  46. list: [], // 试题列表
  47. jieId: null, // 节Id
  48. zhangId: null,
  49. nianji: null,
  50. xueke: null,
  51. })
  52. onLoad((options) => {
  53. const { xueke } = getXueke();
  54. const {
  55. jieId,zhangId,nianji
  56. } = options;
  57. data.jieId = jieId; // 需要路由参数 节Id
  58. data.zhangId = zhangId;// 需要路由参数 章Id
  59. data.nianji = nianji; // 需要年纪Id 来执行返回页面
  60. data.xueke = xueke; // 需要年纪Id 来执行返回页面
  61. // 初始化页面数据
  62. initPage();
  63. })
  64. watch(() => data.list, (val) => {
  65. const list = data.list.filter(item => item.reply!==null);
  66. data.count = list.length;
  67. },{deep: true})
  68. // 初始化页面数据
  69. async function initPage() {
  70. const [err, cdata] = await catchError(httpUnit.getExamData({
  71. jieId: data.jieId
  72. }));
  73. if (err) {
  74. toast("单元测试数据获取异常");
  75. return;
  76. }
  77. refreshExam(cdata);
  78. }
  79. function formatListToUse(list) {
  80. list.forEach((item, index) => {
  81. item.mta_show = false;
  82. item.reply = null;
  83. })
  84. }
  85. // 数据赋值
  86. function refreshExam(list) {
  87. const cList = list;
  88. formatListToUse(cList)
  89. data.list = cList;
  90. data.total = cList.length;
  91. handleShowTishi();
  92. }
  93. // 交卷
  94. async function handleSubmit(dom) {
  95. const result = [];
  96. data.list.forEach(item => {
  97. result.push({reply: item.reply,stId: item.stId})
  98. })
  99. const [error, cdata] =await catchError(httpUnit.getExamSubmit(result));
  100. if (error) {
  101. toast("单元测试数据提交异常");
  102. return ;
  103. }
  104. dom.showPopup({
  105. right:cdata.dui,
  106. wrong:cdata.cuo,
  107. jifen:cdata.jifen
  108. });
  109. }
  110. return {
  111. ...toRefs(data),
  112. rightAnswer,
  113. wrongAnswer,
  114. jifen,
  115. showTishi,
  116. handleSubmit,
  117. initPage,
  118. handleCloseTishi,
  119. handleShowTishi
  120. }
  121. }
  122. // 提示信息显示隐藏
  123. function useTishiLeftRight() {
  124. const showTishi = ref(false);
  125. // 大鹅关闭追加缓存 --- 单独针对当前手机的缓存提示
  126. function handleCloseTishi() {
  127. updateTishi();
  128. showTishi.value = false;
  129. }
  130. // 大鹅显示追加缓存 --- 单独针对当前手机的缓存提示
  131. function handleShowTishi() {
  132. const isNotShow = Boolean(getTishi());
  133. showTishi.value = !isNotShow;
  134. }
  135. return {
  136. showTishi,
  137. handleCloseTishi,
  138. handleShowTishi
  139. }
  140. }