2db47680
hexiaodong
chore: 初始提交 php 遗...
|
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
<?php
namespace addons\antis\model;
use addons\antis\library\StaffAuth;
use app\admin\controller\antis\Base;
use app\admin\library\Auth;
use app\admin\model\AuthGroup;
use app\admin\model\AuthGroupAccess;
use think\Db;
use think\Model;
use traits\model\SoftDelete;
use app\admin\model\Admin;
/**
*员工表
*/
class Staff Extends Model {
use SoftDelete;
// 表名,不含前缀
protected $name = 'qingdong_staff';
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = 'deletetime';
// 追加属性
protected $append = [
'group_text',
];
protected static function init()
{
self::beforeInsert(function ($row) {
$changed = $row->getChangedData();
$admin = [
'username' => $changed['mobile'],
'nickname' => $changed['name'],
'password' => $changed['password'],
'salt' => $changed['salt'],
'avatar' => $changed['img'],
'email' => $changed['email'],
];
if(isset($changed['admin_id']) && $changed['admin_id']){
return true;
}
$adminModel = new Admin();
$result=$adminModel->validate('Admin.add')->save($admin);
if($result == false){
exception($adminModel->getError());
}
$row->admin_id = $adminModel->getLastInsID();
$group = explode(',', $changed['group_ids']);
foreach ($group as $value) {
$dataset[] = ['uid' => $row->admin_id, 'group_id' => $value];
}
model('AuthGroupAccess')->saveAll($dataset);
return $row;
});
self::beforeUpdate(function ($row) {
$changed = $row->getChangedData();
if(!isset($row->id)){
return true;
}
$staff = self::get($row->id);
if (empty($staff->admin_id)) {
return $row;
}
//admin用户不更新
if($staff->admin_id == 1){
return true;
}
if(isset($changed['deletetime']) && $changed['deletetime']){
Admin::where(['id' => $staff->admin_id])->delete();
return true;
}
if(isset($changed['mobile']) || isset($changed['name'])
|| isset($changed['email']) || isset($changed['img']) ){
$params = [];
if(isset($changed['mobile'])){
$params['username']=$changed['mobile'];
}
if(isset($changed['name'])){
$params['nickname']=$changed['name'];
}
if(isset($changed['img'])){
$params['avatar']=$changed['img'];
}
if(isset($changed['email'])){
$params['email']=$changed['email'];
}
//如果有修改密码
if (isset($changed['password'])) {
if ($changed['password']) {
$params['password'] = $changed['password'];
$params['salt'] = $changed['salt'];
}
}
$adminModel = new Admin();
$result = $adminModel->save($params, ['id' => $staff->admin_id]);
if ($result === false) {
exception($row->getError());
}
}
if(isset($changed['group_ids'])){
// 先移除所有权限
model('AuthGroupAccess')->where('uid', $staff->admin_id)->delete();
$group = explode(',', $changed['group_ids']);
foreach ($group as $value) {
$dataset[] = ['uid' => $staff->admin_id, 'group_id' => $value];
}
model('AuthGroupAccess')->saveAll($dataset);
}
return $row;
});
}
// 图片
public function getGroupTextAttr($value, $data)
{
if(!isset($data['group_ids'])){
return '';
}
$names=AuthGroup::where(['id'=>['in',$data['group_ids']]])->column('name');
return implode(',',$names);
}
public static function info()
{
if (StaffAuth::instance()->id) {
return StaffAuth::instance();
}
$auth = new Auth();
if ($auth->isLogin()) {
$base = new Base();
return $base->getStaff();
}
return null;
}
public function getImgAttr($value) {
if ($value) {
return cdnurl($value, true);
} else {
return $value;
}
}
public function getCreatetimeAttr($value){
return date('Y-m-d H:i:s',$value);
}
//员工业绩
public function achievement() {
return $this->belongsTo(Achievement::class, 'id', 'obj_id')->where(['type' => 3]);
}
//团队业绩
public function teamAchievement() {
$condition['type'] = ['in',[2,4]];
return $this->belongsTo(Achievement::class, 'id', 'obj_id')->where($condition);
}
public function department() {
return $this->belongsTo(StaffDepartment::class, 'department_id', 'id')->field('id,name as department_name')->bind('department_name');
}
//绑定admin账号
public function admin() {
return $this->belongsTo(Admin::class, 'admin_id', 'id')->field('id,username');
}
//角色
public function staffrole() {
return $this->hasOne(StaffRole::class, 'id', 'role')->field('id,name');
}
//上级
public function parent() {
return $this->belongsTo(Staff::class, 'parent_id', 'id')->field('id,name as parent_name')->bind('parent_name');
}
//获取员工
public static function getList($notInIds=[]) {
$where=['status'=>1];
if($notInIds){
$where['id']=['not in',$notInIds];
}
return self::where($where)->field('id,name')->select();
}
//包含当前角色
public static function getLowerId($l_ids,$top=true){
if(!is_array($l_ids)){
$l_ids=explode(',',$l_ids);
}
$ids=AuthGroup::where(['pid' =>['in',$l_ids]])->column('id');
if ($ids) {
$w_ids = self::getLowerId($ids,false);
$ids = array_merge($ids, $w_ids);
}else{
$ids=[];
}
if($top){
$ids=array_merge($ids,$l_ids);
}
return array_unique($ids);
}
//获取下属员工IDs
public static function getLowerStaffId()
{
$staff = self::info();
$groupIds = AuthGroupAccess::where(['uid' => $staff->admin_id])->column('group_id');
$role_type = StaffRole::where(['id' => $staff->role])->value('role_type');
switch ($role_type) {
case 1://本人
$l_ids = [];
break;
case 2://本人及下属
$l_ids = self::where([
'parent_id' => $staff->id,
])->column('id');
break;
case 3://本部门
$uids = AuthGroupAccess::where(['group_id' => ['in', $groupIds]])->column('uid');
$l_ids = self::where([
'admin_id' => ['in', $uids],
'status' => 1,
'id' => ['neq', $staff->id]
])->column('id');
break;
case 4://仅下属部门
$groupIds = self::getLowerId($groupIds, false);
$uids = AuthGroupAccess::where(['group_id' => ['in', $groupIds]])->column('uid');
$l_ids = self::where([
'admin_id' => ['in', $uids],
'status' => 1,
'id' => ['neq', $staff->id]
])->column('id');
break;
case 5://本部门及下属部门
$groupIds = self::getLowerId($groupIds, true);
$uids = AuthGroupAccess::where(['group_id' => ['in', $groupIds]])->column('uid');
$l_ids = self::where([
'admin_id' => ['in', $uids],
'status' => 1,
'id' => ['neq', $staff->id]
])->column('id');
break;
case 6://全部
$l_ids = self::where([
'status' => 1,
'id' => ['neq', $staff->id]
])->column('id');
break;
}
if (empty($l_ids)) {//返回空 下属查询 会查询全部
return ['-1'];
}
return $l_ids;
}
//获取全部ID
public static function getMyStaffIds() {
$staff = self::info();
$ids = [$staff->id];
$l_ids = self::getLowerStaffId();
$ids = array_merge($ids, $l_ids);
return $ids;
}
public static function getKeyList() {
$staffs=self::where([])->field('id,name,img,group_ids')->select();
$data=[];
foreach ($staffs as $v){
$data[$v['id']]=$v;
}
return $data;
}
public static function getGroupStaffIds($group_id)
{
return self::where('', 'exp', Db::raw('FIND_IN_SET(' . intval($group_id) . ',group_ids)'))->column('id');
}
public static function getStaff(){
return self::where([])->column('name', 'id');
}
//获取自己及下属员工
public static function allList($notInIds=[]) {
$where['id']=['in',self::getMyStaffIds()];
$where['status']=1;
return self::where($where)->field('id,name')->select();
}
/**
* 获取权限列表
*/
public static function getStaffRule($type)
{
$row = StaffRule::where(['name' => $type])->find();
if (!$row) {
return [];
}
$rules = StaffRule::where(['pid' => $row->id])->column('name', 'id');
$staff = self::info();
$staffRules = StaffRole::where(['id' => $staff->role])->value('rules');
$staffRules = explode(',', $staffRules) ?? [];
$value = [];
foreach ($staffRules as $r) {
if (isset($rules[$r])) {
$value[] = $rules[$r];
}
}
return $value;
}
}
|