微信小程序?qū)崿F(xiàn)發(fā)送驗(yàn)證碼按鈕效果
本文實(shí)例為大家分享了微信小程序發(fā)送驗(yàn)證碼按鈕效果展示的具體代碼,供大家參考,具體內(nèi)容如下
首先上圖,最終效果如下:

實(shí)現(xiàn)關(guān)鍵點(diǎn)
- 獲取驗(yàn)證碼按鈕無邊框: 可以用 button::after{ border: none; } 來去除邊框,或者直接用view綁定點(diǎn)擊事件。本例子中沒有使用button
- 點(diǎn)擊發(fā)送后,60秒內(nèi)按鈕處于disable狀態(tài)
- 點(diǎn)擊發(fā)送后,text分為剩余秒數(shù)和后綴兩部分
- .form_group 使用 flex 布局
代碼
.wxml
<view class="form_group">
<input type="text" placeholder="手機(jī)號碼" placeholder-class="placeholder_style" data-name="data_phone" value="{{data_phone}}" bindinput='getInputKey' />
</view>
<view class="form_group">
<input type="text" class="sendmsg_input" placeholder="短信驗(yàn)證碼" data-name="data_code" value="{{data_code}}" placeholder-class="placeholder_style" bindinput='getInputKey' />
<view class='vertificate' bindtap="getVerificationCode">{{time}}
<text>{{suffix}}</text>
</view>
</view>.wxss
.form_group {
display: flex;
flex-direction: row;
justify-content: space-between;
}
.form_group input, .form_group picker {
width: 676rpx;
border-bottom: 1px solid #ddd;
height: 121rpx;
padding-left: 20rpx;
font-family: PingFangSC-Regular;
font-size: 32rpx;
letter-spacing: 0;
line-height: 121rpx;
}
.form_group .sendmsg_input {
width: 370rpx;
}
.form_group .vertificate {
width: 326rpx;
border-bottom: 1px solid #ddd;
height: 121rpx;
padding-left: 20rpx;
font-family: PingFangSC-Regular;
font-size: 32rpx;
letter-spacing: 0;
line-height: 121rpx;
text-align: center;
color: #34c9c3;
}
.vertificate text {
color: gray;
}
.placeholder_style {
font-family: PingFangSC-Regular;
font-size: 32rpx;
color: #dbdbdb;
letter-spacing: 0;
}.js
import SignupService from '../service/sign-up.service.js';
import HTTP from '../../../utils/http.js';
import Util from '../../../utils/util.js';
let _signupService = new SignupService();
let _http = new HTTP();
let _util = new Util();
Page({
data: {
time: "獲取驗(yàn)證碼",
currentTime: 61,
disabled:false,
suffix:'',
data_phone:'',
data_code:'',
},
...
// 獲取輸入框的值
getInputKey(e) {
let key = e.currentTarget.dataset.name;
let value = e.detail.value;
this.setData({
[key]: value
})
},
// 獲取驗(yàn)證碼
getVerificationCode() {
let _this = this;
if (!_this.data.disabled) {
_this.getCode();
}
},
getCode() {
let _this = this;
let phone = _this.data.data_phone;
if (_util.isPhoneAvailable(phone)) {
_signupService.getCode(phone).then(res=>{ // 調(diào)用后端API,獲取手機(jī)驗(yàn)證碼
_util.showToast('success',"已發(fā)送");
_this.setData({
disabled: true
})
},err=>{
_util.showToast('none',"發(fā)送失敗")
});
// 設(shè)置發(fā)送驗(yàn)證碼按鈕樣式
let interval = null;
let currentTime = _this.data.currentTime;
interval = setInterval(function() {
currentTime--;
_this.setData({
time: currentTime,
suffix: '秒后可重新獲取'
})
if (currentTime <= 0) {
clearInterval(interval)
_this.setData({
time: '重新發(fā)送',
suffix: '',
currentTime: 61,
disabled: false
})
}
}, 1000)
} else {
_util.showToast('none','請輸入正確的手機(jī)號碼。');
}
},
sign-up.service.js
//獲取手機(jī)驗(yàn)證碼
getCode(phone){
return _http.request({
type: 'post',
url: '/account/validate-codes',
data: {
phone:phone
}
})
}http.js: 封裝wx.request 為Promise
class HTTP {
request(param){
let _this = this;
let baseUrl = '.......';
return new Promise((resolve, reject) => {
let access_token = wx.getStorageSync('access_token');
wx.request({
method: param.type || 'get',
url: baseUrl+ param.url || '',
data: param.data || null,
header: access_token ? {
'content-type': 'application/x-www-form-urlencoded',
"Authorization": `Bearer ${access_token}`
} : {
'content-type': 'application/x-www-form-urlencoded',
},
success: (res => {
if (res.statusCode === 200 || res.statusCode === 201) {
//200: 服務(wù)端業(yè)務(wù)處理正常結(jié)束
resolve(res.data)
} else {
//其它錯誤,提示用戶錯誤信息
/***
* 需要根據(jù)接口文檔改?。?!
*/
reject(res)
}
}),
fail: (err => {
if(err.responseJSON){
reject(err.responseJSON.message)
}else{
reject(err);
}
})
});
});
}
}
export default HTTP;util.js
// 驗(yàn)證手機(jī)號碼是否有效
isPhoneAvailable(phone) {
var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
if (!myreg.test(phone)) {
return false;
} else {
return true;
}
}
//小程序彈框提示
showToast(icon,msg,duration=2000){
wx.showToast({
title: msg,
duration: duration,
icon: icon
})
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 微信小程序 功能函數(shù)小結(jié)(手機(jī)號驗(yàn)證*、密碼驗(yàn)證*、獲取驗(yàn)證碼*)
- 微信小程序6位或多位驗(yàn)證碼密碼輸入框功能的實(shí)現(xiàn)代碼
- 微信小程序發(fā)送短信驗(yàn)證碼完整實(shí)例
- 微信小程序?qū)崿F(xiàn)倒計(jì)時60s獲取驗(yàn)證碼
- 微信小程序如何獲取手機(jī)驗(yàn)證碼
- 微信小程序綁定手機(jī)號獲取驗(yàn)證碼功能
- 微信小程序?qū)崿F(xiàn)隨機(jī)驗(yàn)證碼功能
- 微信小程序?qū)崿F(xiàn)驗(yàn)證碼獲取倒計(jì)時效果
- 詳解如何使用微信小程序云函數(shù)發(fā)送短信驗(yàn)證碼
- 微信小程序?qū)崿F(xiàn)圖形驗(yàn)證碼
相關(guān)文章
javascript+mapbar實(shí)現(xiàn)地圖定位
地圖定位 圖吧地圖定位 附j(luò)avascript源碼每行都有注釋2010-04-04
javascript Array數(shù)組對象的擴(kuò)展函數(shù)代碼
我們經(jīng)常給 String,Function,Array 的原型加上自定義的擴(kuò)展函數(shù),比如去除字符串空格,數(shù)組排序等2010-05-05
JS組件Bootstrap實(shí)現(xiàn)圖片輪播效果
這篇文章主要為大家詳細(xì)介紹了JS組件Bootstrap實(shí)現(xiàn)圖片輪播效果的具體代碼,對圖片輪播效果感興趣的小伙伴們可以參考一下2016-05-05
javascript實(shí)現(xiàn)文本域?qū)懭胱址麜r限定字?jǐn)?shù)
這篇文章主要介紹了javascript實(shí)現(xiàn)文本域的寫入字符個數(shù)限定字?jǐn)?shù),需要的朋友可以參考下2014-02-02

