LoginLogic.php
2.0 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
<?php
namespace App\Http\Logic\Aside;
use App\Enums\Common\Common;
use App\Models\Manage\Manage;
use App\Models\Manage\LoginLog;
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->model = new Manage();
}
public function login()
{
$manage = $this->model->select('id', 'name', 'password', 'token', 'status')->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);
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;
}
}