UserLoginLogic.php
4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
namespace App\Http\Logic\Bside\User;
use App\Enums\Common\Code;
use App\Exceptions\AsideGlobalException;
use App\Exceptions\BsideGlobalException;
use App\Helper\Common;
use App\Http\Logic\Aside\Project\ProjectLogic;
use App\Models\SmsLog;
use App\Models\User\ProjectRole as ProjectRoleModel;
use App\Models\User\User;
use Illuminate\Support\Facades\Cache;
class UserLoginLogic
{
const USER_STATUS = 0;
protected $model;
protected $param;
public function __construct()
{
//验证账号密码
$this->param = request()->all();
$this->model = new User();
}
/***
* @name :登录
* @author :liyuhang
* @method
*/
public function login(){
//验证账号密码是否正确
$info = $this->verifyAccount();
//验证角色是否被禁用+获取项目详情
$info = $this->verifyRole($info);
if(isset($info['token']) && !empty($info['token'])){
//清除上一次用户缓存
Cache::pull($info['token']);
}
//生成新token
$token = md5(uniqid().$info['id']);
//存储缓存
$info['token'] = $token;
Cache::add($token,$info);
//更新用户信息
$rs = $this->model->edit(['token'=>$token],['id'=>$info['id']]);
if($rs === false){
$this->fail('系统错误,请联系管理员');
}
//写入日志
Common::set_user_login(['user_id'=>$info['id'],'ip'=>request()->ip()]);
return $this->success($info);
}
/**
* @name :(验证账号、密码或验证码是否正确)verifyAccount
* @author :lyh
* @method :post
* @time :2023/6/12 15:31
*/
public function verifyAccount(){
$password = base64_encode(md5($this->param['password']));
$info = $this->model->read(['mobile'=>$this->param['mobile'],'password'=>$password,'status'=>$this::USER_STATUS],
['id','mobile','role_id','token','name','project_id']);
if($info === false){
//账号密码没通过时,验证验证码
$info = $this->model->read(['mobile'=>$this->param['mobile'],'status'=>$this::USER_STATUS],
['id','mobile','role_id','token','name','project_id']);
if($info === false){
$this->fail('账号密码错误',Code::USER_REGISTER_ERROE);
}
//验证验证码是否准备
$last_sms = SmsLog::getLastLog($this->param['mobile'], SmsLog::TYPE_LOGIN);
if($this->param['password'] != $last_sms->code){
$this->fail('账号密码错误',Code::USER_REGISTER_ERROE);
}
}
return $this->success($info);
}
/**
* @name :(验证角色是否禁用)verifyRole
* @author :lyh
* @method :post
* @time :2023/6/12 15:34
*/
public function verifyRole($info){
//当前用户角色是否被禁用
$projectRoleModel = new ProjectRoleModel();
$role_info = $projectRoleModel->read(['id'=>$info['role_id'],'status'=>$this::USER_STATUS]);
if($role_info === false){
$this->fail('当前用户角色被禁用',Code::USER_REGISTER_ERROE);
}
$project = (new ProjectLogic())->getInfo($info['project_id']);
$info['company'] = $project['company'] ?? '';
$info['plan'] = $project['deploy_build']['plan'][0] ?? '';
$info['domain'] = !empty($project['deploy_optimize']['domain']) ?
$project['deploy_optimize']['domain'] : ($project['deploy_build']['test_domain'] ?? '');
return $this->success($info);
}
/**
* @notes: 请简要描述方法功能
* @param array $data
* @return array
*/
public function success($data = [])
{
return $data;
}
/**
* @notes: 错误抛出
* @param string $code
* @param string $message
* @throws AsideGlobalException|BsideGlobalException
*/
public function fail(string $message = "", string $code = Code::SYSTEM_ERROR)
{
if((request()->path()[0]) == \App\Enums\Common\Common::B){
throw new BsideGlobalException($code, $message);
}
throw new AsideGlobalException($code, $message);
}
}