1、定义前台获取验证码的html模板
<div class="form-group">
<input type="text" class="form-control" name="phone" id="phone" placeholder="手机号码" onfocus="this.placeholder=''" onblur="this.placeholder='手机号码'">
<p class="col-lg-12 col-md-12 col-sm-12 find">
<input type="text" class="form-control pull-left" id="code" name="code" placeholder="验证码" onfocus="this.placeholder=''" onblur="this.placeholder='验证码'">
<button type="button" id="send" class="btn btn-block btn-outline btn-default pull-right acquire">点击获取</button>
</p>
<button type="button" id="submit" class="btn btn-block btn-outline btn-primary">找回密码</button>
<p>
<span class="fgt">还没有账号?<a href="{:url('/web/Index/register')}">免费注册</a></span>
</p>
</div>
2、当点击获取验证码的按钮,则发送手机号到后台
var countdown = 60;
var tt;
$("#send").click(function(){
//验证座机号码的正则
var isPhone = /^([0-9]{3,4}-)?[0-9]{7,8}$/;
//验证手机号码的正则
var isMob=/^((\+?86)|(\(\+86\)))?(13[012356789][0-9]{8}|15[012356789][0-9]{8}|18[012356789][0-9]{8}|147[0-9]{8}|1349[0-9]{7})$/;
var phone_str=$("#phone").val();
phone_str = $.trim(phone_str);
if(phone_str==""){
toastr.error('手机号码必填');
return;
//如果当不是手机号码或者座机号码时,则返回错误
}else if(!(isPhone.test(phone_str)||isMob.test(phone_str))){
toastrr.error('手机号码格式错误');
return;
}
//向验证码函数发送手机号
var url = "{:url('/user/Login/sendPwdCode')}";
$.post(url,{mobile:phone_str},function(data){
if(data.code ==1){
toastr.success('发送成功,请注意查收');
$("#send").attr("disabled","true");
tt = setInterval(function() {
//10分钟修改一次按钮状态,并调用从新获取验证码的函数
settime();
},1000)
}else {
toastr.error(data.msg);
}
})
});
//修改按钮状态的函数
function settime() {
if (countdown == 0) {
$("#send").val("没收到?重新获取");
$("#send").removeAttr("disabled");
countdown = 60;
clearInterval(tt);
} else {
$("#send").val(countdown);
countdown--;
}
}
3、前台发送请求到后台获取验证码的函数
//发送验证码 ,找回密码
function sendPwdCode($mobile, $app = false)
{
$umsg = findById("user", array("name" => $mobile), "id");
if (empty($umsg["data"])) {
if ($app) {
return json(array('code' => '0', 'msg' => '没有找到该账号,请重新输入', 'data' => ''));
} else {
return array('code' => '0', 'msg' => '没有找到该账号,请重新输入', 'data' => '');
}
}
//判断当前手机号一个小时内已经发送了多少
$start_time = time() - 3600;
$map = array(
'addtime' => array('gt', $start_time),
'mobile' => $mobile,
'type' => 0
);
$msg = getDataList('SmsLog', $map, 'id');
if ($msg['code'] == '1') {
$count = count($msg['data']);
}
//一个小时内不能超过三次
if ($count >= 3) {
if ($app) {
return json(array('code' => '0', 'msg' => '发送次数过于频繁,请一个小时后再试', 'data' => ''));
} else {
return array('code' => '0', 'msg' => '发送次数过于频繁,请一个小时后再试', 'data' => '');
}
}
//四位随机数字验证码
$code = get_random(4);
$tplId = config('sms_tpl_id.findcode');
$smsdata = array('code'=>$code);
// $value = "#code#=" . $code;
// $result = sendSms($mobile, $value, '49047');
$result = alsms($tplId,$mobile,$smsdata);
if ($result['code'] == "1") {
//把该条数据保存到数据库
//发送成功,做记录
$data = array(
'mobile' => $mobile,
'msg' => $code,
'type' => '0',
'addtime' => time()
);
saveData('SmsLog', $data);
if ($app) {
return json(array('code' => '1', 'msg' => '发送成功,十分钟内有效', 'data' => '发送成功,十分钟内有效'));
} else {
return array('code' => '1', 'msg' => '发送成功,十分钟内有效', 'data' => '发送成功,十分钟内有效');
}
} else {
if ($app) {
return json(array('code' => '0', 'msg' => "发送失败", 'data' => '发送失败'));
} else {
return array('code' => '0', 'msg' => $result['msg'], 'data' => $result['msg']);
}
}
}
4、当前台点击找回密码按钮,则发送手机号和验证码到后台判断是否符合,如果前台收到后台验证成功的参数,则跳转到新的密码设置页面
$("#submit").click(function () {
var isPhone = /^([0-9]{3,4}-)?[0-9]{7,8}$/;
var isMob=/^((\+?86)|(\(\+86\)))?(13[012356789][0-9]{8}|15[012356789][0-9]{8}|18[012356789][0-9]{8}|147[0-9]{8}|1349[0-9]{7})$/;
var phone_str=$("#phone").val();
phone_str = $.trim(phone_str);
if(phone_str==""){
toastr.error('手机号码必填');
return;
}else if(!(isPhone.test(phone_str)||isMob.test(phone_str))){
toastr.error('电话号码格式错误');
return;
}
var code = $('#code').val();
code = $.trim(code);
if(code ==''){
toastr.error('验证码必填');
return ;
}
var url = "{:url('/user/Login/checkCode')}";
$.post(url, {'phone':phone_str,'code':code}, function (data) {
if(data.code ==1){
toastr.success('验证成功,请输入新密码');
var phone = data.phone;
setTimeout("go("+phone+")",1000);
// location.href="{:url('/web/Index/newPwd')}?phone="+phone;
}else {
toastr.error(data.msg);
}
});
});
});
//跳转到新的密码设置页面,并将手机号携带过去以便获取手机号进行设置
function go(phone){
location.href="{:url('/web/Index/newPwd')}?phone="+phone;
}
5、后台验证验证码是否正确的函数
//找回密码功能,验证验证码
function checkCode($app = false)
{
$phone = input('phone', '');
$code = input('code', '');
//
$res = verifySmsCode($phone, $code, 600);
if ($res['code'] == '0') {
if (empty($umsg["data"])) {
if ($app) {
return json(array('code' => '0', 'msg' => $res['msg'], 'data' => ''));
} else {
return array('code' => '0', 'msg' => $res['msg'], 'data' => '');
}
}
} else {
if ($app) {
return json(array('code' => 1, 'phone' => $phone));
} else {
return array('code' => 1, 'phone' => $phone);
}
}
}
6、定义新的密码设置页面的表单
<form action="{:url('/user/User/findPassword')}" method="post" id="reset">
<input type="password" style="margin-bottom:14px;" class="form-control" name="password" id="password" placeholder="新密码(6-20位字母、数字、符号组合)" onfocus="this.placeholder=''" onblur="this.placeholder='新密码(6-20位字母、数字、符号组合)'">
< input type="password" style="margin-bottom:14px;" class="form-control" name="cfmpassword" id="cfmpassword" placeholder="确认密码" onfocus="this.placeholder=''" onblur="this.placeholder='确认密码'">
<input type="hidden" id="phone" name="phone" value="{$phone|default=''}">
</form>
7、设置新密码的js提交函数
$("#btnreset").click(function () {
var password = $("#password").val();
var cfmpassword = $.trim($("#cfmpassword").val());
password = $.trim(password);
if(password==""){
toastr.error('新密码必填');
return;
}
if(password !==cfmpassword){
toastr.error('两次密码不一致');
return;
}
var regExp=/^(?![\d]+$)(?![a-zA-Z]+$)(?![^\da-zA-Z]+$).{6,20}$/;
if(!regExp.test(password)){
toastr.error('密码必须为6-20位字母、数字或符号组合');
return;
}
var pwd = $("#password").val();
var cfmpwd = $("#cfmpassword").val();
var phone = $("#phone").val();
var url = "{:url('/web/Index/findPassword')}" ;
$.post(url, {'phone':phone,'password':pwd,'cfmpassword':cfmpwd}, function(data){
if (data.code == 1) {
toastr.success('重置密码成功,请用新密码登录');
setTimeout('go()',1000);
} else {
toastr.error(data.msg);
}
});
});
//当返回新密码设置成功后,则跳转到登陆账号页面
function go(){
location.href="{:url('/web/Index/login')}";
}
8、当点击保存新密码后,进入新密码保存函数
//保存新密码
function findPassword($app = false)
{
try{
$phone = input('phone', '');
$pwd = input('password');
$pwd = trim($pwd);
$repwd = input('cfmpassword');
$repwd = trim($repwd);
if (empty($phone)) {
if ($app) {
return json(array('code' => 0, 'msg' => '电话号码不能为空'));
} else {
return array('code' => 0, 'msg' => '电话号码不能为空');
}
}
if ($pwd != $repwd) {
if ($app) {
return json(array('code' => 0, 'msg' => '新密码和确认密码不一致'));
} else {
return array('code' => 0, 'msg' => '新密码和确认密码不一致');
}
}
$phone =think_decrypt($phone); //解密一下
$rlt = saveDataByCon("user", array("password" => md5($pwd)), array("name" => $phone));
if ($app) {
return json($rlt);
} else {
return $rlt;
}
}catch (\Exception $e) {
//记录事务
c_Log($e);
return array("code" => 0, "msg" =>'找回密码失败','data'=>'');
}
}