LoginLogic.php
3.8 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
<?php
namespace App\Http\Logic\Aside;
use App\Enums\Common\Common;
use App\Models\Manage\Manage;
use App\Models\Manage\LoginLog;
use App\Models\Manage\MenuSpecial;
use App\Models\Service\Service;
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')
->where('mobile', $this->requestAll['mobile'])->first();
if (!$manage){
$this->fail('登录用户名不存在');
}
if (Manage::STATUS_DISABLE == $manage->status) {
$this->fail('帐号已被禁用');
}
if (!Hash::check($this->requestAll['password'], $manage->password)) {
$this->fail('登录密码不正确');
}
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);
//更新用户信息
$manage->token = $token;
$res = $manage->save();
if(!$res){
$this->fail('系统错误,请联系管理员');
}
LoginLog::addLog($manage->id);
//获取当前用户特殊模块权限
$manage['special'] = $this->getSpecialMenu($manage['id']);
return $this->success($manage->makeVisible('token')->toArray());
}
public function logout(){
Cache::pull(request()->header('token'));
return $this->success();
}
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;
}
}