From 53615988c849e7a4ab9ed950a19d20d19bfcfed3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B7=B4=E6=8E=8C?= Date: Sun, 13 Sep 2020 21:30:02 +0800 Subject: [PATCH] 0 --- app/login/controller/Index.php | 147 +++++++++++++++++--------------- app/login/route/login.php | 20 ++--- app/login/validate/Yanzheng.php | 9 +- app/login/view/index/index.html | 90 ++++++++----------- app/middleware/Online.php | 2 +- app/middleware/UserLogin.php | 4 +- 6 files changed, 135 insertions(+), 137 deletions(-) diff --git a/app/login/controller/Index.php b/app/login/controller/Index.php index 064e906e..3e4ab2e7 100644 --- a/app/login/controller/Index.php +++ b/app/login/controller/Index.php @@ -5,14 +5,8 @@ namespace app\login\controller; use app\admin\model\Admin as AD; // // 引用加密类 use WhiteHat101\Crypt\APR1_MD5; -// // 引用验证码类 -use think\captcha\Captcha; // 引用view类 use think\facade\View; -// 引用验证规则 -use app\login\validate\Yanzheng; -// 引用验证类 -use think\exception\ValidateException; class Index @@ -20,7 +14,6 @@ class Index // 显示登录选择界面 public function index() { - $mobile = request()->isMobile(); // 清除cookie cookie('userid', null); cookie('username', null); @@ -31,7 +24,6 @@ class Index // 获取系统名称和版本号 $list['webtitle'] = config('shangma.webtitle'); $list['version'] = config('shangma.version'); - $list['mobile'] = $mobile; View::assign('list',$list); @@ -40,42 +32,77 @@ class Index } - // 登录验证 - public function yanzheng() + + // 登录验证分流 + public function yanchengdenglu() { // 获取表单数据 - $data = request()->only(['username','password','captcha','online']); + $list = request() + ->only([ + 'username' + ,'password' + ,'category' + ]); - // 验证表单数据 - try { - // 实例化验证模型 - $validate = new \app\login\validate\Yanzheng; - validate(Yanzheng::class)->scene('admin')->check($data); - } catch (ValidateException $e) { - // 验证失败 输出错误信息 - $data=['msg'=>$e->getError(),'status'=>0]; + $validate = new \app\login\validate\Yanzheng; + $result = $validate->scene('fenliu')->check($list); + $msg = $validate->getError(); + if(!$result){ + return json(['msg' => $msg, 'val' => 0]); + } + + switch ($list['category']) { + case 'admin': + $data = $this->admin($list); + break; + + case 'teacher': + $data = $this->teacher($list); + break; + + case 'student': + $data = $this->student($list); + break; + + default: + $data = ['msg' => '用户身份丢失', 'val' => 1]; + break; + } + + return json($data); + } + + + + // 登录验证 + public function admin($list) + { + // 获取服务器密码 + $userinfo = AD::where('username', $list['username']) + ->where('val', 1) + ->field('id, lastip, username, ip, denglucishu, lasttime, thistime, password') + ->find(); + + if($userinfo == null) + { + // 清除session(当前作用域) + session(null); + // 验证结果; + $data = ['msg' => '管理员帐号不存在或被禁用', 'val' => 0]; return json($data); } + // 验证用户名和密码 - $check = $this->check($data['username'],$data['password']); + $check = $this->check($list['password'], $userinfo->password); - if($check['status']==1) + if($check['val'] === 1) { - - // 设置cookie值 - if( request()->post('online') == true ) - { - // 设置 - cookie('userid', session('userid') ,259200); - cookie('username', $data['username'] ,259200); - cookie('password', $data['password'] ,259200); - } + session(null); + session('onlineCategory', 'admin'); + session('user_id', $userinfo->id); // 将本次信息上传到服务器上 - $userinfo = AD::where('username',$data['username']) - ->field('lastip,username,ip,denglucishu,lasttime,thistime') - ->find(); $userinfo->lastip = $userinfo->ip; $userinfo->ip = request()->ip(); $userinfo->denglucishu = ['inc', 1]; @@ -84,10 +111,10 @@ class Index $userinfo->save(); // 跳转到首页 - $data=['msg'=>'验证成功','status'=>1]; + $data = ['msg' => '验证成功', 'val' => 1]; }else{ // 提示错误信息 - $data=['msg'=>$check['msg'],'status'=>0]; + $data = ['msg' => $check['msg'], 'val' => 0]; } return json($data); @@ -95,47 +122,31 @@ class Index // 已知密码进行验证 - public function check($username,$password) + public function check($srcfrom) { + $src = [ + 'username' => '' + ,'password' => '' + ,'onlineCategory' => '' + ,'user_id' => '' + ]; + $src = array_cover($srcfrom, $src); + // 实例化加密类 $md5 = new APR1_MD5(); - // 获取服务器密码 - $userinfo = AD::where('username',$username)->where('status',1)->find(); - - if($userinfo == null) - { - - // 清除session(当前作用域) - session(null); - // 验证结果; - $data=['msg'=>'用户名不存在或被禁用','status'=>0]; - return $data; - } - - //验证密码 - $check = $md5->check($password,$userinfo->password); - + $check = $md5->check($inputPassword, $serverPassword); + if($check) { - session('admin', null); - session('onlineCategory', 'admin'); - session('admin.userid', $userinfo->id); - session('admin.username', $username); - session('admin.password', $password); - - $data=['msg'=>'验证成功','status'=>1]; + session('username', $username); + session('password', $password); + $data = ['msg' => '验证成功', 'val' => 1]; }else{ - // 清除cookie - cookie('userid', null); - cookie('username', null); - cookie('password', null); - // 清除session(当前作用域) - session(null); - $data=['msg'=>'用户名或密码错误','status'=>0]; + $data = ['msg' => '用户名或密码错误', 'val' => 0]; } - return $data; + return $data; } @@ -175,5 +186,5 @@ class Index View::assign('list',$list); return View::fetch(); } - + } diff --git a/app/login/route/login.php b/app/login/route/login.php index 76aaf102..65d973eb 100644 --- a/app/login/route/login.php +++ b/app/login/route/login.php @@ -20,15 +20,15 @@ Route::group('', function () { Route::rule('weihu','Index/weihu','get'); //错误页 }); -// 学生查询成绩登录 -Route::group('student', function () { - Route::rule('','Student/index','get'); - Route::rule('','Student/yanzheng','post'); -}); +// // 学生查询成绩登录 +// Route::group('student', function () { +// Route::rule('','Student/index','get'); +// Route::rule('','Student/yanzheng','post'); +// }); -// 学生查询成绩登录 -Route::group('teacher', function () { - Route::rule('','Teacher/index','get'); - Route::rule('','Teacher/yanzheng','post'); -}); +// // 学生查询成绩登录 +// Route::group('teacher', function () { +// Route::rule('','Teacher/index','get'); +// Route::rule('','Teacher/yanzheng','post'); +// }); diff --git a/app/login/validate/Yanzheng.php b/app/login/validate/Yanzheng.php index 6ed2c02a..c88c6e49 100644 --- a/app/login/validate/Yanzheng.php +++ b/app/login/validate/Yanzheng.php @@ -11,11 +11,11 @@ class Yanzheng extends Validate * 格式:'字段名' => ['规则1','规则2'...] * * @var array - */ + */ protected $rule = [ 'username|用户名' => 'require', 'password|密码' => 'require', - // 'captcha|验证码'=>'require|captcha', + 'category|用户身份' => 'require', ]; protected $message = [ @@ -27,6 +27,11 @@ class Yanzheng extends Validate 'username' ,'password' ,'shengfenzhenghao' + ], + 'fenliu' => [ + 'username' + ,'password' + ,'category' ] ]; } diff --git a/app/login/view/index/index.html b/app/login/view/index/index.html index d1d56dcf..8804586d 100644 --- a/app/login/view/index/index.html +++ b/app/login/view/index/index.html @@ -35,14 +35,14 @@
{$list['webtitle']} {$list['version']}
- +
- +
-
- - - +
+ + +

@@ -63,61 +63,43 @@ var sliderVerify = layui.sliderVerify, form = layui.form; - mobile = "{$list['mobile']}"; - if(mobile!==true) - { - var slider = sliderVerify.render({ - elem: '#slider', - onOk: function(){ - //当验证通过回调 - // layer.msg("滑块验证通过"); - }, - }) - } + var slider = sliderVerify.render({ + elem: '#slider', + onOk: function(){ + //当验证通过回调 + // layer.msg("滑块验证通过"); + }, + }) //监听提交 form.on('submit(login)', function(data){ - mobile = "{$list['mobile']}"; - if(mobile == true) + yz = slider.isOk(); + if(yz == false) { - yz = true - }else{ - yz = slider.isOk(); - } - - if(yz){ - $.post("/login",data.field,function(data,status){ - if(status != 'success') - { - layer.msg('数据传输错误'); - $('#captchaimg').onclick(); - $('#captcha').val(''); - $('#password').val(''); - slider.reset(); - } - if(data.status == 1) - { - window.location.href="/"; - }else{ - layer.msg(data.msg); - $('#captchaimg').click(); - $('#captcha').val(''); - $('#password').val(''); - slider.reset(); - } - }); - return false; - }else{ layer.msg("请先通过滑块验证"); + return false; } - return false; - }); - - // 单选事件 - $("[name='category']").each(function(){ - $(this).bind('click', function(){ - $('username').val('a'); + $.ajax({ + url:"/login", + type:'POST', + async: true, + data:data.field, + success:function(result){ + if(result.val == 1) + { + window.location.href="/"; + }else{ + layer.msg(result.msg); + $('#password').val(''); + slider.reset(); + } + }, + error:function(result){ + $('#password').val(''); + layer.msg('数据扔半道,回不来啦。',function(){}); + }, }); + return false; }); }) diff --git a/app/middleware/Online.php b/app/middleware/Online.php index 54dd7256..584d76fc 100644 --- a/app/middleware/Online.php +++ b/app/middleware/Online.php @@ -25,7 +25,7 @@ class Online $online = session('?onlineCategory'); if($online == false) { - \app\facade\OnLine::jump('/login', '登录超时啦~'); + \app\facade\OnLine::jump('/login/index/index', '登录超时啦~'); } return $next($request); } diff --git a/app/middleware/UserLogin.php b/app/middleware/UserLogin.php index 27132d99..e471a3e7 100644 --- a/app/middleware/UserLogin.php +++ b/app/middleware/UserLogin.php @@ -19,7 +19,7 @@ class UserLogin if ($category != 'admin') { - \app\facade\OnLine::jump('/login', '请使用管理员帐号登录'); + \app\facade\OnLine::jump('/login/index/index', '请使用管理员帐号登录'); } $userInfo = \app\facade\OnLine::getLoginInfo('admin'); @@ -29,7 +29,7 @@ class UserLogin $yz = $yz->check($userInfo['username'],$userInfo['password']); if($yz['status'] == 0){ - \app\facade\OnLine::jump('/login', '请使用新密码登录'); + \app\facade\OnLine::jump('/login/index/index', '请使用新密码登录'); } return $next($request);