usePaperQuestionCondition.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import {useInjectPaperExactCondition} from "@/views/dz/papers/hooks/usePaperExactCondition.js";
  2. import {useInjectPaperFullCondition} from "@/views/dz/papers/hooks/usePaperFullCondition.js";
  3. import {useInjectPaperKnowledgeCondition} from "@/views/dz/papers/hooks/usePaperKnowledgeCondition.js";
  4. import {useInjectGlobalLoading} from "@/views/hooks/useGlobalLoading.js";
  5. import {getPaperQuestions, getPaperQuestionTypes} from "@/api/dz/papers.js";
  6. import {injectLocal, provideLocal} from "@vueuse/core";
  7. import {ElMessage} from "element-plus";
  8. const key = Symbol('PaperQuestionCondition')
  9. export const useProvidePaperQuestionCondition = function (exactMode, handMode) {
  10. const keyword = ref('')
  11. const qtpye = ref('') // 历史遗留拼写错误
  12. const qTypes = ref([])
  13. const pageNum = ref(1)
  14. const pageSize = ref(10)
  15. const total = ref(0)
  16. const questionList = ref([])
  17. const {conditionArgs, conditionData} = exactMode ? useInjectPaperExactCondition() : useInjectPaperFullCondition()
  18. const {knowledgeId, knowledgeIds} = useInjectPaperKnowledgeCondition()
  19. const loading = useInjectGlobalLoading()
  20. // question cart
  21. const cart = ref([])
  22. const currentSubject = computed(() => {
  23. // 如果组卷条件里没有科目,试题篮就不设当前科目,即可以随意加题
  24. if (!conditionArgs.value.subjectId) return null
  25. if (!cart.value.length) return null
  26. // 否则,试题篮里不能跨科目加题
  27. const demoId = cart.value[0].subjectId
  28. return conditionData.value.subjectList.find(s => s.subjectId == demoId)
  29. })
  30. const groupedQuestions = computed(() => {
  31. const results = {}
  32. cart.value.forEach(q => {
  33. if (!results[q.qtpye]) {
  34. results[q.qtpye] = []
  35. }
  36. results[q.qtpye].push(q)
  37. })
  38. return Object.keys(results).map(k => ({qtpye: k, questions: results[k]}))
  39. })
  40. const hasQuestion = (q) => {
  41. return cart.value.some(c => c.id == q.id)
  42. }
  43. const removeQuestion = (q) => {
  44. const idx = cart.value.findIndex(c => c.id == q.id)
  45. if (idx > -1) cart.value.splice(idx, 1)
  46. }
  47. const addQuestion = (q) => {
  48. if (currentSubject.value && currentSubject.value.subjectId != q.subjectId) {
  49. return ElMessage.error(`当前科目【${currentSubject.value.subjectName}】冲突`)
  50. }
  51. cart.value.push(q)
  52. }
  53. const removeQuestionGroup = (qtpye) => {
  54. const ls = groupedQuestions.value.find(g => g.qtpye == qtpye)?.questions
  55. ls?.forEach(q => removeQuestion(q))
  56. }
  57. const clearCart = () => cart.value = []
  58. // hooks
  59. watch([() => conditionArgs.value.subjectId, knowledgeId, () => knowledgeIds.value.toString()], async ([subjectId, knowledgeId, knowledgeIds]) => {
  60. // clean
  61. qtpye.value = ''
  62. qTypes.value = []
  63. if (!subjectId && conditionData.value.subjectList?.length) return
  64. if (!knowledgeId && !knowledgeIds) return
  65. const query = {subjectId, knowledgeIds: knowledgeId || knowledgeIds}
  66. const res = await getPaperQuestionTypes(query)
  67. qTypes.value = res.data
  68. })
  69. const questionQuery = computed(() => ({
  70. pageNum: pageNum.value,
  71. pageSize: pageSize.value,
  72. title: keyword.value,
  73. qtpye: qtpye.value,
  74. knowledgeId: knowledgeId.value,
  75. knowledges: knowledgeIds.value.toString()
  76. }))
  77. const getQuestionList = async function () {
  78. if (!handMode) return // 智能组卷时不需要查询
  79. loading.value = true
  80. try {
  81. const res = await getPaperQuestions(questionQuery.value)
  82. total.value = res.total
  83. questionList.value = res.rows
  84. } finally {
  85. loading.value = false
  86. }
  87. }
  88. watch([keyword, qtpye, knowledgeId, () => knowledgeIds.value.toString()], async ([title, qtpye, knowledgeId, knowledges]) => {
  89. pageNum.value = 1
  90. questionList.value = []
  91. total.value = 0
  92. if (!knowledgeId && !knowledges) return // 有知识点即可以查询题库
  93. await getQuestionList(pageNum.value, pageSize.value)
  94. })
  95. const payload = {
  96. keyword, qtpye, qTypes, pageNum, pageSize, total, questionList, getQuestionList,
  97. cart, currentSubject, groupedQuestions,
  98. hasQuestion, addQuestion, removeQuestion, removeQuestionGroup, clearCart
  99. }
  100. provideLocal(key, payload)
  101. return payload
  102. }
  103. export const useInjectPaperQuestionCondition = function () {
  104. return injectLocal(key)
  105. }