微信小程序獲取驗證碼的代碼參考

// 引入手機格式驗證文件 (根據業務需求進行引入)
var phoneFormat = app.Simplify.verificationPhone;
// 點擊事件
_getPhoneCode: function (e) {
	let userPhone = e.target.id;
	let that = this;
	if (phoneFormat.test(userPhone)) {
		that.setData({
			getLoading: true //用來顯示獲取中
		});
		iFunctions._getPhoneCodeRequest(userPhone, that);
	} else {
		app.ejectInformation('提示', '手機格式有誤。', false);
	}
}
};

iFunctions = {
	//獲取驗證碼
	_getPhoneCodeRequest: function (userPhone, that) {
		var codeTime;
		var timeIndex = 60;
		wx.showLoading({
			title: '獲取中',
		});
		wx.request({
			url: app.Simplify.requestUrl + '/api/tmp_2801/dig3396/getcodes',
			data: {
				phone: userPhone
			},
			method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
			success: function (res) {
				app.showInformation('獲取成功', 1500);
				that.setData({
					getPhoneCode: '重新獲取(' + timeIndex + ')'
				});
				codeTime = setInterval(function () {
					timeIndex--;
					if (timeIndex == 0 || timeIndex < 0) {
						clearInterval(codeTime);
						that.setData({
							getLoading: false,
							getPhoneCode: '重新獲取'
						});
					} else {
						that.setData({
							getPhoneCode: '重新獲取(' + timeIndex + ')'
						});
					}
				}, 1000);
			},
			fail: function () {
			}
		})
	},
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章