useUnit.js 5.3 KB

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