useUnit.js 4.6 KB

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