score-form.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <template>
  2. <view class="p-20 text-sm text-main">
  3. <uv-text v-if="isScoreLocked&&false" type="error" :text="MxConfig.scoreLockedTips"/>
  4. <uv-text v-if="isScoreUnlocked&&false" type="error" :text="MxConfig.scoreRuleTips"/>
  5. <course-selector/>
  6. <uv-line margin="15px 0 0 0"/>
  7. <uv-input v-model="model.score" :disabled="isScoreLocked" placeholder="请输入您的分数"
  8. v-bind="inputCommon">
  9. <template #prefix>您的分数:</template>
  10. </uv-input>
  11. <uv-input v-if="false" v-model="model.rank.lowestRank" disabled v-bind="inputCommon">
  12. <template #prefix>匹配位次:</template>
  13. </uv-input>
  14. <uv-input v-model="model.seatInput" placeholder="输入成绩单位次" v-bind="inputCommon">
  15. <template #prefix>填写位次:</template>
  16. </uv-input>
  17. <view class="font-[PingFang] text-content text-2xs mt-20">
  18. <uv-icon name="info-circle" class="mr3" style="display: inline-block"/>
  19. 已根据最新位次表获取分数的最低位次,您也可以修改为成绩单上的位次
  20. <template v-if="model.rank&&isScoreLocked">,位次区间[{{
  21. model.rank.highestRank
  22. }}~{{ model.rank.lowestRank }}]
  23. </template>
  24. <text class="underline text-primary" @click="goQuerySegment">查看位次</text>
  25. </view>
  26. </view>
  27. </template>
  28. <script setup>
  29. import {watch} from 'vue';
  30. import MxConfig from "@/common/mxConfig";
  31. import {createPropDefine} from "@/utils";
  32. import {useUserStore} from "@/hooks/useUserStore";
  33. import {useInjectTransfer} from "@/hooks/useTransfer";
  34. import {useInjectVoluntaryData} from "@/hooks/useVoluntaryDataInjection";
  35. import CourseSelector from "@/pages/voluntary/index/components/course-selector.vue";
  36. import debounce from "@/uni_modules/uv-ui-tools/libs/function/debounce";
  37. import {useCacheStore} from "@/hooks/useCacheStore";
  38. import {getRankByScore} from "@/api/webApi/volunteer";
  39. import {toast} from "@/uni_modules/uv-ui-tools/libs/function";
  40. const props = defineProps({
  41. model: createPropDefine({}, Object)
  42. })
  43. const inputCommon = {
  44. type: 'number',
  45. border: 'bottom',
  46. fontSize: '18px',
  47. customStyle: {height: '30px'}
  48. }
  49. const {isScoreLocked, isScoreUnlocked} = useUserStore()
  50. const {transferTo} = useInjectTransfer()
  51. const {validate, voluntaryData} = useInjectVoluntaryData()
  52. const {dispatchCache} = useCacheStore()
  53. const goQuerySegment = () => {
  54. transferTo('/pages/career/query-segment/query-segment')
  55. }
  56. const setRankByScore = async () => {
  57. const {firstSubject, score} = props.model
  58. if (!score) {
  59. props.model.seatInput = ''
  60. props.model.rank = {}
  61. return
  62. }
  63. const payload = {mode: firstSubject, scoreRank: score}
  64. const res = await dispatchCache(getRankByScore, payload)
  65. // 2次校验,防止串分
  66. if (payload.scoreRank != props.model.score) return
  67. props.model.rank = res.data
  68. const {lowestRank, highestRank} = res.data
  69. const {seatInput} = props.model
  70. if (seatInput >= highestRank && seatInput <= lowestRank) return
  71. props.model.seatInput = lowestRank
  72. }
  73. const validateForm = async function () {
  74. const {score, seatInput, rank} = props.model
  75. // score validate
  76. await validate(score)
  77. // seat validate
  78. let error = ''
  79. if (!seatInput) error = '请输入位次'
  80. else if (seatInput && !/^[1-9]\d*$/.test(seatInput)) error = '请输入合法的位次'
  81. else if (!isScoreUnlocked.value && (seatInput < rank.highestRank || seatInput > rank.lowestRank)) error = `位次必须在[${rank.highestRank}~${rank.lowestRank}]之间`
  82. if (error) {
  83. toast(error)
  84. return Promise.reject(error)
  85. }
  86. }
  87. watch([() => props.model.firstSubject, () => props.model.score * 1], async ([firstSubject, score]) => {
  88. // console.log('watch firstSubject and score', firstSubject, score)
  89. if (/*!firstSubject || */!score) return // 单招没有firstSubject
  90. // error input fix, when props.model.score assigned, watch will be triggered again.
  91. if (score > voluntaryData.value.maxScore) return props.model.score = voluntaryData.value.maxScore
  92. else if (score < 0) return props.model.score = 0
  93. // silence validation
  94. await validate(score, true)
  95. debounce(setRankByScore)
  96. })
  97. defineExpose({validate: validateForm})
  98. </script>
  99. <style scoped>
  100. </style>