UserLogic.php
5.7 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
namespace App\Http\Logic\Bside\User;
use App\Enums\Common\Code;
use App\Helper\Common;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\SmsLog;
use App\Models\User\ProjectRole as ProjectRoleModel;
use App\Models\User\User;
use Illuminate\Support\Facades\Cache;
class UserLogic extends BaseLogic
{
public function __construct()
{
parent::__construct();
$this->model = new User();
$this->param = $this->requestAll;
}
/**
* @name :用户详情
* @return void
* @author :liyuhang
* @method
*/
public function user_info(){
$info = $this->model->read($this->param);
return $this->success($info);
}
/**
* @name :添加会员
* @author :liyuhang
*/
public function user_add(){
//验证当前用户是否存在
$info = $this->model->read(['mobile'=>$this->param['mobile']]);
if($info !== false){
$this->fail('当前手机号码已注册');
}
$this->param['create_id'] = $this->user['id'];
$this->param['operator_id'] = $this->user['id'];
$this->param['project_id'] = $this->user['project_id'];
//上传图片
if(isset($this->param['image'])){
$this->param['image'] = $this->upload();
}
//密码加密
$this->param['password'] = base64_encode(md5($this->param['password']));
$rs = $this->model->add($this->param);
if($rs === false){
$this->fail('error');
}
return $this->success();
}
/**
* @name :编辑用户
* @author :liyuhang
*/
public function user_edit(){
$condition = [
'id'=>['!=',$this->param['id']],
'mobile'=>$this->param['mobile']
];
$info = $this->model->read($condition);
if($info !== false){
$this->fail('当前编辑的手机号码已存在');
}
$this->param['operator_id'] = $this->user['id'];
try {
//上传图片
if(isset($this->param['image']) && is_file($this->param['image'])){
$this->param['image'] = $this->upload();
}
$this->model->edits($this->param);
}catch (\exception $e){
$this->fail('参数错误或其他服务器原因,编辑失败');
}
return $this->success();
}
/**
* @name :编辑状态/排序
* @author :liyuhang
*/
public function user_status(){
$this->param['operator_id'] = $this->user['id'];
$rs = $this->model->edit($this->param,['id'=>$this->param['id']]);
if($rs === false){
$this->fail('error',Code::USER_ERROR);
}
return $this->success();
}
/**
* @name :删除用户(逻辑删除)
* @return void
* @author :liyuhang
* @method
*/
public function user_del(){
$this->param['id'] = ['in',$this->param['id']];
$this->model->del($this->param);
//对应删除组织架构
return $this->success();
}
/***
* @name :登录
* @return void
* @author :liyuhang
* @method
*/
public function login(){
//验证账号密码
$password = base64_encode(md5($this->param['password']));
$info = $this->model->read(['mobile'=>$this->param['mobile'],'password'=>$password,'status'=>0], ['id','mobile','role_id','token','name','project_id']);
if($info === false){
//账号密码没通过时,验证验证码
$info = $this->model->read(['mobile'=>$this->param['mobile'],'status'=>0], ['id','mobile','role_id','token','name','project_id']);
if($info === false){
$this->fail('账号密码错误',Code::USER_REGISTER_ERROE);
}
//验证验证码是否准备
$last_sms = SmsLog::getLastLog($this->param['mobile'], SmsLog::TYPE_LOGIN);
if($this->param['password'] != $last_sms->code){
$this->fail('账号密码错误',Code::USER_REGISTER_ERROE);
}
}
//当前用户角色是否被禁用
$projectRoleModel = new ProjectRoleModel();
$role_info = $projectRoleModel->read(['id'=>$info['role_id'],'status'=>0]);
if($role_info === false){
$this->fail('当前用户角色被禁用',Code::USER_REGISTER_ERROE);
}
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->model->edit(['token'=>$token],['id'=>$info['id']]);
if($rs === false){
$this->fail('系统错误,请联系管理员');
}
//写入日志
Common::set_user_login(['user_id'=>$info['id'],'ip'=>request()->ip()]);
return $this->success($info);
}
/**
* @param $param
* @name :编辑管理员
* @return bool
* @author :liyuhang
* @method
*/
public function edits($param){
//查看密码是否修改
$info = $this->model->read(['id'=>$param['id']]);
$param['password'] = base64_encode(md5($param['password']));
if($param['password'] == $info['password']){
unset($param['password']);
}
//密码加密
$rs = $this->model->edit($param,['id'=>$param['id']]);
if($rs === false){
$this->fail('系统错误,请联系管理员');
}
//清空当前用户登录缓存
Cache::pull($info['token']);
return $this->success();
}
}