Commit 6caf8244 by 梁建民

验证码

parent 21d0e8e2
{ {
"pages": [ "pages": [
"pages/person/verifycode/index",
"pages/person/auth/index", "pages/person/auth/index",
"pages/person/verifycode/index",
"pages/person/login/index", "pages/person/login/index",
"pages/person/getphone/index", "pages/person/getphone/index",
"pages/person/agreement/index", "pages/person/agreement/index",
......
const http = require('../../../utils/util.js'); const http = require('../../../utils/util.js');
import { import {
apis apis
} from '../../../utils/api.js'; } from '../../../utils/api.js';
Page({ Page({
/** /**
* 页面的初始数据 * 页面的初始数据
*/ */
data: { data: {
error: false, error: false,
errorText: '', errorText: '',
mobileFlag: false, mobileFlag: false,
codeFlag: false, codeFlag: false,
disabled: false, disabled: false,
disabledBtn: false, disabledBtn: false,
loading: false, loading: false,
currentTime: 61, currentTime: 61,
vcode:true, vcode: false,
vcodeFlag:true, vcodeFlag: false,
timeText: '发送验证码', timeText: '发送验证码',
formData: { captchaUrl: '',
mobile: '', captchaUuid: '',
code: '' captcha: '',
} formData: {
}, mobile: '',
code: ''
/** }
* 生命周期函数--监听页面加载 },
*/
onLoad: function(options) { /**
* 生命周期函数--监听页面加载
}, */
onLoad: function(options) {
/**
* 生命周期函数--监听页面初次渲染完成 },
*/
onReady: function() { /**
* 生命周期函数--监听页面初次渲染完成
}, */
onReady: function() {
/**
* 生命周期函数--监听页面显示 },
*/
onShow: function() { /**
* 生命周期函数--监听页面显示
}, */
onShow: function() {
/**
* 生命周期函数--监听页面隐藏 },
*/
onHide: function() { /**
* 生命周期函数--监听页面隐藏
}, */
onHide: function() {
/**
* 生命周期函数--监听页面卸载 },
*/
onUnload: function() { /**
* 生命周期函数--监听页面卸载
}, */
onUnload: function() {
/**
* 页面相关事件处理函数--监听用户下拉动作 },
*/
onPullDownRefresh: function() { /**
* 页面相关事件处理函数--监听用户下拉动作
}, */
onPullDownRefresh: function() {
/**
* 页面上拉触底事件的处理函数 },
*/
onReachBottom: function() { /**
* 页面上拉触底事件的处理函数
}, */
onReachBottom: function() {
/**
* 用户点击右上角分享 },
*/
onShareAppMessage: function() { /**
* 用户点击右上角分享
}, */
/** onShareAppMessage: function() {
* 获取验证码
*/ },
getCode: function() { /**
* 获取验证码
let self = this; */
getCode: function() {
var currentTime = this.data.currentTime;
let self = this;
var interval = setInterval(function() { let currentTime = this.data.currentTime;
currentTime--; let interval = setInterval(function() {
self.setData({ currentTime--;
timeText: currentTime + 's'
}); self.setData({
timeText: currentTime + 's'
});
if (currentTime <= 0) {
clearInterval(interval); if (currentTime <= 0) {
self.setData({ clearInterval(interval);
timeText: '重新发送', self.setData({
currentTime: 61, timeText: '重新发送',
disabled: false currentTime: 61,
}); disabled: false
});
}
}, 1000) }
}, }, 1000)
/** },
* 发送验证码 /**
*/ * 发送验证码
getVerificationCode: function(e) { */
getVerificationCode: function(e) {
let self = this;
//验证必填手机号
if (self.data.formData.mobile) { if (this.data.formData.mobile) {
http.getData(apis.getWxCode, { //验证是否发送了验证码
mobile: self.data.formData.code, if (this.data.captchaUuid) {
code_type: 2
}, function(res) { //是否填写了验证码
if (res.err_code === 0) { if (this.data.captcha) {
http.getData(apis.getRegistCode, {
self.getCode(); captchaUuid: this.data.captchaUuid,
self.setData({ captcha: this.data.captcha,
disabled: true mobile: this.data.formData.mobile,
}); code_type: 2
}, (res) => {
}else{ if (res.err_code === 0) {
this.getCode();
wx.showToast({ this.setData({
title: '发送失败', disabled: true
icon: 'none', });
duration: 2000 } else {
}); wx.showToast({
title: res.err_msg,
} icon: 'none',
}, true); duration: 2000
} else { });
}
this.setData({ }, true);
error: true, } else {
errorText: '手机号不能为空', this.setData({
mobileFlag: true error: true,
}); errorText: '图形验证码不能为空',
vcodeFlag: true
} });
}
},
/** } else {
* 刷新验证码 this.refreshVerification();
*/ }
verificationFn:function(){ } else {
this.setData({
}, error: true,
/** errorText: '手机号不能为空',
* 校验字段 mobileFlag: true
*/ });
bindinputFn: function(e) { }
let value = e.detail.value
let reg_mobile = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/; },
/**
if (e.currentTarget.dataset.type == 1) { * 刷新图形验证码
*/
this.setData({ refreshVerification: function() {
'formData.mobile': value
}); this.setData({
vcode: true
if (value) { });
this.setData({ http.getData(apis.captchaInfo, null, (res) => {
error: true, this.setData({
errorText: '请填写正确的手机号', captchaUrl: res.captchaUrl,
mobileFlag: true captchaUuid: res.captchaUuid
}); });
});
if (reg_mobile.test(value)) { },
/**
this.setData({ * 校验字段
error: false, */
errorText: '', bindinputFn: function(e) {
mobileFlag: false let value = e.detail.value
}); let reg_mobile = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/;
} if (e.currentTarget.dataset.type == 1) {
}
this.setData({
} else { 'formData.mobile': value
});
if (value) {
if (value) {
this.setData({
error: false, this.setData({
errorText: '', error: true,
codeFlag: false errorText: '请填写正确的手机号',
}); mobileFlag: true
});
}
if (reg_mobile.test(value)) {
} this.setData({
error: false,
}, errorText: '',
/** mobileFlag: false
* 校验字段 });
*/ }
calcForm: function(val, type) { }
let mobile = val.mobile;
let code = val.code; } else if (e.currentTarget.dataset.type == 2) {
let reg_mobile = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/;
if (value) {
if (!mobile) { this.setData({
error: false,
this.setData({ errorText: '',
error: true, codeFlag: false
errorText: '手机号不能为空', });
mobileFlag: true }
});
return false; } else if (e.currentTarget.dataset.type == 3) {
} if (value) {
this.setData({
if (!reg_mobile.test(mobile)) { captcha: value
});
this.setData({ this.setData({
error: true, error: false,
errorText: '请填写正确的手机号', errorText: '',
mobileFlag: true vcodeFlag: false
}); });
}
return false; }
} },
/**
if (!code) { * 校验字段
*/
this.setData({ calcForm: function(val, type) {
error: true, let mobile = val.mobile;
errorText: '验证码不能为空', let code = val.code;
codeFlag: true let reg_mobile = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/;
});
if (!mobile) {
return false;
this.setData({
} error: true,
errorText: '手机号不能为空',
mobileFlag: true
this.setData({ });
error: false, return false;
errorText: '', }
codeFlag: false,
mobileFlag: false if (!reg_mobile.test(mobile)) {
});
this.setData({
return true; error: true,
}, errorText: '请填写正确的手机号',
/** mobileFlag: true
* 登录 });
*/ return false;
formSubmit: function(e) { }
var self = this; if (!code) {
if (this.calcForm(e.detail.value)) { this.setData({
error: true,
self.setData({ errorText: '验证码不能为空',
disabledBtn: true, codeFlag: true
loading: true });
}); return false;
}
http.postData(apis.authMobilelogin, e.detail.value, function(res) {
if (res.err_code === 0) { this.setData({
error: false,
//注入token errorText: '',
wx.setStorage({ codeFlag: false,
key: "access_token", mobileFlag: false
data: res.data.access_token });
});
return true;
// wx.switchTab({ },
// url: '/pages/tab/home/home' /**
// }) * 登录
*/
} else { formSubmit: function(e) {
wx.showToast({ var self = this;
title: res.err_msg,
icon: 'none', if (this.calcForm(e.detail.value)) {
duration: 2000
}); self.setData({
} disabledBtn: true,
loading: true
self.setData({ });
disabledBtn: false,
loading: false http.postData(apis.authMobilelogin, e.detail.value, (res) => {
}); if (res.err_code === 0) {
}); //注入token
} wx.setStorage({
key: "access_token",
} data: res.data.access_token
}) });
\ No newline at end of file
wx.switchTab({
url: '/pages/tab/home/home'
})
} else {
wx.showToast({
title: res.err_msg,
icon: 'none',
duration: 2000
});
}
self.setData({
disabledBtn: false,
loading: false
});
});
}
}
})
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
<text class="t1">Hi,上午好</text> <text class="t1">Hi,上午好</text>
<text class="t1">欢迎使用IC业务助手!</text> <text class="t1">欢迎使用IC业务助手!</text>
</view> </view>
<form bindsubmit="formSubmit"> <form bindsubmit="formSubmit">
<view class="inp-wrap row verCenter {{mobileFlag ? 'inp-error':''}}"> <view class="inp-wrap row verCenter {{mobileFlag ? 'inp-error':''}}">
<text class="icon iconfont iconiconxiantiaoshouji21"></text> <text class="icon iconfont iconiconxiantiaoshouji21"></text>
...@@ -21,9 +20,9 @@ ...@@ -21,9 +20,9 @@
<view class="inp-wrap row verCenter {{vcodeFlag ? 'inp-error':''}}" wx:if="{{vcode}}"> <view class="inp-wrap row verCenter {{vcodeFlag ? 'inp-error':''}}" wx:if="{{vcode}}">
<text class="icon iconfont iconiconxiantiaoshouji"></text> <text class="icon iconfont iconiconxiantiaoshouji"></text>
<view class="form-item-right row verCenter bothSide"> <view class="form-item-right row verCenter bothSide">
<input placeholder="请输入图形验证码" placeholder-class="placeholderClass" name='code' data-type="3" bindinput='bindinputFn'></input> <input placeholder="请输入图形验证码" placeholder-class="placeholderClass" name='captcha' data-type="3" bindinput='bindinputFn'></input>
<view class='code-img'> <view class='code-img'>
<cover-image src="http://authapi.icsales.cc/captcha/default/d162987e-6aa8-545e-3b73-8c2cf3c361cc" class="img" bindtap='verificationFn'></cover-image> <cover-image src="{{captchaUrl}}" class="img" bindtap='refreshVerification'></cover-image>
</view> </view>
</view> </view>
</view> </view>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment