useVoluntaryHeaderInjection.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import {computed, ref, watch} from 'vue';
  2. import {injectLocal, provideLocal, toValue} from "@vueuse/core";
  3. import {empty} from "@/uni_modules/uv-ui-tools/libs/function/test";
  4. import {getVoluntaryHeaders} from "@/api/webApi/volunteer";
  5. import {useInjectVoluntaryForm} from "@/pages/voluntary/hooks/useVoluntaryFormInjection";
  6. import {useCacheStore} from "@/hooks/useCacheStore";
  7. import {useInjectVoluntaryStep} from "@/pages/voluntary/hooks/useVoluntaryStepInjection";
  8. const key = Symbol('VOLUNTARY_HEADER')
  9. export const useProvideVoluntaryHeader = () => {
  10. const cols = ref([])
  11. const isMock = ref(undefined) // unset status
  12. const {dispatchCache} = useCacheStore()
  13. const historyYears = computed(() => {
  14. if (empty(cols.value)) return []
  15. return cols.value
  16. .filter(col => col.includes('&'))
  17. .map(col => col.split('&')[0])
  18. })
  19. const headerPlanYear = computed(() => {
  20. if (empty(cols.value)) return ''
  21. const suffix = '招生计划'
  22. const col = cols.value.find(col => col.endsWith(suffix))
  23. return col?.replace(suffix, '') || ''
  24. })
  25. const ensureHistoryYears = async (payload) => {
  26. const res = await dispatchCache(getVoluntaryHeaders, payload)
  27. cols.value = res.data
  28. isMock.value = res.isMock
  29. }
  30. const ensureHistoryYearsFromPageData = async (prevData) => {
  31. const {mode, detail: {year, isMock}} = prevData
  32. const headerPayload = {mode, year, isMock: isMock || false}
  33. await ensureHistoryYears(headerPayload)
  34. }
  35. const options = {cols, isMock, headerPlanYear, historyYears, ensureHistoryYears, ensureHistoryYearsFromPageData}
  36. provideLocal(key, options)
  37. return options
  38. }
  39. export const useInjectVoluntaryHeader = () => {
  40. return injectLocal(key)
  41. }