usePaperNavigationServiceInjection.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import {injectLocal, provideLocal} from "@vueuse/core";
  2. import {toast} from "@/uni_modules/uv-ui-tools/libs/function";
  3. import _ from "lodash";
  4. import {useInjectPaperService} from "@/components/mx-paper/usePaperInjection";
  5. import {useInjectQuestionService} from "@/components/mx-question/useQuestionInjection";
  6. const key = Symbol('PAPER_NAVIGATION_SERVICE')
  7. export const useProvidePaperNavigationService = (paperService, questionService, overrideService = {}) => {
  8. const {goToQuestion, allowAnswer, allowScore, commitPaper, scorePaper} = useInjectPaperService()
  9. const {unansweredQuestions, unscoredQuestions, doChunk} = useInjectQuestionService()
  10. const questionsNavigationHandler = (navigator, list, label) => {
  11. if (!list.length) return toast(`没有${label}的题`)
  12. if (list.length == 1) return goToQuestion(_.first(list))
  13. navigator.open(list, `${label}的题`)
  14. }
  15. const uncompletedCheckForQuit = async (navigator) => {
  16. await doChunk()
  17. return new Promise((resolve, reject) => {
  18. if (allowAnswer.value) {
  19. if (unansweredQuestions.value.length) {
  20. navigator.open(unansweredQuestions.value, '您还有未做的题', {
  21. left: '强制退出',
  22. right: '继续做题',
  23. onLeft: () => {
  24. navigator.close()
  25. resolve() // 允许用户继续执行后续逻辑
  26. },
  27. onRight: () => {
  28. navigator.close()
  29. goToQuestion(_.first(unansweredQuestions.value))
  30. reject('操作终止,用户选择继续做题')
  31. }
  32. })
  33. } else {
  34. navigator.open([], '提示', {
  35. description: '您已经做完所有题,是否完成提交',
  36. left: '强制退出',
  37. right: '完成提交',
  38. onLeft: () => {
  39. navigator.close()
  40. resolve()
  41. },
  42. onRight: () => {
  43. navigator.close()
  44. commitPaper()
  45. reject('操作终止,用户选择完成提交')
  46. }
  47. })
  48. }
  49. } else if (allowScore.value) {
  50. if (unscoredQuestions.value.length) {
  51. navigator.open(unscoredQuestions.value, '您还有未阅的题', {
  52. left: '强制退出',
  53. right: '继续阅卷',
  54. onLeft: () => {
  55. navigator.close()
  56. resolve() // 允许用户继续执行后续逻辑
  57. },
  58. onRight: () => {
  59. navigator.close()
  60. goToQuestion(_.first(unscoredQuestions.value))
  61. reject('操作终止,用户选择继续阅卷')
  62. }
  63. })
  64. } else {
  65. navigator.open([], '提示', {
  66. description: '您已经阅完所有题,是否完成阅卷',
  67. left: '强制退出',
  68. right: '完成阅卷',
  69. onLeft: () => {
  70. navigator.close()
  71. resolve()
  72. },
  73. onRight: () => {
  74. navigator.close()
  75. scorePaper()
  76. reject('操作终止,用户选择完成阅卷')
  77. }
  78. })
  79. }
  80. } else {
  81. resolve()
  82. }
  83. })
  84. }
  85. const uncompletedCheckForSubmit = async (navigator) => {
  86. await doChunk()
  87. return new Promise((resolve, reject) => {
  88. if (allowAnswer.value) {
  89. if (unansweredQuestions.value.length) {
  90. navigator.open(unansweredQuestions.value, '您还有未做的题', {
  91. left: '仍然提交',
  92. right: '继续做题',
  93. onLeft: () => {
  94. navigator.close()
  95. resolve() // 题没有做完是可以强制交卷的!
  96. },
  97. onRight: () => {
  98. navigator.close()
  99. goToQuestion(_.first(unansweredQuestions.value))
  100. reject('操作终止,用户选择继续做题') // 注意这里也是reject
  101. }
  102. })
  103. return // 只要有题未做,弹层都会阻止原提交行为
  104. }
  105. } else if (allowScore.value) {
  106. if (unscoredQuestions.value.length) {
  107. navigator.open(unscoredQuestions.value, '未阅的题')
  108. reject() // 阅卷时必须给所有题打完分才行
  109. return
  110. }
  111. }
  112. resolve()
  113. })
  114. }
  115. const options = {
  116. questionsNavigationHandler,
  117. uncompletedCheckForQuit,
  118. uncompletedCheckForSubmit,
  119. ...overrideService
  120. }
  121. provideLocal(key, options)
  122. return options
  123. }
  124. export const useInjectPaperNavigationService = () => {
  125. return injectLocal(key)
  126. }