From 2cb2202ed2ea01f222fb39c944cfcb69def36fac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B7=B4=E6=8E=8C?= Date: Wed, 17 Oct 2018 13:46:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BD=BF=E7=94=A8=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E5=8F=8A=E6=B7=BB=E5=8A=A0=E7=94=A8=E6=88=B7=E5=9C=A8?= =?UTF-8?q?=E7=BA=BF=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/common/controller/Base.php | 4 +- application/common/controller/BaseError.php | 4 +- application/home/controller/Index.php | 38 --------------- application/http/middleware/YanZheng.php | 33 ++++++++++--- application/login/controller/Index.php | 46 +++++++++++++------ application/member/model/Member.php | 9 ++-- config/app.php | 4 +- .../notfound.html => public/error/error.tpl | 6 +-- .../error/success.tpl | 0 9 files changed, 68 insertions(+), 76 deletions(-) rename application/home/view/index/notfound.html => public/error/error.tpl (96%) rename application/home/view/index/mysuccess.html => public/error/success.tpl (100%) diff --git a/application/common/controller/Base.php b/application/common/controller/Base.php index fe900611..6505483a 100644 --- a/application/common/controller/Base.php +++ b/application/common/controller/Base.php @@ -15,8 +15,6 @@ class Base extends Controller protected $middleware = [ 'YanZheng'=>[ 'except'=>[ - 'notfound', - 'mysuccess', 'ajaxData', ], ], @@ -24,7 +22,7 @@ class Base extends Controller public function _empty() { - $this->redirect(url('/notfound')); + $this->error('不好意思,您访问的页面不存在~'); } } diff --git a/application/common/controller/BaseError.php b/application/common/controller/BaseError.php index 42ae426b..6d9c8031 100644 --- a/application/common/controller/BaseError.php +++ b/application/common/controller/BaseError.php @@ -15,12 +15,12 @@ class BaseError extends Controller public function index() { // 跳转页面 - $this->redirect(url('/notfound')); + $this->error('不好意思,您访问的页面不存在~'); } public function _empty() { - $this->redirect(url('/notfound')); + $this->error('不好意思,您访问的页面不存在~'); } } diff --git a/application/home/controller/Index.php b/application/home/controller/Index.php index 777181e1..f49bb2f4 100644 --- a/application/home/controller/Index.php +++ b/application/home/controller/Index.php @@ -8,42 +8,4 @@ use app\common\controller\Base; class Index extends Base { - /** - * 操作成功跳转 - * - * @return \think\Response - */ - public function mysuccess($msg = '操作成功~') - { - // 模版赋值 - $this->assign('msg',$msg); - - // 跳转页面 - return $this->fetch(); - } - - - /** - * 操作失败跳转 - * - * @return \think\Response - */ - public function notfound ($msg = '不好意思,您访问的页面不存在~' ) - { - if (session('?msg')) - { - $msg = session('msg'); - session('msg', null); - } - - - // 模版赋值 - $this->assign('msg',$msg); - - // 跳转页面 - return $this->fetch(); - } - - - } diff --git a/application/http/middleware/YanZheng.php b/application/http/middleware/YanZheng.php index ea3f590a..1edb261d 100644 --- a/application/http/middleware/YanZheng.php +++ b/application/http/middleware/YanZheng.php @@ -6,22 +6,41 @@ namespace app\http\middleware; use think\Controller; // 引用权限验证类 use \php4world\Auth; +// 引用用户数据模型 +use app\member\model\Member; class YanZheng extends Controller { public function handle($request, \Closure $next) { - // 验证用户在否登录 - if(!session('?userid') && cookie('name') == null ) + // 声明帐号密码变量 + $username = 0; + $password = 0; + + // 实例化用户类 + $membermod = new Member(); + + // 如果用户没有登录直接跳转到登录页面 + if( session('?userid') ) { - // 跳转到首页 - $this->redirect('/login'); - return '验证失败'; + $username = session('username'); + $password = session('password'); + }elseif(cookie('username') != null) + { + // 获取用户名和密码 + $username = cookie('username'); + $password = cookie('password'); }else{ - // - dump('检验密码对不对'); + $this->error('登录超时啦,请登录登录'); } + + // 检验用户名或密码是否正确 + $yz = action('login/index/check',['username'=>$username,'password'=>$password]); + if($yz == null){ + $this->error('请使用新密码登录'); + } + // 验证用户名和密码是否正确 // 验证用户权限 diff --git a/application/login/controller/Index.php b/application/login/controller/Index.php index 0913aacd..f06311d6 100644 --- a/application/login/controller/Index.php +++ b/application/login/controller/Index.php @@ -38,7 +38,7 @@ class Index extends Controller $result = $validate->check($data); $msg = $validate->getError(); if(!$result){ - $this->redirect('/notfound',['msg' => $msg]); + $this->error($msg); } // 重新计算密码 @@ -46,18 +46,12 @@ class Index extends Controller $data['password'] = $md5->hash($miyao,$data['password']); // 验证用户名和密码 - $userid = $membermod->check($data); + $userid = $this->check($data['username'],$data['password']); if($userid) { - // 获取用户信息 - $userinfo = $membermod->get($userid); - - // Session存值 - session('username', $userinfo->username); - session('password', $userinfo->password); - session('userid', $userinfo->id); + // 设置cookie值 if( request()->post('online') == true ) { // 设置 @@ -66,6 +60,7 @@ class Index extends Controller } // 将本次信息上传到服务器上 + $userinfo = $membermod->get($userid); $userinfo->lastip = $userinfo->ip; $userinfo->ip = request()->host(); $userinfo->denglucishu = ['inc', 1]; @@ -75,19 +70,40 @@ class Index extends Controller // 跳转到首页 $this->redirect(url('/')); - return '验证完成'; - - //模版 }else{ - // 跳转到首页 - $this->redirect(url('/notfound',['msg' => '用户名或密码错误'])); - return '验证失败'; + // 提示错误信息 + $this->error('用户名或密码错误'); } return ''; } + // 已知密码进行验证 + public function check($username,$password) + { + //实例化类 + $membermod = new membermod(); + + //验证密码 + $yz = $membermod->check($username,$password); + + if($yz) + { + // Session存值 + session('username', $username); + session('password', $password); + session('userid', $yz); + }else{ + // 清除cookie + cookie(null, 'think_'); + // 清除session(当前作用域) + session(null); + } + return $yz; + } + + // 首次登录添加admin public function first() diff --git a/application/member/model/Member.php b/application/member/model/Member.php index 85a39008..b08a5373 100644 --- a/application/member/model/Member.php +++ b/application/member/model/Member.php @@ -7,14 +7,13 @@ use think\Model; class Member extends Model { // 密码验证 - // $data = ['username'=>'aa,'password'=>'bb'] - public function check( $data = [] ) + public function check( $username ,$password ) { // 验证用户名与密码是否匹配 $list = $this - ->where('username',$data['username']) - ->where('password',$data['password']) - ->value('id');dump($list); + ->where('username',$username) + ->where('password',$password) + ->value('id'); return $list; } diff --git a/config/app.php b/config/app.php index 3545bfbb..aecc2fee 100644 --- a/config/app.php +++ b/config/app.php @@ -130,8 +130,8 @@ return [ 'route_cache_option' => [], // 默认跳转页面对应的模板文件 - 'dispatch_success_tmpl' => Env::get('think_path') . 'tpl/dispatch_jump.tpl', - 'dispatch_error_tmpl' => Env::get('think_path') . 'tpl/dispatch_jump.tpl', + 'dispatch_success_tmpl' => Env::get('think_path') . '../public/error/success.tpl', + 'dispatch_error_tmpl' => Env::get('think_path') . '../public/error/error.tpl', // 异常页面的模板文件 'exception_tmpl' => Env::get('think_path') . 'tpl/think_exception.tpl', diff --git a/application/home/view/index/notfound.html b/public/error/error.tpl similarity index 96% rename from application/home/view/index/notfound.html rename to public/error/error.tpl index fad09c04..5d485886 100644 --- a/application/home/view/index/notfound.html +++ b/public/error/error.tpl @@ -24,11 +24,9 @@

系统提示

-

{$msg}

+

- -
-
+

{js href="/static/H-ui.admin_v3.1.3.1/H-ui.admin/lib/jquery/1.9.1/jquery.min.js"} diff --git a/application/home/view/index/mysuccess.html b/public/error/success.tpl similarity index 100% rename from application/home/view/index/mysuccess.html rename to public/error/success.tpl