vCode.vue 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <template>
  2. <component
  3. :type="type"
  4. :loading="inSend"
  5. class="d-inline-block rounded-xs"
  6. :size="size"
  7. :plain="plain"
  8. :is="tag"
  9. @click="send"
  10. >{{load?(num+'S'):text}}</component>
  11. </template>
  12. <script>
  13. import vButton from './vButton.vue'
  14. import serve from "@/api/serve";
  15. export default {
  16. name: "vCode",
  17. props: {
  18. url: {
  19. default: "",
  20. type: String,
  21. required: true,
  22. },
  23. data: {
  24. default: undefined,
  25. required: false,
  26. },
  27. tag: {
  28. default: "v-button",
  29. type: String,
  30. require: false,
  31. },
  32. plain: {
  33. default: true,
  34. type: Boolean,
  35. require: false,
  36. },
  37. size: {
  38. default: 'mini',
  39. type: String,
  40. require: false,
  41. },
  42. type: {
  43. default: 'green-plain',
  44. type: String,
  45. require: false,
  46. },
  47. },
  48. components:{
  49. vButton
  50. },
  51. data() {
  52. return {
  53. load: false,
  54. inSend: false,
  55. num: 60,
  56. Interval: null,
  57. };
  58. },
  59. computed: {
  60. text(){
  61. return ` ${this.$t('common.getCode')} `
  62. }
  63. },
  64. methods: {
  65. // 发送验证码
  66. send() {
  67. if (this.load) return;
  68. this.inSend = true;
  69. serve.post(this.url, this.data)
  70. .then(() => {
  71. this.inSend = false;
  72. this.load = true;
  73. this.countDown();
  74. this.$toast(this.$t('common.sendSuccess'));
  75. })
  76. .catch(() => {
  77. this.inSend = false;
  78. });
  79. },
  80. countDown() {
  81. clearInterval(this.Interval);
  82. this.Interval = setInterval(() => {
  83. if (this.num <= 0) {
  84. this.num = 60;
  85. this.load = false;
  86. clearInterval(this.Interval);
  87. return;
  88. }
  89. this.num--;
  90. }, 1000);
  91. },
  92. },
  93. };
  94. </script>