useUnit.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import {
  2. onLoad,
  3. onReady,
  4. } from "@dcloudio/uni-app"
  5. import {
  6. reactive,
  7. ref,
  8. computed,
  9. toRefs,
  10. onMounted,
  11. } from "vue";
  12. import {
  13. catchError,
  14. toast
  15. } from "@/utils/common.js"
  16. import * as httpUnit from "@/api/unitTest.js"
  17. function useJifen() {
  18. const data = reactive({
  19. rightAnswer: 0, // 答对
  20. wrongAnswer: 0, // 答错
  21. jifen: 0, // 积分
  22. })
  23. function updateJifen({rightAnswer,wrongAnswer,jifen}) {
  24. data.rightAnswer = rightAnswer;
  25. data.wrongAnswer = wrongAnswer;
  26. data.jifen = jifen;
  27. }
  28. return {
  29. ...toRefs(data),
  30. updateJifen
  31. }
  32. }
  33. export function useExam(dom) {
  34. const {rightAnswer,wrongAnswer,jifen, updateJifen} = useJifen();
  35. const data = reactive({
  36. count: 0, // 已答题数
  37. total: 0, // 总题数
  38. current: 0, // 当前试题序列
  39. list: [], // 试题列表
  40. jieId: null, // 节Id
  41. })
  42. // 当前试题
  43. const activeQa = computed(() => data.list.length ? data.list[data.current] : null);
  44. // 第一题
  45. const isFirst = computed(() => {
  46. if (!activeQa.value) return false;
  47. return activeQa.value.id === data.list[0].id;
  48. })
  49. // 最后一题
  50. const isLast = computed(() => {
  51. if (!activeQa.value) return false;
  52. const clength = data.list.length;
  53. return activeQa.value.id === data.list[clength - 1].id;
  54. })
  55. onLoad((options) => {
  56. const {
  57. jieId
  58. } = options;
  59. data.jieId = jieId;
  60. // 初始化页面数据
  61. initPage();
  62. })
  63. // 下一题
  64. function nextQuestion() {
  65. data.current = data.current + 1;
  66. }
  67. // 前一题
  68. function prevQuestion() {
  69. data.current = data.current - 1;
  70. }
  71. // 初始化页面数据
  72. async function initPage() {
  73. const [err, data] = await catchError(httpUnit.getExamData({
  74. nianji: 1,
  75. xueqi: 1
  76. }));
  77. if (err) {
  78. toast("单元测试数据获取异常");
  79. return;
  80. }
  81. refreshExam(data);
  82. }
  83. // 数据赋值
  84. function refreshExam(list) {
  85. // FIXME 模拟id 待删除
  86. list.forEach((item, index) => item.id = index)
  87. data.list = list;
  88. data.total = list.length;
  89. }
  90. // 交卷
  91. function handleSubmit() {}
  92. return {
  93. ...toRefs(data),
  94. activeQa,
  95. isFirst,
  96. isLast,
  97. isFirst,
  98. isLast,
  99. rightAnswer,
  100. wrongAnswer,
  101. jifen,
  102. nextQuestion,
  103. prevQuestion,
  104. handleSubmit,
  105. initPage
  106. }
  107. }