useVoluntaryFormInjection.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import {ref, computed, watch} from 'vue';
  2. import {injectLocal, provideLocal, toValue} from "@vueuse/core";
  3. import {array, empty} from "@/uni_modules/uv-ui-tools/libs/function/test";
  4. import {useUserStore} from "@/hooks/useUserStore";
  5. import {zytbBatches} from "@/api/webApi/volunteer";
  6. import {useCacheStore} from "@/hooks/useCacheStore";
  7. const key = Symbol('VOLUNTARY_FORM')
  8. export const useProvideVoluntaryForm = function () {
  9. const {currentUser} = useUserStore()
  10. const {dispatchCache} = useCacheStore()
  11. const model = ref({
  12. firstSubject: '',
  13. lastSubject: [],
  14. score: '',
  15. rank: {},
  16. seatInput: ''
  17. })
  18. const batch = ref({})
  19. const batchList = ref([])
  20. const courses = computed(() => {
  21. return getCourses(toValue(model))
  22. })
  23. const mode = computed(() => {
  24. return courses.value.toString()
  25. })
  26. const simpleMode = computed(() => {
  27. if (courses.value.length === 1) return courses.value.toString()
  28. return courses.value.map(i => i.substring(0, 1)).join('')
  29. })
  30. const getCourses = (modelVal) => {
  31. const results = []
  32. const {firstSubject, lastSubject} = modelVal
  33. if (!empty(firstSubject)) results.push(firstSubject)
  34. if (!empty(lastSubject) && array(lastSubject)) results.push(...lastSubject)
  35. return results
  36. }
  37. const getMode = (modelVal) => {
  38. return getCourses(modelVal).toString()
  39. }
  40. const reloadScoreAndMode = async () => {
  41. const {mode, score, seatInput} = toValue(currentUser)
  42. const modeParams = mode?.split(',') || []
  43. model.value.score = score || ''
  44. model.value.firstSubject = modeParams[0]
  45. model.value.lastSubject = modeParams.slice(1) || []
  46. model.value.seatInput = (seatInput || 0) * 1 // This value may be changed by setRankByScore feature
  47. if (!score) model.value.seatInput = ''
  48. }
  49. const loadBatchList = async () => {
  50. const m = toValue(mode)
  51. const {score} = toValue(model)
  52. const res = await dispatchCache(zytbBatches, {mode: m, score})
  53. batchList.value = res.rows
  54. }
  55. const getBatchList = async (modelVal) => {
  56. const m = getMode(modelVal)
  57. const {score} = modelVal
  58. const res = await dispatchCache(zytbBatches, {mode: m, score})
  59. return res.rows
  60. }
  61. watch(currentUser, () => reloadScoreAndMode(), {immediate: true})
  62. const options = {
  63. model, batch, batchList,
  64. mode, simpleMode,
  65. loadBatchList,
  66. getBatchList
  67. }
  68. provideLocal(key, options)
  69. return options
  70. }
  71. export const useInjectVoluntaryForm = function () {
  72. return injectLocal(key)
  73. }