useUnit.js 5.3 KB

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