🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
1.路由 routes/web.php ```php Route::namespace('Admin')->group(function () { Route::match(['POST'], '/reset', 'Index@reset'); }); ``` 1.重置密码页面 admin/index/forget.balde.php ![](https://img.kancloud.cn/5d/1c/5d1cd82654855f0cb83db4fbcf7d3c49_306x256.png) ```html ~~~ <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>博客后台管理系统</title> <link rel="shortcut icon" href="assets/admin/img/logo.jpg" type="image/x-icon"> <link href="assets/admin/css/bootstrap.min.css" rel="stylesheet" /> <link href="assets/admin/css/font-awesome.min.css" rel="stylesheet" /> <link href="assets/admin/css/weather-icons.min.css" rel="stylesheet" /> <link id="beyond-link" href="assets/admin/css/beyond.min.css" rel="stylesheet" type="text/css" /> </head> <body> <div class="login-container"> <form action=""> <div class="loginbox bg-white"> <div class="loginbox-title">找回密码</div> <div class="loginbox-or"> <div class="or-line"></div> </div> <div class="loginbox-textbox"> <input type="text" class="form-control" name="email" placeholder="请输入邮箱" /> </div> <div class="loginbox-textbox hidden"> <input type="text" class="form-control" name="code" placeholder="请输入验证码" /> </div> <div class="loginbox-submit"> <input type="submit" class="btn btn-primary btn-block" id="verify" value="获取验证码"> </div> <div class="loginbox-submit hidden"> <input type="submit" class="btn btn-primary btn-block" id="reset" value="重置密码"> </div> </div> <div class="logobox"> <p class="text-center" style="font-size: 18px;font-weight: bold;text-shadow: 3px 3px 3px #FF0000;font-style: italic;"></p> </div> </form> </div> <script src="assets/admin/js/skins.min.js"></script> <!--Basic Scripts--> <script src="assets/admin/js/jquery.min.js"></script> <script src="assets/admin/js/bootstrap.min.js"></script> <script src="assets/admin/js/slimscroll/jquery.slimscroll.min.js"></script> <script src="assets/lib/layer/layer.js"></script> <!--Beyond Scripts--> <script src="assets/admin/js/beyond.js"></script> <script> $('#verify').click(function () { var data = $('form').serialize();//表单序列化 $.ajax({ url: "{{url('forget')}}", data: data, type: 'post', dataType:'json', success:function(data){ if (data.code == 1) { layer.alert(data.msg,{ offset:'220px', icon:6, time:2000 },function () { $('input[name=email]').parent().addClass('hidden'); $('input[name=code]').parent().removeClass('hidden'); $('#verify').parent().addClass('hidden'); $('#reset').parent().removeClass('hidden'); }); } else { layer.open({ skin:'layui-layer-molv', offset:'220px', title:'验证码发送失败', content:data.msg, icon: 5, anim:6 }); } } }); return false; }) //发送验证码 $('#reset').click(function () { //重置密码 var data = $('form').serialize();//表单序列化 $.ajax({ url: "{{url('reset')}}", data: data, type: 'post', dataType:'json', success:function(data){ if (data.code == 1) { layer.alert(data.msg,{ offset:'220px', icon:6, time:2000 },function () { window.location.href=data.url; }); } else { layer.open({ skin:'layui-layer-molv', offset:'220px', title:'重置密码失败', content:data.msg, icon: 5, anim:6 }); } } }); return false; }) //发送验证码 </script> </body> </html> ``` 2.控制器 Controller/admin/Index.php ```php ublic function reset() { $data=request()->only(['email','code']); $res=(new Admin)->reset($data); if ($res == 1){ $msg=[ 'code'=>1, 'msg'=>'重置密码成功,请去邮箱查看!', 'url'=>url('login') ]; }else{ $msg=[ 'code'=>1, 'msg'=>$res, ]; } return $msg; } ``` 3.模型 Models/Admin.php ```php public function reset($data) { $rules=[ 'code'=>'bail|required', ]; $msg=[ 'code.required'=>'请输入验证码' ]; $validat=Validator::make($data,$rules,$msg); if ($validat->fails()){ return $validat->errors()->first(); } if ($data['code'] != session('code')){ return '验证码不正确'; } $password=mt_rand(100000,999999); $adminInfo=$this->where('email',$data['email'])->first(); $adminInfo->password=$password; $res=$adminInfo->save(); if ($res){ $content='您的用户名是:'.$adminInfo['username'].'<br密码是:>'.$password; $sendPass=mailto($data['email'],'恭喜您,密码重置成功',$content); if ($sendPass) { return 1; }else{ return '密码重置失败!'; } }else{ return '密码重置失败'; } } ```