LoginLogic.php
5.1 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
namespace App\Http\Logic\Aside;
use App\Enums\Common\Code;
use App\Enums\Common\Common;
use App\Models\Manage\LoginLog;
use App\Models\Manage\Manage;
use App\Models\Manage\MenuSpecial;
use App\Models\Service\Service;
use App\Models\Sms\SmsLog;
use App\Utils\EncryptUtils;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Hash;
/**
* Class LoginLogic
* @package App\Http\Logic\Aside
* @author zbj
* @date 2023/4/19
*/
class LoginLogic extends BaseLogic
{
public function __construct()
{
parent::__construct();
$this->param = $this->requestAll;
$this->model = new Manage();
}
public function login()
{
$manage = $this->model->select('id', 'name', 'password', 'token', 'status', 'gid', 'dept_id','role')
->where('mobile', $this->param['mobile'])->first();
if (!$manage){
$this->fail('登录用户名不存在');
}
if (Manage::STATUS_DISABLE == $manage->status) {
$this->fail('帐号已被禁用');
}
$type = 1;//账号密码登录
if (!Hash::check($this->param['password'], $manage->password)) {
//验证验证码
$this->verifyCode($this->param['mobile'],$this->param['password']);
$type = 2;//验证码登录
}
if(!empty($manage['token'])){
//清除上一次用户缓存
Cache::pull(Common::MANAGE_TOKEN . $manage['token']);
}
//生成新token
$token = md5(uniqid().$manage['id']);
//存储缓存
$manage['token'] = $token;
Cache::add(Common::MANAGE_TOKEN . $token,$manage,3600 * 6);
//更新用户信息
$manage->token = $token;
$res = $manage->save();
if(!$res){
$this->fail('系统错误,请联系管理员');
}
LoginLog::addLog($manage->id,$type);
//获取当前用户特殊模块权限
$manage['special'] = $this->getSpecialMenu($manage['id']);
return $this->success($manage->makeVisible('token')->toArray());
}
/**
* @remark :退出登录
* @name :logout
* @author :lyh
* @method :post
* @time :2023/9/7 16:30
*/
public function logout(){
Cache::pull(request()->header('token'));
return $this->success();
}
/**
* @remark :获取登录详情
* @name :manage
* @author :lyh
* @method :post
* @time :2023/9/7 16:30
*/
public static function manage($field = ''){
$manage = Cache::get(Common::MANAGE_TOKEN . request()->header('token'));
$manage = Manage::find($manage['id'] ?? 0);
if($field){
return $manage[$field] ?? '';
}
return $manage;
}
/**
* @remark :获取访问地址
* @name :accessAddress
* @author :lyh
* @method :post
* @time :2023/8/7 9:09
*/
public function accessAddress(){
$serviceSettingModel = new Service();
$info = $serviceSettingModel->read(['type'=>4]);
if($info === false){
$this->fail('当前地址不存在或者已被删除');
}
$encrypt = new EncryptUtils();
$data = [
'domain'=>$info['values'],
'remark'=>'自动登录地址和code',
];
//获取超级管理员登录
if(isset($this->param['project_id']) && !empty($this->param['project_id'])){
$data['autologin_code'] = $encrypt->lock_url(json_encode(['project_id'=>$this->param['project_id'],'manager_id'=>$this->manager['id']]),$info['values']);
}
//使用用户登录
if(isset($this->param['user_id']) && !empty($this->param['user_id'])){
$data['autologin_code'] = $encrypt->lock_url(json_encode(['user_id'=>$this->param['user_id'],'manager_id'=>$this->manager['id']]),$info['values']);
}
//获取当前超级管理员的token
return $this->success($data);
}
/**
* @remark :获取当前登录用户特殊权限模块
* @name :getSpecialMenu
* @author :lyh
* @method :post
* @time :2023/8/8 13:56
*/
public function getSpecialMenu($id){
$specialMenuModel = new MenuSpecial();
$list = $specialMenuModel->list(['user_list'=>['like','%,'.$id.',%']],'id',['id','name','remark']);
return $list;
}
/**
* @remark :验证验证码是否正确或是否过期
* @name :verifyCode
* @author :lyh
* @method :post
* @time :2023/7/25 17:17
*/
public function verifyCode($mobile,$password){
//账号密码没通过时,验证验证码
$smsModel = new SmsLog();
$smsInfo = $smsModel->formatQuery(['mobile'=>$mobile,'type'=>$smsModel::TYPE_MANAGER_LOGIN])->orderBy('id','desc')->first();
if(!empty($smsInfo)){
if(($password != $smsInfo['code']) || ($smsInfo['created_at'] < date('Y-m-d H:i:s',time() - 300))){
$this->fail('账号密码错误/验证码错误',Code::USER_LOGIN_ERROE);
}
}else{
$this->fail('账号密码错误/验证码错误',Code::USER_LOGIN_ERROE);
}
return true;
}
}