usePaperExactCondition.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import {useProvidePaperBatchCondition} from "@/views/dz/papers/hooks/usePaperBatchCondition.js";
  2. import {getPaperExamTypes, getPaperMajors, getPaperSubjects, getPaperUniversities} from "@/api/dz/papers.js";
  3. import {createEventHook, injectLocal, provideLocal} from "@vueuse/core";
  4. const key = Symbol('PaperExactCondition')
  5. export const useProvidePaperExactCondition = function (type) {
  6. const {buildType, batchId, batchList, formatBatchName, getBatchDisplayName} = useProvidePaperBatchCondition(type)
  7. const examType = ref('')
  8. const examTypes = ref([])
  9. const universityId = ref('')
  10. const universities = ref([])
  11. const _allMajors = ref([])
  12. const majorGroup = ref('')
  13. const majorGroups = computed(() => {
  14. const results = []
  15. _allMajors.value.forEach(m => {
  16. if (results.includes(m.majorGroup)) return
  17. results.push(m.majorGroup)
  18. })
  19. return results
  20. })
  21. const majorPlanId = ref('')
  22. const majors = computed(() => {
  23. if (!majorGroup.value) return
  24. return _allMajors.value.filter(m => m.majorGroup == majorGroup.value)
  25. })
  26. const subjectId = ref('')
  27. const subjectList = ref([])
  28. const conditionArgs = computed(() => ({
  29. buildType: toValue(buildType),
  30. batchId: toValue(batchId),
  31. examType: toValue(examType),
  32. universityId: toValue(universityId),
  33. majorGroup: toValue(majorGroup),
  34. majorPlanId: toValue(majorPlanId),
  35. subjectId: toValue(subjectId)
  36. }))
  37. const conditionData = computed(() => ({
  38. examTypes: toValue(examTypes),
  39. batchList: toValue(batchList),
  40. universities: toValue(universities),
  41. majorGroups: toValue(majorGroups),
  42. majors: toValue(majors),
  43. subjectList: toValue(subjectList)
  44. }))
  45. const exactEvent = createEventHook()
  46. const triggerExactEvent = async function (args) {
  47. await exactEvent.trigger(args)
  48. }
  49. watch(batchId, async (batchId) => {
  50. examType.value = ''
  51. examTypes.value = []
  52. if (!batchId) return
  53. const res = await getPaperExamTypes({buildType, batchId})
  54. examTypes.value = res.data
  55. })
  56. watch([batchId, examType], async ([batchId, examType]) => {
  57. universityId.value = ''
  58. universities.value = []
  59. if (!batchId || !examType) return
  60. const res = await getPaperUniversities({buildType, batchId, examType})
  61. universities.value = res.data
  62. const resSubject = await getPaperSubjects({buildType, batchId, examType})
  63. subjectList.value = resSubject.data
  64. })
  65. watch([batchId, examType, universityId], async ([batchId, examType, universityId]) => {
  66. majorGroup.value = ''
  67. if (!batchId || !examType || !universityId) return
  68. const res = await getPaperMajors({buildType, batchId, examType, universityId})
  69. _allMajors.value = res.data
  70. // 没有数据时,说明已经满足了条件
  71. if (!majorGroups.value.length) await triggerExactEvent({buildType, batchId, examType, universityId})
  72. else if (majorGroups.value.length == 1) majorGroup.value = majorGroups.value[0]
  73. })
  74. watch([batchId, examType, universityId, majorGroup], async ([batchId, examType, universityId, majorGroup]) => {
  75. majorPlanId.value = ''
  76. if (!batchId || !examType || !universityId || !majorGroup) return
  77. if (!majors.value.length) await triggerExactEvent({buildType, batchId, examType, universityId, majorGroup})
  78. else if (majors.value.length == 1) majorPlanId.value = majors.value[0].id
  79. })
  80. watch(majorPlanId, async (majorPlanId) => {
  81. if (!majorPlanId) return
  82. // 最细的情况,选中了专业
  83. const args = {
  84. buildType,
  85. batchId: toValue(batchId),
  86. examType: toValue(examType),
  87. universityId: toValue(universityId),
  88. majorGroup: toValue(majorGroup),
  89. majorPlanId
  90. }
  91. await triggerExactEvent(args)
  92. })
  93. const payload = {buildType, batchId, batchList, formatBatchName, getBatchDisplayName,
  94. examType, examTypes,
  95. universityId, universities,
  96. majorGroup, majorGroups,
  97. majorPlanId, majors,
  98. conditionArgs, conditionData, subjectList,
  99. onConditionReady: exactEvent.on}
  100. provideLocal(key, payload)
  101. return payload
  102. }
  103. export const useInjectPaperExactCondition = function () {
  104. return injectLocal(key)
  105. }