123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- import {injectLocal, provideLocal} from "@vueuse/core";
- import {toast} from "@/uni_modules/uv-ui-tools/libs/function";
- import _ from "lodash";
- import {useInjectPaperService} from "@/components/mx-paper/usePaperInjection";
- import {useInjectQuestionService} from "@/components/mx-question/useQuestionInjection";
- const key = Symbol('PAPER_NAVIGATION_SERVICE')
- export const useProvidePaperNavigationService = (paperService, questionService, overrideService = {}) => {
- const {goToQuestion, allowAnswer, allowScore, commitPaper, scorePaper} = useInjectPaperService()
- const {unansweredQuestions, unscoredQuestions, doChunk} = useInjectQuestionService()
- const questionsNavigationHandler = (navigator, list, label) => {
- if (!list.length) return toast(`没有${label}的题`)
- if (list.length == 1) return goToQuestion(_.first(list))
- navigator.open(list, `${label}的题`)
- }
- const uncompletedCheckForQuit = async (navigator) => {
- await doChunk()
- return new Promise((resolve, reject) => {
- if (allowAnswer.value) {
- if (unansweredQuestions.value.length) {
- navigator.open(unansweredQuestions.value, '您还有未做的题', {
- left: '强制退出',
- right: '继续做题',
- onLeft: () => {
- navigator.close()
- resolve() // 允许用户继续执行后续逻辑
- },
- onRight: () => {
- navigator.close()
- goToQuestion(_.first(unansweredQuestions.value))
- reject('操作终止,用户选择继续做题')
- }
- })
- } else {
- navigator.open([], '提示', {
- description: '您已经做完所有题,是否完成提交',
- left: '强制退出',
- right: '完成提交',
- onLeft: () => {
- navigator.close()
- resolve()
- },
- onRight: () => {
- navigator.close()
- commitPaper()
- reject('操作终止,用户选择完成提交')
- }
- })
- }
- } else if (allowScore.value) {
- if (unscoredQuestions.value.length) {
- navigator.open(unscoredQuestions.value, '您还有未阅的题', {
- left: '强制退出',
- right: '继续阅卷',
- onLeft: () => {
- navigator.close()
- resolve() // 允许用户继续执行后续逻辑
- },
- onRight: () => {
- navigator.close()
- goToQuestion(_.first(unscoredQuestions.value))
- reject('操作终止,用户选择继续阅卷')
- }
- })
- } else {
- navigator.open([], '提示', {
- description: '您已经阅完所有题,是否完成阅卷',
- left: '强制退出',
- right: '完成阅卷',
- onLeft: () => {
- navigator.close()
- resolve()
- },
- onRight: () => {
- navigator.close()
- scorePaper()
- reject('操作终止,用户选择完成阅卷')
- }
- })
- }
- } else {
- resolve()
- }
- })
- }
- const uncompletedCheckForSubmit = async (navigator) => {
- await doChunk()
- return new Promise((resolve, reject) => {
- if (allowAnswer.value) {
- if (unansweredQuestions.value.length) {
- navigator.open(unansweredQuestions.value, '您还有未做的题', {
- left: '仍然提交',
- right: '继续做题',
- onLeft: () => {
- navigator.close()
- resolve() // 题没有做完是可以强制交卷的!
- },
- onRight: () => {
- navigator.close()
- goToQuestion(_.first(unansweredQuestions.value))
- reject('操作终止,用户选择继续做题') // 注意这里也是reject
- }
- })
- return // 只要有题未做,弹层都会阻止原提交行为
- }
- } else if (allowScore.value) {
- if (unscoredQuestions.value.length) {
- navigator.open(unscoredQuestions.value, '未阅的题')
- reject() // 阅卷时必须给所有题打完分才行
- return
- }
- }
- resolve()
- })
- }
- const options = {
- questionsNavigationHandler,
- uncompletedCheckForQuit,
- uncompletedCheckForSubmit,
- ...overrideService
- }
- provideLocal(key, options)
- return options
- }
- export const useInjectPaperNavigationService = () => {
- return injectLocal(key)
- }
|