index.vue 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <template>
  2. <el-input text maxlength="4" placeholder="请输入验证码" clearable autocomplete="off" v-bind="$attrs">
  3. <template #prefix>
  4. <el-icon class="el-input__icon"><ele-Message /></el-icon>
  5. </template>
  6. <template #suffix>
  7. <el-link type="primary" :underline="false" v-show="state.status !== 'countdown'" :disabled="state.status === 'countdown'" @click="onClick">{{
  8. state.status === 'ready' ? state.startText : state.endText
  9. }}</el-link>
  10. <el-countdown
  11. v-show="state.status === 'countdown'"
  12. :format="state.changeText"
  13. :value="state.countdown"
  14. value-style="font-size:var(--el-font-size-base);color:var(--el-color-primary)"
  15. @change="onChange"
  16. />
  17. </template>
  18. </el-input>
  19. </template>
  20. <script lang="ts" setup name="my-input-code">
  21. import { reactive } from 'vue'
  22. const props = defineProps({
  23. seconds: {
  24. type: Number,
  25. default: 60,
  26. },
  27. startText: {
  28. type: String,
  29. default: '获取验证码',
  30. },
  31. changeText: {
  32. type: String,
  33. default: 's秒重新获取',
  34. },
  35. endText: {
  36. type: String,
  37. default: '重新获取',
  38. },
  39. })
  40. const state = reactive({
  41. status: 'ready',
  42. startText: props.startText,
  43. changeText: props.changeText,
  44. endText: props.endText,
  45. countdown: Date.now(),
  46. })
  47. const onClick = () => {
  48. state.status = 'countdown'
  49. state.countdown = Date.now() + (props.seconds + 1) * 1000
  50. }
  51. const onChange = (value: number) => {
  52. if (value < 1000) state.status = 'finish'
  53. }
  54. </script>
  55. <style scoped lang="scss">
  56. :deep(.el-statistic__content) {
  57. font-size: var(--el-font-size-base);
  58. }
  59. </style>