useUnit.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. activeZhang: null
  66. })
  67. onLoad((options) => {
  68. const jieId = options.jieId
  69. const cacheZhangInfo = cacheManager.get('zhangInfo');
  70. const {
  71. cardId,
  72. zhangId,
  73. nianji
  74. } = cacheManager.get('auth');
  75. data.jieId = jieId; // 需要路由参数 节Id
  76. data.zhangId = zhangId; // 需要路由参数 章Id
  77. data.nianji = nianji; // 需要年纪Id 来执行返回页面
  78. data.xueke = cardId; // 需要年纪Id 来执行返回页面
  79. data.activeZhang = getZhangInfoByJieId(cacheZhangInfo.zhangList,jieId);
  80. data.haveFlag = data.activeZhang.curZhang ? data.activeZhang.curZhang.haveFlag: 0;
  81. // 初始化页面数据
  82. initPage();
  83. })
  84. watch(() => data.list, (val) => {
  85. const list = data.list.filter(item => {
  86. if (item.type == 3) {
  87. // 填空题 所有试题答完
  88. return !item.reply.some(citem => citem.trim() == '');
  89. } else {
  90. return item.reply !== null
  91. }
  92. });
  93. data.count = list.length;
  94. }, {
  95. deep: true
  96. })
  97. // 初始化页面数据
  98. async function initPage() {
  99. const [err, cdata] = await catchError(httpUnit.getExamData({
  100. jieId: data.jieId
  101. }));
  102. if (err) {
  103. toast("单元测试数据获取异常");
  104. return;
  105. }
  106. refreshExam(cdata);
  107. }
  108. function formatListToUse(list) {
  109. list.forEach((item, index) => {
  110. item.mta_show = false;
  111. item.reply = null;
  112. if (item.type == 3) {
  113. item.result = JSON.parse(item.result);
  114. item.placeholders = item.result.map((item, cindex) => `[bank${cindex+1}]`)
  115. item.reply = item.reply ? JSON.parse(item.reply): item.result.map(() => '');
  116. }
  117. })
  118. }
  119. // 数据赋值
  120. function refreshExam(list) {
  121. const cList = list;
  122. formatListToUse(cList)
  123. data.list = cList;
  124. data.total = cList.length;
  125. handleShowTishi();
  126. }
  127. // 交卷
  128. async function handleSubmit(dom) {
  129. const result = [];
  130. data.list.forEach(item => {
  131. if (item.type == 1) {
  132. result.push({
  133. reply: item.reply,
  134. stId: item.stId
  135. })
  136. } else if (item.type == 2) {
  137. result.push({
  138. reply: item.reply ,
  139. stId: item.stId
  140. })
  141. } else if (item.type ==3){
  142. result.push({
  143. reply: item.reply ? JSON.stringify(item.reply) : '',
  144. stId: item.stId
  145. })
  146. }
  147. })
  148. const [error, cdata] = await catchError(httpUnit.getExamSubmit({
  149. jieId: data.jieId,
  150. shitiList: result
  151. }));
  152. if (error) {
  153. toast("单元测试数据提交异常");
  154. return;
  155. }
  156. dom.showPopup({
  157. right: cdata.dui,
  158. wrong: cdata.cuo,
  159. jifen: cdata.jifen
  160. });
  161. }
  162. return {
  163. ...toRefs(data),
  164. rightAnswer,
  165. wrongAnswer,
  166. jifen,
  167. showTishi,
  168. handleSubmit,
  169. initPage,
  170. handleCloseTishi,
  171. handleShowTishi
  172. }
  173. }
  174. // 提示信息显示隐藏
  175. function useTishiLeftRight() {
  176. let timer = null;
  177. const {
  178. updateTishi,
  179. getTishi
  180. } = useUnitTestTishi();
  181. const showTishi = ref(false);
  182. // 大鹅关闭追加缓存 --- 单独针对当前手机的缓存提示
  183. function handleCloseTishi() {
  184. updateTishi();
  185. showTishi.value = false;
  186. clearTimeout(timer);
  187. timer = null;
  188. }
  189. // 大鹅显示追加缓存 --- 单独针对当前手机的缓存提示
  190. function handleShowTishi() {
  191. const isNotShow = Boolean(getTishi());
  192. showTishi.value = !isNotShow;
  193. timer = setTimeout(() => {
  194. handleCloseTishi();
  195. },3000)
  196. }
  197. return {
  198. showTishi,
  199. handleCloseTishi,
  200. handleShowTishi
  201. }
  202. }
  203. function getZhangInfoByJieId(list, jieId) {
  204. const curZhang = list.find(item => item.jieList.some(cite => cite.jieId == jieId)) || null;
  205. let nextZhang = null;
  206. if (curZhang) {
  207. const nextIndex = list.findIndex(item => curZhang.zhangId == item.zhangId);
  208. if (nextIndex+1<list.length) {
  209. nextZhang = list[nextIndex+1];
  210. }
  211. }
  212. return {
  213. curZhang,
  214. nextZhang
  215. };
  216. }