TP5框架中实现多条件登录
控制器代码
//登录功能 public function login(){ $data = input(); $validate = $this->validate($data,[ 'uname|登录账号'=>'require',//可以是账号username、手机phone、邮箱email 'upwd|登录密码'=>'require' ]); if($validate!==true){ return json(['code'=>1,'msg'=>$validate,'data'=>null]); } $res = User::loginSel($data); if(empty($res)){ return json(['code'=>1,'msg'=>'账号密码有误','data'=>null]); } session('uid',$res['id']); session('unick',$res['nickname']); return json(['code'=>0,'msg'=>'登录成功','result'=>$res]); }
模型代码:
public static function loginSel($arr){ $username['username'] = $arr['uname']; $username['password'] = md5($arr['upwd']); $phone['phone'] = $arr['uname']; $phone['password'] = md5($arr['upwd']); $email['email'] = $arr['uname']; $email['password'] = md5($arr['upwd']); $usernameRes = self::where($username)->find(); $phoneRes = self::where($phone)->find(); $emailRes = self::where($email)->find(); if($usernameRes){ return $usernameRes; } if($phoneRes){ return $phoneRes; } if($emailRes){ return $emailRes; } return null; }
结果返回:
结果返回: { "code": 0, "msg": "登录成功", "result": { "id": 7, "username": "18235355939", "nickname": "182****5939", "phone": "18235355939", "password": "dc483e80a7a0bd9ef71d8cf973673924", "figure_url": null, "email": "123@qq.com", "last_login_time": 0, "open_type": null, "openid": "", "create_time": "1970-01-01 08:00:00", "update_time": "1970-01-01 08:00:00", "delete_time": null } }