useUnit.js 5.1 KB

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