useVoluntarySearchInjection.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import {ref, computed, onMounted} from 'vue';
  2. import {injectLocal, provideLocal, toValue} from "@vueuse/core";
  3. import {useProvideSearchModel} from "@/components/mx-condition/useSearchModelInjection";
  4. import {filters} from "@/api/webApi/collegemajor";
  5. import {useCacheStore} from "@/hooks/useCacheStore";
  6. import {useConditionPickType} from "@/components/mx-condition/modules/useConditionPickType";
  7. import {useConditionCollegeFeatures} from "@/components/mx-condition/modules/useConditionCollegeFeatures";
  8. import {useConditionCollegeType} from "@/components/mx-condition/modules/useConditionCollegeType";
  9. import {useConditionCollegeNatureTypeCN} from "@/components/mx-condition/modules/useConditionCollegeNatureTypeCN";
  10. import {useConditionCollegeLocation} from "@/components/mx-condition/modules/useConditionCollegeLocation";
  11. const key = Symbol('VOLUNTARY_SEARCH')
  12. export const useProvideVoluntarySearch = () => {
  13. const queryDefault = {
  14. majors: [],
  15. pickType: '',
  16. minScore: '',
  17. maxScore: '',
  18. sinoforeign: '',
  19. collect: '',
  20. specialProjectNation: '',
  21. specialProjectLocal: '',
  22. specialProjects: '',
  23. // props of university
  24. name: '',
  25. location: [],
  26. natureTypeCN: [],
  27. type: [],
  28. features: []
  29. }
  30. const queryParams = ref({})
  31. const formatQueryParams = () => {
  32. const vProps = ['majors', 'pickType', 'minScore', 'maxScore', 'sinoforeign', 'collect',
  33. 'specialProjectNation', 'specialProjectLocal', 'specialProjects']
  34. const uProps = ['name', 'location', 'natureTypeCN', 'type', 'features']
  35. const input = toValue(queryParams)
  36. const output = {}
  37. vProps.forEach(p => output[p] = input[p])
  38. output.university = {}
  39. uProps.forEach(p => output.university[p] = input[p].toString())
  40. return output
  41. }
  42. const filter = ref({})
  43. const {dispatchCache} = useCacheStore()
  44. const {onSearch, conditions, reset: resetCore} = useProvideSearchModel([
  45. useConditionPickType(),
  46. // 因为填报里的数据是后触发的,所以需要配置isImmediate=true
  47. useConditionCollegeFeatures(computed(() => filter.value['features'])),
  48. useConditionCollegeType(computed(() => filter.value['types'])),
  49. useConditionCollegeNatureTypeCN(computed(() => filter.value['natureTypes'])),
  50. useConditionCollegeLocation(computed(() => filter.value['locations']))
  51. ], queryParams)
  52. const reset = () => {
  53. resetCore()
  54. queryParams.value = {
  55. ...queryDefault
  56. }
  57. }
  58. onMounted(async () => {
  59. const res = await dispatchCache(filters)
  60. filter.value = {...res.data} // 创建副本,防止不能正确触发searchModelService
  61. })
  62. const options = {
  63. queryParams,
  64. formatQueryParams,
  65. conditions,
  66. onSearch,
  67. reset
  68. }
  69. provideLocal(key, options)
  70. return options
  71. }
  72. export const useInjectVoluntarySearch = () => {
  73. return injectLocal(key)
  74. }