useVoluntaryHeaderInjection.js 1.4 KB

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