本文實例為大家分享了微信小程序發送驗證碼按鈕效果展示的具體代碼,供大家參考,具體內容如下
首先上圖,最終效果如下:
實現關鍵點
代碼
.wxml
<view class="form_group"> <input type="text" placeholder="手機號碼" 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="短信驗證碼" 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: "獲取驗證碼", 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 }) }, // 獲取驗證碼 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=>{ // 調用后端API,獲取手機驗證碼 _util.showToast('success',"已發送"); _this.setData({ disabled: true }) },err=>{ _util.showToast('none',"發送失敗") }); // 設置發送驗證碼按鈕樣式 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: '重新發送', suffix: '', currentTime: 61, disabled: false }) } }, 1000) } else { _util.showToast('none','請輸入正確的手機號碼。'); } },
sign-up.service.js
//獲取手機驗證碼 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: 服務端業務處理正常結束 resolve(res.data) } else { //其它錯誤,提示用戶錯誤信息 /*** * 需要根據接口文檔改!!! */ reject(res) } }), fail: (err => { if(err.responseJSON){ reject(err.responseJSON.message) }else{ reject(err); } }) }); }); }}export default HTTP;
util.js
// 驗證手機號碼是否有效 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 }) }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答