写一个发送验证码组件

<template>
    <el-button class="count-button" type="primary" size="small" :style="{ width:contentWidth+'px', }" :disabled="isStart"  @click="handleStart">
        {{ !isStart ? '获取短信验证码' : `${currentCount}秒后重新获取` }}
    </el-button>
</template>

<script>
import { sms } from "@/api/home/home.js";
import md5 from 'js-md5';
export default {
    name: "CountButton",
    props: {
        count: {
            type: Number,
            default: 60,
        },
        FormData: {
            type: Object
        },
        contentWidth: {
            // 容器宽度
            type: Number,
            default: 116
        },
    },
    data() {
        return {
            isStart: false,
            currentCount: 60,
            timerId: null
        }
    },
    methods: {
        handleStart() {
            if (!this.FormData.user_name || !this.FormData.password) {
                let arr = [];
                !this.FormData.user_name ? arr.push('user_name') : '';
                !this.FormData.password ? arr.push('password') : '';
                this.$emit('monitor', arr)
                this.$messages.warning("获取验证码需要填写用户名,密码!");
            } else {
                let params = {
                    user_name: this.FormData.user_name.trim(),
                    password: md5(this.FormData.password),
                };
                sms(params.user_name, params.password).then(res => {
                    console.log(res)
                    // TODO:处理响应
                    if (res.data.status == 0) {
                        this.start()
                        this.$messages.success(res.data.message);
                    } else {
                        this.$messages.warning(res.data.message);
                    }
                })

            }

        },
        start() {
            this.timerId = null
            if (this.isStart || !!this.timerId) {
                return;
            }
            this.isStart = true;
            this.timerId = setInterval(() => {
                if (this.currentCount === 1) {
                    this.stop();
                    this.currentCount = this.count;
                } else {
                    this.currentCount -= 1;
                }
            }, 1000);
        },
        stop() {
            this.isStart = false;
            this.timerId = null;
            this.clear();
        },
        clear() {
            this.timerId && clearInterval(this.timerId);
        }

    }

}
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值