vue组件系列-验证码倒计时组件

复制代码
<style scoped>
    .veri-code{
        width: 100%;
        .veri-send{
            width: 100%;
            height: 91px;
            position:relative;
            overflow:hidden;
            >.sendBtn{
                background:url(../assets/images/hongbao_code_btn.jpg) 100% 100% no-repeat;
                background-size: contain;
                display: inline-block;
                width: 171px;
                height: 91px;
                z-index:2;
                position: absolute;
                right:0;
                top:0;
                span{
                    display: inline-block;
                    width: 171px;
                    height: 91px;
                    line-height: 91px;
                    text-align: center;
                    font-family: PingFangSC;
                    font-size: 32px;
                    font-weight: 600;
                    color: #c31222;
                }
            }
        }
        .veri-activated{
            >.sendBtn{
                span{
                    background:url(../assets/images/hongbao_code_btn_disabled.jpg) 100% 100% no-repeat;
                    background-size: contain;
                    color:#8e0612;
                }
            }
        }
    }
</style>

<template>
    <div class="veri-code">
        <div :class="['veri-send',{'veri-activated':isSended || activityEnd}]">
            <txt-input placeholder="请输入手机号" max-length="11" type="tel"></txt-input>
            <div class="sendBtn">
                <span v-if="!isSended" @click="getVeriCode">验证码</span>
                <span v-if="isSended">{{seconds}}s</span>
            </div>
        </div>
    </div>
</template>

<script>
    import Vue from 'vue';
    import TxtInput from './TxtInput.vue';
    import bus from '../lib/bus';
    export default Vue.extend({
        name:'veri-code',
        components:{
            'txt-input':TxtInput
        },
        data(){
            return {
                seconds:60,
                left_time:0,
                isSended: false
            }
        },
        props: {
            sended: {
                default: this.isSended
            },
            activityEnd:{
                default: false
            }
        },
        watch:{
            sended: function(val,oldVal){
                this.isSended = this.sended;
                if(this.sended & this.left_time == 0){
                    let _this = this,flag = this.seconds;
                    let setSended = () => {
                        clearInterval(timmer);
                        _this.isSended = false;
                        _this.seconds = flag;
                        _this.$parent.codeSended = false;
                    };
                    let timmer = setInterval(function(){
                        _this.seconds > 1 ? _this.seconds-- : setSended()
                    },1000);
                }
            },
            left_time: function(val,oldVal){
                this.defaultState()
            }
        },
        methods: {
            getVeriCode: function(){
                this.$parent.getVeriCode()
            },
            defaultState: function(){
              if(this.left_time != 0){
                let _this = this,flag = this.seconds;
                this.seconds = this.left_time
                this.sended = true
                let setSended = () => {
                    clearInterval(timmer);
                    _this.sended = false;
                    _this.seconds = flag;
                    this.left_time = 0;
                };
                let timmer = setInterval(function(){
                    _this.seconds > 0 ? _this.seconds-- : setSended()
                },1000);
              }
            },
        },
        // created(){
        //   this.defaultState()
        // }
    })
</script>
复制代码

 

posted @   创业男生  阅读(235)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
历史上的今天:
2013-01-17 DataTable批量插入数据库四种方式比较
点击右上角即可分享
微信分享提示