作者 lyh

gx

... ... @@ -44,8 +44,28 @@ class LoginController extends BaseController
'mobile.regex' => '请输入正确的手机号码',
]);
$userLogic = new UserLoginLogic();
$res = $userLogic->login();
$this->response('请求成功',Code::SUCCESS,$res);
$project = $userLogic->login();
$this->response('success',Code::SUCCESS,$project);
}
/**
* @remark :根据选择的项目id登录
* @name :projectLogin
* @author :lyh
* @method :post
* @time :2023/8/29 15:21
*/
public function projectLogin(){
$this->request->validate([
'project_id'=>'required',
'mobile'=>['required'],
],[
'project_id.required'=>'请选择项目',
'mobile.required'=>'参数错误',
]);
$userLogic = new UserLoginLogic();
$userLogic->projectLogin();
$this->response('success');
}
/**
... ... @@ -68,7 +88,7 @@ class LoginController extends BaseController
$this->response('无效Code',Code::USER_ERROR);
}
$res = $logic->autologin($data);
$this->response('请求成功',Code::SUCCESS, $res);
$this->response('success',Code::SUCCESS, $res);
}
... ...
... ... @@ -28,17 +28,45 @@ class UserLoginLogic
}
/**
* @remark :登录接口
* @remark :(登录)
* @name :verifyAccountPassword
* @author :lyh
* @method :post
* @time :2023/8/29 14:50
*/
public function login(){
//先验证手机号是否在项目中存在
$info = $this->model->read(['mobile'=>$this->param['mobile'],'status'=>$this->model::STATUS_ZERO],['id']);
if($info === false){
$this->fail('当前用户不存在或者被禁用',Code::USER_REGISTER_ERROE);
}
//查看当前账号下有几个项目
$list = $this->model->list(['mobile'=>$this->param['mobile'],
'password'=>$this->param['password'],'status'=>$this->model::STATUS_ZERO],'id',['id','project_id']);
if(empty($list)){
//验证code
$list = $this->verifyCode($this->param['mobile'],$this->param['password']);
}
//获取所有项目的项目id
foreach ($list as $v){
$projectArr[] = $v['project_id'];
}
$projectModel = new Project();
$project_list = $projectModel->list(['id'=>['in',$projectArr]],'id',['id','title']);
return $this->success($project_list);
}
/**
* @remark :根据选择的项目登录
* @name :login
* @author :lyh
* @method :post
* @time :2023/6/17 16:43
*/
public function login(){
//验证账号密码是否正确
$info = $this->verifyAccount();
//验证角色是否被禁用+获取项目详情
$info = $this->verifyRole($info);
public function projectLogin(){
//获取项目详情
$info = $this->assembleParam($this->param['mobile'],$this->param['project_id']);
if(isset($info['token']) && !empty($info['token'])){
//清除上一次用户缓存
Cache::pull($info['token']);
... ... @@ -58,6 +86,7 @@ class UserLoginLogic
return $this->success($info);
}
/**
* 自动登录
* @author zbj
... ... @@ -71,12 +100,12 @@ class UserLoginLogic
}
//根据特定用户自动登录
if(isset($data['user_id']) && !empty($data['user_id'])){
$has_user = $this->model->read(['id'=>$data['user_id']]);
$has_user = $this->model->read(['id'=>$data['user_id'],'status'=>$this->model::STATUS_ZERO]);
}
if ($has_user === false) {
$this->fail('该项目未找到注册账号');
}
$info = $this->verifyRole($has_user);
$info = $this->autoAssembleParam($has_user);
if(!Cache::get($info['token'])){
//生成新token
$token = md5(uniqid().$info['id']);
... ... @@ -91,29 +120,6 @@ class UserLoginLogic
}
/**
* @name :(验证账号、密码或验证码是否正确)verifyAccount
* @author :lyh
* @method :post
* @time :2023/6/12 15:31
*/
public function verifyAccount(){
$info = $this->model->read(['mobile'=>$this->param['mobile']],['id','mobile','status','password','role_id','wechat','token','name','project_id']);
if($info === false){
$this->fail('当前用户不存在',Code::USER_REGISTER_ERROE);
}
if($info['status'] != self::USER_STATUS){
$this->fail('当前用户被禁用',Code::USER_REGISTER_ERROE);
}
//密码加密
$password = base64_encode(md5($this->param['password']));
if($password != $info['password']){
$this->verifyCode($this->param['mobile'],$this->param['password']);
}
unset($info['password']);
return $this->success($info);
}
/**
* @remark :验证验证码是否正确或是否过期
* @name :verifyCode
* @author :lyh
... ... @@ -131,24 +137,17 @@ class UserLoginLogic
}else{
$this->fail('账号密码错误/验证码错误',Code::USER_REGISTER_ERROE);
}
return true;
$list = $this->model->list(['mobile'=>$this->param['mobile'],'status'=>$this->model::STATUS_ZERO],['id','project_id']);
return $this->success($list);
}
/**
* @name :(验证角色是否禁用)verifyRole
* @name :自动登录组装返回数据
* @author :lyh
* @method :post
* @time :2023/6/12 15:34
*/
public function verifyRole($info){
//当前用户角色是否被禁用
$projectRoleModel = new ProjectRoleModel();
if($info['role_id'] != 0){
$role_info = $projectRoleModel->read(['id'=>$info['role_id'],'status'=>$this::USER_STATUS]);
if($role_info === false){
$this->fail('当前用户角色被禁用',Code::USER_REGISTER_ERROE);
}
}
//获取项目详情
public function autoAssembleParam($info){
$project = $this->getProjectInfo($info['project_id']);
$info['company'] = $project['company'] ?? '';
$info['plan'] = Project::planMap()[$project['deploy_build']['plan']];
... ... @@ -160,6 +159,27 @@ class UserLoginLogic
}
/**
* @remark :组装返回数据
* @name :getProjectInfo
* @author :lyh
* @method :post
* @time :2023/8/29 15:26
*/
public function assembleParam($mobile,$project_id){
$info = $this->model->read(['mobile'=>$mobile,'project_id'=>$project_id],['id','mobile','status','role_id','token','name','project_id']);
//获取项目详情
$project = $this->getProjectInfo($project_id);
$info['company'] = $project['company'] ?? '';
$info['plan'] = Project::planMap()[$project['deploy_build']['plan']];
$info['domain'] = (!empty($project['deploy_optimize']['domain']) ?
$project['deploy_optimize']['domain'] : ($project['deploy_build']['test_domain'] ?? ''));
//保存项目缓存
Cache::add('user-'.$project_id,$project);
return $this->success($info);
}
/**
* @remark :获取项目数据详情
* @name :getProjectInfo
* @author :lyh
... ...
... ... @@ -12,6 +12,9 @@ class User extends Base
// use HasApiTokens, HasFactory, Notifiable;
const ROLE_MANAGER = 0;//超级管理员
const TYPE_ONE = 1;
const STATUS_ZERO = 0;
protected $table = 'gl_project_user';
//自动维护create_at创建时间 updated_at修改时间
public $timestamps = true;
... ...