useVoluntaryFormInjection.js 2.5 KB

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