ver_code.vue 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <template>
  2. <div class="container">
  3. <el-input class="input" placeholder="请输入验证码" v-model="value" @input="onInput"></el-input>
  4. <button class="ver-btn" @click="clickBtn">{{remind}}</button>
  5. </div>
  6. </template>
  7. <script>
  8. let remindInit = '获取验证码'
  9. let max = 60
  10. let interval
  11. export default {
  12. data() {
  13. return {
  14. // 提示语
  15. remind: remindInit,
  16. // 读秒中
  17. running: false,
  18. value: '',
  19. max,
  20. }
  21. },
  22. methods: {
  23. onInput(value) {
  24. this.value = value
  25. this.$emit('change', value)
  26. },
  27. clickBtn() {
  28. if (this.running) return
  29. this.$emit('click', () => {
  30. this.running = true
  31. this.remind = `${this.max}秒`
  32. interval = setInterval(() => {
  33. this.max -= 1
  34. if (this.max < 1) {
  35. clearInterval(interval)
  36. this.max = max
  37. this.remind = remindInit
  38. this.running = false
  39. } else {
  40. this.remind = `${this.max}秒`
  41. }
  42. }, 1000)
  43. })
  44. },
  45. }
  46. }
  47. </script>
  48. <style scoped>
  49. .container {
  50. position: relative;
  51. align-items: flex-start;
  52. }
  53. .input {
  54. width: 311px;
  55. height: 44px;
  56. }
  57. .ver-btn {
  58. position: absolute;
  59. top: 11px;
  60. right: 8px;
  61. color: var(--mainColor);
  62. font-size: 13px;
  63. }
  64. </style>