如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
初始化操作controller-----Admin.php
<?php namespace app\admin\Controller; use think\Controller; use think\Request; class Admin extends Controller { public function __construct ( Request $request = null ) { parent::__construct( $request ); //session渲染到页面 $this->assign('admin',session('admin')); //执行登录验证 if(!session('admin')) { $this->error('请登录','Logindo/index'); } } // public function _empty(){ // echo "没有此方法"; // } }
<?php namespace app\admin\controller; use think\Controller; use app\admin\model\Login; //model里的Login.php class Conf extends Admin //controller中的Admin.php { public function pwd() { $this->assign('title','FLY后台管理-修改密码'); return view('Conf/pwd'); } public function pwddo(){ if(request()->isPost()){ $res = (new Login())->pwd(input('post.')); //new Login.php,使用里面的pwd if($res['valid']) { session('admin',null); //说明修改成功 $this->success($res['msg']); }else{ //说明修改失败 $this->error($res['msg']);exit; } } } }
<?php namespace app\admin\model; use think\Loader; use think\Model; use think\Validate; class Login extends Model { protected $pk = 'id';//主键 //设置当前模型对应的完整数据表名称 protected $table = 'fly_user'; /** * 登录 * $data 是接收的post数据 */ public function login($data) { //1.执行验证 $validate = new Validate([ 'name'=>'require', 'pwd'=>'require' ],[ 'name.require'=>'请输入管理账号', 'pwd.require'=>'请输入密码' ] ); // //如果验证不通过 if (!$validate->check($data)) { return ['valid' => 0, 'msg' => $validate->getError()]; } //2.比对用户名和密码是否正确 $userInfo = $this->where('name', $data['name'])->where('pwd', md5($data['pwd']))->where('sort', 1)->find(); // dump($userInfo);die; if (!$userInfo) { //说明在数据库未匹配到相关数据 return ['valid' => 0, 'msg' => '用户名或者密码不正确 或没有权限']; } //3.将用户信息存入到session中 $admin = array('id' => $userInfo['id'],'name'=>$userInfo['name'] ); session('admin', $admin); return ['valid' => 1, 'msg' => '登录成功']; } /** * 修改密码 * $data 是接收的post数据 */ public function pwd($data){ //执行验证 $validate = new Validate([ 'pwd' => 'require', 'new_pwd' => 'require', 'confirm_pwd' => 'require|confirm:new_pwd' ],[ 'pwd.require' => '请输入原始密码', 'new_pwd.require' => '请输入新密码', 'confirm_pwd.require' => '请重复新密码', 'confirm_pwd.confirm' => '确认密码与新密码不一致' ] ); if (!$validate->check($data)) { return ['valid'=> 0,'msg'=>$validate->getError()]; } //原始密码是否正确 $userInfo = $this->where('pwd',md5($data['pwd']))->where('id',session('admin.id'))->find(); if(!$userInfo){ return ['valid'=>0, 'msg'=>'原始密码不正确']; } //修改密码 // save方法第二个参数为更新条件 $res = $this->save([ 'pwd' =>md5($data['new_pwd']), ],[$this->pk => session('admin.id')]); if($res){ return ['valid'=>1, 'msg'=>'密码修改成功']; }else{ return ['valid'=>0, 'msg'=>'密码修改失败']; } } }