ver.vue 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. console.log("running+++++++++");
  40. console.log(this.running);
  41. if (this.running) return;
  42. this.$emit("click", () => {
  43. this.running = true;
  44. this.time--;
  45. interval = setInterval(() => {
  46. this.time--;
  47. }, 1000);
  48. });
  49. }
  50. }
  51. };
  52. </script>
  53. <style scoped>
  54. .input-ver {
  55. position: relative;
  56. }
  57. #getCode {
  58. /* position: relative; */
  59. /* width: 100%; */
  60. /* z-index: 20; */
  61. }
  62. .el-button {
  63. position: absolute;
  64. top: 0;
  65. right: 10px;
  66. }
  67. .grey {
  68. color: #909399;
  69. }
  70. </style>