ver_img.vue 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <template>
  2. <div class="input-ver">
  3. <el-input v-model="code" @change="$emit('change', code)" placeholder="请输入验证码"></el-input>
  4. <img @click="getUrl" v-if="url" :src="url" alt="img-url">
  5. </div>
  6. </template>
  7. <script>
  8. let running = false
  9. let time = 60
  10. let text = '获取验证码'
  11. let interval
  12. export default {
  13. data() {
  14. return {
  15. url: '',
  16. code: '',
  17. time: time + 1,
  18. }
  19. },
  20. computed: {
  21. text() {
  22. if(this.time < 1 || this.time > time) {
  23. running = false
  24. if(this.time < 1) {
  25. clearInterval(interval)
  26. this.time = time + 1
  27. }
  28. return text
  29. }
  30. return `重新获取(${this.time}s)`
  31. }
  32. },
  33. mounted() {
  34. this.getUrl()
  35. },
  36. methods: {
  37. run() {
  38. if(running) return
  39. this.time--
  40. interval = setInterval(() => {
  41. this.time--
  42. }, 1000)
  43. },
  44. getUrl() {
  45. this.url = `/api/captcha/image?_${~~(Math.random() * 1e7)}`
  46. }
  47. }
  48. }
  49. </script>
  50. <style scoped>
  51. .input-ver {
  52. display: flex;
  53. align-items: center;
  54. }
  55. .el-input {
  56. width: 195px;
  57. margin-right: 10px;
  58. }
  59. img {
  60. flex: 1;
  61. width: 115px;
  62. height: 40px;
  63. cursor: pointer;
  64. }
  65. </style>