usePaperExactCondition.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import {createEventHook, injectLocal, provideLocal} from "@vueuse/core";
  2. import {
  3. getPaperBatches,
  4. getPaperExamTypes,
  5. getPaperKnowledges,
  6. getPaperMajors,
  7. getPaperProvinces,
  8. getPaperSubjects,
  9. getPaperUniversities
  10. } from "@/api/dz/papers.js";
  11. const key = Symbol('PaperExactCondition')
  12. export const useProvidePaperExactCondition = function () {
  13. const batchId = ref('')
  14. const batchList = ref([])
  15. const location = ref('')
  16. const provinces = ref([])
  17. const examType = ref('')
  18. const examTypes = ref([])
  19. const universityId = ref('')
  20. const universities = ref([])
  21. const majorPlanId = ref('')
  22. const majors = ref([])
  23. const subjects = ref([])
  24. const subjectId = ref('')
  25. const knowledges = ref([])
  26. const knowledgeNode = ref(null) // 单选节点
  27. const knowledgeCheckNodes = ref([]) // 多选的节点
  28. const knowledgeIds = computed(() => knowledgeCheckNodes.value.map(k => k.id))
  29. const knowledgeId = computed(() => knowledgeNode.value?.id || '') // 单选
  30. const knowledgeRemoveEvent = createEventHook()
  31. const removeKnowledge = async (k) => {
  32. const nodes = knowledgeCheckNodes.value
  33. const idx = nodes.indexOf(k)
  34. if (idx > -1) {
  35. nodes.splice(idx, 1)
  36. await knowledgeRemoveEvent.trigger(k)
  37. }
  38. }
  39. const payload = {
  40. location, provinces, examType, examTypes, universityId, universities,
  41. batchId, batchList, majorPlanId, majors, subjects, subjectId,
  42. knowledges, knowledgeNode, knowledgeId, knowledgeCheckNodes, knowledgeIds,
  43. removeKnowledge, onKnowledgeRemove: knowledgeRemoveEvent.on
  44. }
  45. provideLocal(key, payload)
  46. // hooks
  47. onMounted(async () => {
  48. const res = await getPaperProvinces()
  49. provinces.value = res.data
  50. })
  51. onMounted(async () => {
  52. const res = await getPaperBatches()
  53. batchList.value = res.data
  54. })
  55. watch(location, async (location) => {
  56. // clean
  57. examType.value = ''
  58. examTypes.value = []
  59. if (!location) return
  60. const resT = await getPaperExamTypes({location})
  61. examTypes.value = resT.data
  62. })
  63. watch([location, batchId], async ([location, batchId]) => {
  64. // clean
  65. universityId.value = ''
  66. universities.value = []
  67. if (!location || !batchId) return
  68. const resU = await getPaperUniversities({location, batchId})
  69. universities.value = resU.data
  70. })
  71. watch([location, examType, batchId, universityId], async ([location, examType, batchId, universityId]) => {
  72. // clean
  73. majorPlanId.value = ''
  74. majors.value = []
  75. if (!location || !examType || !batchId || !universityId) return
  76. const res = await getPaperMajors({location, examType, batchId, universityId})
  77. majors.value = res.data
  78. if (res.data.length) majorPlanId.value = res.data[0].id
  79. })
  80. watch(universityId, async (universityId) => {
  81. // clean
  82. subjects.value = []
  83. subjectId.value = ''
  84. if (!universityId) return
  85. const res = await getPaperSubjects({universityId})
  86. subjects.value = res.data
  87. if (res.data.length) subjectId.value = res.data[0].subjectId
  88. })
  89. watch([majorPlanId, subjectId], async ([majorPlanId, subjectId]) => {
  90. // clean
  91. knowledges.value = []
  92. knowledgeNode.value = null // 单选的情况
  93. knowledgeCheckNodes.value = [] // 多选的情况
  94. if (!subjectId || !majorPlanId) return
  95. // 获取知识点数据
  96. const res = await getPaperKnowledges({subjectId, majorPlanId})
  97. knowledges.value = res.data
  98. })
  99. return payload
  100. }
  101. export const useInjectPaperExactCondition = function () {
  102. return injectLocal(key)
  103. }