微信小程序获取验证码60秒倒计时模板

微信小程序获取验证码60秒倒计时模板

效果图

微信小程序获取验证码60秒倒计时模板
微信小程序获取验证码60秒倒计时模板

index.wxml

<view class="Info">
      <view class="Num">
          <view>6位数字验证码</view>
          <view class="verification_time">
              <button bindtap='MosendSms' disabled='{{MoDisabled}}' class="Box_hid" style='color:{{MoColor}}' type="button">{{MoCodeMsg}}</button>
          </view>
      </view>
      <view class='verification'>
          <block wx:for="{{Length}}" wx:key="item">
              <input class='frame' value="{{Showboxval.length>=index+1?Showboxval[index]:''}}" disabled catchtap='InputTap' />
          </block>
      </view>
      <view class="error" wx:if="{{error}}">验证码输入错误</view>
      <input name="password" password="{{true}}" class='ipt' maxlength="{{Length}}" focus="{{isFocus}}" bindinput="FocuInput" />
</view>

index.js

data: {
    code""//后端验证码
    Jurisdictiontrue//是否有权限
    errorfalse//错误提示
    Length6//输入框个数
    isFocustrue//聚焦
    Showboxval""//输入的内容
    MoDisabledfalse//验证码是否可点击
    MoCodeMsg'获取验证码'//文案
    MoMsgWait60//时间秒
    MoColor'rgba(40, 200, 122, 1)'//默认验证码字体颜色
},

/**
* 生命周期函数--监听页面加载 正常第一次进来先调用一次
*/

onLoad(options) {
    this.MosendSms() // 60秒后重新获取验证码
},

// 60秒后验证码
sendSbinms() {
    wx.showToast({
        title'短信验证码发送成功,请注意查收',
        icon'none'
    })
    this.setData({
        MoCodeMsgthis.data.MoMsgWait + "  s",
        MoColor'rgba(40, 200, 122, 1)',
        MoMsgWaitthis.data.MoMsgWait - 1,
        MoDisabledtrue
    });
    let inter = setInterval(function ({
        this.setData({
            MoCodeMsgthis.data.MoMsgWait + "  s",
            MoColor'rgba(40, 200, 122, 1)',
            MoMsgWaitthis.data.MoMsgWait - 1,
            MoDisabledtrue
        });
        if (this.data.MoMsgWait < 0) {
            clearInterval(inter)
            this.setData({
                MoCodeMsg"重新获取",
                MoMsgWait60,
                MoDisabledfalse,
                MoColor'rgba(40, 200, 122, 1)'
            });
        }
        //注意后面的bind绑定,最关键。不然又是未定义,无法使用外围的变量;
    }.bind(this), 1000);
},

// 点击获取验证码
MosendSms() {
   if (this.data.Jurisdiction) {
       this.sendSbinms() // 60秒后重新获取验证码
       this.obtain();  //后端接口 获取验证码
   } else {
       wx.showToast({
          titlethis.data.massage ? this.data.massage : '手机号未注册',
           icon'error',
           duration3000
       })
   }
},
obtain() {
    let params = {
        phonethis.data.rstProduct,
        type1
    }
    appletValidateCode(params).then((res) => {
        this.setData({
            code: res.data.data,
        });
    }).catch((res) => {})
},
//验证码输入框
FocuInput(e) {
    let that = this;
    let inputValue = e.detail.value;
    that.setData({
        Showboxval: inputValue,
    })
    if (inputValue.length === 6) {
        if (inputValue == this.data.code) {
            this.setData({
                errorfalse,
            });
        } else {
            this.setData({
                errortrue,
            });
        }
    }
},
//验证码输入框点击
InputTap() {
    let that = this;
    that.setData({
        isFocustrue,
    })
}, 

index.wxss

.Info {
    padding138rpx 32rpx 0 32rpx;
}

.verification {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top30rpx;
    font-size32rpx;
}

.Num {
    display: flex;
    align-items: center;
    justify-content: space-between;
    colorrgba(0000.65);
    font-size30rpx;
    font-family: PingFang SC-Regular, PingFang SC;
    font-weight400;
}

.frame {
    width80rpx;
    height80rpx;
    border-radius2px;
    border2rpx solid #DEDEDE;
    display: flex;
    justify-content: center;
    align-items: center;
    text-align: center;
    font-size32rpx;
    font-family: PingFang SC-Medium, PingFang SC;
    font-weight500;
    colorrgba(0000.65);
}

.ipt {
    width0;
    height0;
}

.Box_hid {
    font-size30rpx;
    background#fff !important;
    text-align: left;
    colorrgba(402001221!important;
    padding-right0 !important;
    font-family: PingFang SC-Regular, PingFang SC;
    font-weight400 !important;
}

.Box_hid::after {
    border: none;
}

.error {
    color#F24236;
    margin-top8rpx;
    font-size28rpx;
    font-family: PingFang SC-Regular, PingFang SC;
    font-weight400;
}

文章出自:https://juejin.cn/post/7221808657532174393

作者:switch


原文始发于微信公众号(前端24):微信小程序获取验证码60秒倒计时模板

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/216415.html

(0)
李, 若俞的头像李, 若俞

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!