User.php
3.6 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
<?php
namespace App\Models;
//use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Models\ProjectRole as ProjectRoleModel;
use App\Models\User as UserModel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
//use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Cache;
use Laravel\Sanctum\HasApiTokens;
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], ['*']);
}else{
//TODO::验证验证码是否正确
$info = $this->read(['mobile'=>$param['mobile']],['*']);
}
if(empty($info)){
return false;
}
//当前用户角色是否被禁用
$projectRoleModel = new ProjectRoleModel();
$role_info = $projectRoleModel->read(['id'=>$info['role_id'],'status'=>0]);
if(empty($role_info)){
return false;
}
//验证码登录
if(isset($info['token']) && !empty($info['token'])){
//清除上一次用户缓存
Cache::pull($info['token']);
}
//生成新token
$token = md5(uniqid().$info['id']);
//存储缓存
Cache::add($token,$info);
$rs = $this->edit(['token'=>$token],['id'=>$info['id']]);
if($rs === false){
return false;
}
return $info;
}
//新增用户
public function adds($param){
//验证当前用户是否存在
$info = $this->read(['mobile'=>$param['mobile']]);
if(!empty($info)){
return false;
}
//密码加密
$param['password'] = base64_encode(md5($param['password']));
$rs = $this->add($param);
if($rs === false){
return false;
}
return true;
}
/**
* @param $param
* @name :编辑管理员
* @return bool
* @author :liyuhang
* @method
*/
public function edits($param){
//查看密码是否修改
$info = $this->read(['id'=>$param['id']]);
if($param['password'] == $info['password']){
unset($param['password']);
}
//密码加密
$param['password'] = base64_encode(md5($param['password']));
$rs = $this->edit($param,['id'=>$param['id']]);
if($rs === false){
return false;
}
//清空当前用户登录缓存
Cache::pull($info['token']);
return true;
}
}