ver.vue 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <template>
  2. <div class="input-ver">
  3. <el-input v-model="code" @change="$emit('change', code)" placeholder="请输入验证码"></el-input>
  4. <el-button :class="{grey: running}" type="text" @click="run">
  5. <span>{{text}}</span>
  6. </el-button>
  7. </div>
  8. </template>
  9. <script>
  10. let time = 60;
  11. let text = "获取验证码";
  12. let interval;
  13. export default {
  14. data() {
  15. return {
  16. // 验证码
  17. code: "",
  18. time: time + 1,
  19. running: false
  20. };
  21. },
  22. computed: {
  23. text() {
  24. if (this.time < 1 || this.time > time) {
  25. this.running = false;
  26. if (this.time < 1) {
  27. clearInterval(interval);
  28. this.time = time + 1;
  29. this.running = false;
  30. }
  31. return text;
  32. }
  33. return `重新获取(${this.time}s)`;
  34. }
  35. },
  36. mounted() {},
  37. methods: {
  38. run() {
  39. if (this.running) return;
  40. this.$emit("click", () => {
  41. this.running = true;
  42. this.time--;
  43. interval = setInterval(() => {
  44. this.time--;
  45. }, 1000);
  46. });
  47. }
  48. }
  49. };
  50. </script>
  51. <style scoped>
  52. .input-ver {
  53. position: relative;
  54. }
  55. #getCode {
  56. /* position: relative; */
  57. /* width: 100%; */
  58. /* z-index: 20; */
  59. }
  60. .el-button {
  61. position: absolute;
  62. top: 0;
  63. right: 10px;
  64. }
  65. .grey {
  66. color: #909399;
  67. }
  68. </style>