怎么在微信小程序中實(shí)現(xiàn)發(fā)送驗(yàn)證碼按鈕效果?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
公司主營(yíng)業(yè)務(wù):網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作、移動(dòng)網(wǎng)站開(kāi)發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。成都創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開(kāi)放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來(lái)驚喜。成都創(chuàng)新互聯(lián)推出分宜免費(fèi)做網(wǎng)站回饋大家。
實(shí)現(xiàn)關(guān)鍵點(diǎn)
獲取驗(yàn)證碼按鈕無(wú)邊框: 可以用 button::after{ border: none; } 來(lái)去除邊框,或者直接用view綁定點(diǎn)擊事件。本例子中沒(méi)有使用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ī)號(hào)碼" 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','請(qǐng)輸入正確的手機(jī)號(hào)碼。'); } },
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 { //其它錯(cuò)誤,提示用戶錯(cuò)誤信息 /*** * 需要根據(jù)接口文檔改?。。? */ reject(res) } }), fail: (err => { if(err.responseJSON){ reject(err.responseJSON.message) }else{ reject(err); } }) }); }); } } export default HTTP;
util.js
// 驗(yàn)證手機(jī)號(hào)碼是否有效 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)容,你們掌握怎么在微信小程序中實(shí)現(xiàn)發(fā)送驗(yàn)證碼按鈕效果的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
本文標(biāo)題:怎么在微信小程序中實(shí)現(xiàn)發(fā)送驗(yàn)證碼按鈕效果
分享地址:http://bm7419.com/article46/jddeeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站維護(hù)、建站公司、品牌網(wǎng)站建設(shè)、微信公眾號(hào)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)