useUnit.js 4.4 KB

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