User.php
3.4 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
<?php
namespace App\Models\User;
//use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Helper\Common;
use App\Models\Base;
use App\Models\User\ProjectRole as ProjectRoleModel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Cache;
use Laravel\Sanctum\HasApiTokens;
//use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Base
{
use HasApiTokens, HasFactory, Notifiable;
protected $table = 'gl_project_user';
//自动维护create_at创建时间 updated_at修改时间
public $timestamps = true;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
// 'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
'created_at' => 'datetime:Y-m-d H:i:s',
'updated_at' => 'datetime:Y-m-d H:i:s',
];
/***
* @name :登录
* @return void
* @author :liyuhang
* @method
*/
public function login($param){
if(!isset($param['login_method'])){
//密码加密
$param['password'] = base64_encode(md5($param['password']));
$info = $this->read(['mobile'=>$param['mobile']
,'password'=>$param['password'],'status'=>0], ['id','mobile','role_id','token','name','project_id']);
}else{
//TODO::验证验证码是否正确
$info = $this->read(['mobile'=>$param['mobile']],['*']);
}
if($info === false){
return false;
}
//当前用户角色是否被禁用
$projectRoleModel = new ProjectRoleModel();
$role_info = $projectRoleModel->read(['id'=>$info['role_id'],'status'=>0]);
if($role_info === false){
return false;
}
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->edit(['token'=>$token],['id'=>$info['id']]);
if($rs === false){
return false;
}
//写入日志
Common::set_user_login(['user_id'=>$info['id'],'ip'=>request()->ip()]);
return $info;
}
/**
* @param $param
* @name :编辑管理员
* @return bool
* @author :liyuhang
* @method
*/
public function edits($param){
//查看密码是否修改
$info = $this->read(['id'=>$param['id']]);
$param['password'] = base64_encode(md5($param['password']));
if($param['password'] == $info['password']){
unset($param['password']);
}
//密码加密
$rs = $this->edit($param,['id'=>$param['id']]);
if($rs === false){
return false;
}
//清空当前用户登录缓存
Cache::pull($info['token']);
return true;
}
}