版权声明:版权所有_lrxu https://blog.csdn.net/u013368397/article/details/81985050
hellow大家好,许久不更新博客了,最近呢主要是由于个人的私事比较多,没能抽出时间出来给大家分享,好不闲聊了。今天呢主要和大家分享小程序中遇到的短信验证界面怎么做。额,到这里我觉得应该就结束了。
哈哈,因为我觉得,确实没什么东西,等我给大家放出代码出来就知道了。不过这里要声明一下,这个是项目上兄弟做的,至于哪位仁兄,我还真给忘了。如果哪位兄弟说这不是原创啊,啊,不好意思 ,确实不是,主要呢我觉得对大家有帮助,就拿出来分享一下,这个见仁见智的事情,也无所谓了,只要咱么的出发点没错就okay。好,老样子,给大家上一波图和代码。
这里页面结构。
<!--index.wxml-->
<view class="container">
<view class="header">
<view class="tel">
<view class="phone">
<text class="text-tel">手机号</text>
<text class="text-tel" style="margin-left:30px;">150 ***** 759</text>
</view>
<button class='getcode' bindtap="captcha" disabled="{{captchaDisabled}}" ><text style="font-size:12px;color:#628AE3">{{captchaLabel}}</text></button>
</view>
<view class="code">
<text class="text-tel">验证码</text>
<view class="input">
<input type="number" maxlength='6' placeholder="请输入验证码" placeholder-class="place" bindblur="blur" bindinput="onChange"></input>
</view>
</view>
</view>
<view class="{{disabled?'footer':'footer-disable'}}" hover-class="hover-footer" bindtap="{{disabled?'submit':''}}">
<text style="font-size:16px;color:#FFF;">验证</text>
</view>
</view>
这里呢是页面逻辑
//index.js
//获取应用实例
const app = getApp()
const timer = require('../../utils/timer.js')
Page({
data:{
captchaLabel: '获取验证码',
seconds: timer.length,
captchaDisabled: false,
disabled: false,
},
blur:function(){
},
onChange: function (e) {
const cursor = e.detail.cursor;//填写的验证码位数
if (cursor >=6 && !this.data.disabled) {
this.setData({ disabled: true });
} else if (this.data.disabled) {
this.setData({ disabled: false });
}
},
captcha: function (e) {
//发送验证码。倒计时结束之前设置按钮diable属性,由captchaDisabled来控制
this.setData({
captchaDisabled: true
});
// 立刻显示重发提示,不必等待倒计时启动
this.setData({
captchaLabel: timer.length + 'S'
});
// 启动以1s为步长的倒计时
var interval = setInterval(() => {
timer.countdown(this);
}, 1000);
// 停止倒计时,这里一定要将损耗内存的interval关掉,
setTimeout(function () {
clearInterval(interval);
}, timer.length * 1000);
if (this.data.seconds == timer.length) {
wx.showToast({
title: '发送成功'
});
}
},
submit:function(){
wx.navigateBack({
delta: 2,
})
}
})
这里呢是抽出来的timer.js工具包
'use strict'
const length = 10
const countdown = that => {
var seconds = that.data.seconds;
var captchaLabel = that.data.captchaLabel;
if (seconds <= 1) {
//当剩余时间小于1秒,将将‘获取验证码’重新显示,并将将按钮的disable属性置回。
captchaLabel = '获取验证码';
seconds = length;
that.setData({
captchaDisabled: false
});
} else {
captchaLabel = --seconds + 'S'
}
that.setData({
seconds: seconds,
captchaLabel: captchaLabel
});
}
module.exports = {
countdown:countdown,
length:length
}
源码下载地址:https://download.csdn.net/download/u013368397/10622891
好了,到这就告一段落了,最近本人在做小程序的定位,用的是腾通地图sdk,下一片文章将会着重介绍一下,小程序之地图篇