useVoluntaryAssistantInjection.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import {computed} from 'vue';
  2. import {createEventHook, injectLocal, provideLocal, toValue} from "@vueuse/core";
  3. import {sleep, toast} from "@/uni_modules/uv-ui-tools/libs/function";
  4. import MxConst from "@/common/mxConst";
  5. import {empty} from "@/uni_modules/uv-ui-tools/libs/function/test";
  6. import {saveZhiyuan} from "@/api/webApi/volunteer";
  7. const key = Symbol('VOLUNTARY_ASSISTANT')
  8. export const useProvideVoluntaryAssistant = function (stepSvc, dataSvc, formSvc, cartSvc, highlightSvc, scrollHeight) {
  9. const {title, currentStep} = stepSvc
  10. const {id, name, locking, selectedList, resetCart} = cartSvc
  11. const {voluntaryData} = dataSvc
  12. const {batch, model, mode, batchesList} = formSvc
  13. const {formedMajors, resetHighlight} = highlightSvc
  14. const beforeBack = createEventHook()
  15. const afterBack = createEventHook()
  16. const beforeForward = createEventHook()
  17. const afterForward = createEventHook()
  18. const complete = createEventHook()
  19. const handleBack = async function () {
  20. if (currentStep.value == 0) return
  21. await beforeBack.trigger()
  22. currentStep.value -= 1
  23. await afterBack.trigger()
  24. }
  25. const handleForward = async function () {
  26. if (currentStep.value == 2) return
  27. await beforeForward.trigger()
  28. currentStep.value += 1
  29. await afterForward.trigger()
  30. }
  31. const navBinding = computed(() => ({
  32. title: title.value,
  33. subTitle: name.value,
  34. leftIcon: currentStep.value > 0 ? 'arrow-left' : '',
  35. onLeftClick: handleBack
  36. }))
  37. const voluntaryDataCalculate = computed(() => {
  38. // NOTE: will override by history voluntary data in edit mode
  39. const param = toValue(voluntaryData) || {}
  40. const {batch: batchId} = toValue(batch)
  41. const defaultLimit = {groups: 9999, profession: 9999}
  42. // noinspection JSUnresolvedVariable
  43. const firedLimit = param.batches?.find(i => i.batch == batchId) || defaultLimit
  44. return {
  45. ...param,
  46. firedLimit
  47. }
  48. })
  49. const calculateScoreBatchRange = computed(() => {
  50. // for re-use
  51. const vData = toValue(voluntaryData)
  52. const vBatch = toValue(batch)
  53. if (empty(vData) || vBatch.batch) return [0, 0]
  54. const maxScore = vData.maxScore
  55. const minBatch = vBatch.score2 || vBatch.score1
  56. let maxBatch = maxScore
  57. const batchIndex = batchesList.value.indexOf(this.batch)
  58. if (batchIndex > 0) {
  59. const upBatch = batchesList.value[batchIndex - 1]
  60. maxBatch = upBatch.score2 || upBatch.score1
  61. }
  62. return [minBatch, Math.min(maxScore, maxBatch)]
  63. })
  64. const save = async (isMockHeader) => {
  65. if (toValue(id) && !toValue(name)) {
  66. toast('请设置志愿表名称')
  67. return Promise.reject(false)
  68. }
  69. const wishes = selectedList.value.map(item => ({
  70. uniqueCode: item.uniqueCode,
  71. universityId: item.university.id,
  72. collegeCode: item.recruitPlan.collegeCode,
  73. code: item.university.code,
  74. jCode: item.jCode,
  75. name: item.university.name,
  76. pickType: item.pickType,
  77. enrollRatio: item.enrollRatio,
  78. enrollRatioText: item.enrollRatioText,
  79. ranking: item.university.ranking,
  80. rankingOfEdu: item.university.rankingOfEdu,
  81. seat: item.history?.seat,
  82. marjors: item.majors
  83. .filter(major => major.selected)
  84. .sort(MxConst.recommendMajorSortFn)
  85. .map(major => ({
  86. id: major.id,
  87. code: major.marjorBelongs,
  88. name: major.marjorName,
  89. pickType: item.pickType,
  90. submitMajorId: major.history?.id,
  91. enrollRatio: major.enrollRatio,
  92. enrollRatioText: major.enrollRatioText,
  93. enrollFluctuate: major.enrollFluctuate
  94. }))
  95. }))
  96. if (wishes.length < 1) {
  97. // NOTE: 职高对口院校较少,降低填报限制
  98. toast('至少选择1个志愿组')
  99. return Promise.reject(false)
  100. }
  101. locking.value = true
  102. uni.showLoading()
  103. const vBatch = toValue(batch)
  104. const vModel = toValue(model)
  105. const vMode = toValue(mode)
  106. const data = {
  107. batch: vBatch.batch,
  108. detail: {
  109. batch: {
  110. batch: vBatch.batch,
  111. name: vBatch.name,
  112. score1: vBatch.score1 || '',
  113. score2: vBatch.score2 || '',
  114. scoreBatchLimit: calculateScoreBatchRange.value[1],
  115. recommand: true,
  116. scores: [],
  117. wishes: wishes,
  118. sort: voluntaryDataCalculate.value.sort,
  119. paramBatches: [voluntaryDataCalculate.value.firedLimit],
  120. highlightMajorIds: Object.keys(formedMajors.value)
  121. },
  122. mode: vMode,
  123. score: vModel.score,
  124. seatInput: vModel.seatInput,
  125. year: vBatch.year,
  126. isMock: isMockHeader
  127. },
  128. id: id.value,
  129. name: name.value
  130. }
  131. let res = null
  132. try {
  133. res = await saveZhiyuan(data)
  134. toast('保存成功,即将跳转3')
  135. await sleep(1000)
  136. toast('保存成功,即将跳转2')
  137. await sleep(1000)
  138. toast('保存成功,即将跳转1')
  139. await sleep(500)
  140. await complete.trigger(res.data)
  141. } finally {
  142. locking.value = false
  143. uni.hideLoading()
  144. }
  145. }
  146. const resetAll = () => {
  147. resetCart()
  148. resetHighlight()
  149. }
  150. const options = {
  151. navBinding,
  152. handleBack,
  153. handleForward,
  154. voluntaryDataCalculate,
  155. save,
  156. resetAll,
  157. scrollHeight,
  158. onBeforeBack: beforeBack.on,
  159. onAfterBack: afterBack.on,
  160. onBeforeForward: beforeForward.on,
  161. onAfterForward: afterForward.on,
  162. onComplete: complete.on
  163. }
  164. provideLocal(key, options)
  165. return options
  166. }
  167. export const useInjectVoluntaryAssistant = function (useDefaultVal) {
  168. return injectLocal(key, useDefaultVal ? {voluntaryDataCalculate: {}} : undefined)
  169. }