You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
3.5 KiB
PHP

<?php
namespace App\Services\Login;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use JWT;
class LoginService
{
public function login($arr){
date_default_timezone_set('PRC');
$nowTime=date('Y-m-d H:i:s',time());
$result = array();
if(isset($arr['username']) and isset($arr['password'])){
$query=DB::table('users')->select('id','pwd','group')->where([['username','=',$arr['username']],['status','=',1],['lock_to','<',$nowTime]])->get();
if(count($query)==1){
// $hash = password_hash($arr['password'], PASSWORD_DEFAULT);
// dd($query[0]->pwd);
$check=$this->CheckPwd(['userid'=>$query[0]->id,'password'=>$arr['password']]);
if($check['status']){
$jwt= new JWT();
$accessTimeout = $jwt -> GetGetSecretTimeOut();
$refreshTimeout = $jwt -> GetRefreshTokenTimeOut();
$access_token = $jwt->BuildJWT('yz','access',$query[0]->id,$query[0]->group,$accessTimeout);
$refresh_token = $jwt->BuildJWT('yz','refresh',$query[0]->id,'',$refreshTimeout);
if(!empty($arr['mian7'])){
$mian7_token = $jwt->BuildJWT('yz','mian7',$query[0]->id,'',$jwt -> GetMian7TokenTimeOut());
$result['mian7_token']=$mian7_token;
}
DB::table('users')->where(['id'=>$query[0]->id,'status'=>1])->update(['token'=>md5($refresh_token)]);
$result['token']=$access_token;
$result['refresh_token']=$refresh_token;
$result['status']='ok';
}else{
$result=$check;
}
}else{
$result['status']='no';
$result['msg']='查询出错';
}
}else{
$result['status']='no';
$result['msg']='缺少参数';
}
return $result;
}
public function CheckPwd($arr){
date_default_timezone_set('PRC');
$nowTime=date('Y-m-d H:i:s',time());
// 当前时间戳XXXX分
$LockToTime=date('Y-m-d H:i:s', strtotime('+'.env('LOCK_TIME').'minute'));
$query=DB::table('users')->select('pwd','times','lock_to')->where(['id'=>$arr['userid'],'status'=>1,['lock_to','<',$nowTime]])->get();
if(count($query)==1){
if (password_verify($arr['password'],$query[0]->pwd)) {
$u=DB::table('users')->where(['id'=>$arr['userid']])->update(['times'=>env('LOGOIN_CHECK_FAIL_TIMES')]);
return ['status'=>true,'msg'=>'ok'];
}else{
$shengyuTimes=$query[0]->times;
if($shengyuTimes==0){
$u=DB::table('users')->where(['id'=>$arr['userid']])->update(['times'=>env('LOGOIN_CHECK_FAIL_TIMES')]);
$shengyuTimes=env('LOGOIN_CHECK_FAIL_TIMES');
}
//减少剩余失败次数
$jian=DB::table('users')->where(['id'=>$arr['userid'],'status'=>1])->decrement('times',1);
if($shengyuTimes<=1 and $jian==1){
$u=DB::table('users')->where(['id'=>$arr['userid']])->update(['lock_to'=>$LockToTime]);
}
return ['status'=>false,'msg'=>'密码不正确,剩余次数'.($shengyuTimes-1)];
}
}else{
return ['status'=>false,'msg'=>'用户不存在或因密码错误次数过多暂时被锁定,请稍后再试'];
}
}
}