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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<?php
namespace app\admin\controller;
use app\admin\model\DepartmentModel;
use app\admin\model\RoleModel;
use app\admin\model\StaffIdentity;
use app\admin\model\StaffModel;
use app\admin\model\StaffPropertyModel;
use app\admin\model\StaffRoleModel;
use app\admin\validate\StaffValidate;
use app\admin\model\ElectricMeter;
use app\admin\model\PermissionDataModel;
use think\facade\Session;
use think\Request;
use think\db;
/**
* 员工控制器
* 负责员工的增删改查
* 员工密码的初始化
* Class Staff
* @package app\admin\controller
*/
class Staff extends Base
{
public function index(Request $request)
{
$map=[];
$search_text = $request->get('search_text');
$search_type = $request->get('search_type');
if($search_text){
switch ($search_type) {
case '1':
$this->assign('search_text',$search_text);
$map[]=['staff.name','like',"%$search_text%"];
$this->assign('search_type',$search_type);
break;
case '2':
$this->assign('search_text',$search_text);
$map[]=['email','like',"%$search_text%"];
$this->assign('search_type',$search_type);
break;
case '3':
$this->assign('search_text',$search_text);
$map[]=['tel','like',"%$search_text%"];
$this->assign('search_type',$search_type);
break;
}
}else{
$this->assign('search_type','');
$this->assign('search_text','');
}
$page = $request->get('page')?$request->get('page'):1;
$limit = $request->get('limit')?$request->get('limit'):10;
if(empty($request->get('page'))){
$page_s=0;
}else{
$page_s=(($page-1)*$limit);
}
$statff= new StaffModel;
$statfflist= $statff->selectStaffList($map,$page,$limit);
$statffcount= $statff->selectStaffCount($map);
if($request->get('page')){
return ['code'=>0,'msg'=>'','count'=>$statffcount,'data'=>$statfflist];
}
return $this->fetch('index');
}
public function create()
{
$data = (new DepartmentModel)->getDepartmentTree();
$idens = StaffIdentity::field('id,title,level')->select();
$this->assign('idens',$idens);
$this->assign('data',$data);
return $this->fetch('add');
}
public function edit($id){
$staff = StaffModel::find($id);
$model = new DepartmentModel;
$data = $model->getDepartmentTree();
$idens = StaffIdentity::field('id,title,level')->select();
$this->assign('idens',$idens);
$this->assign('data',$data);
$this->assign('staff',$staff);
return $this->fetch('edit');
}
public function store(Request $request){
$validate = new StaffValidate;
if(!$validate->check($request->post())){
return jsonErr($validate->getError());
}
$staff = new StaffModel;
$staff->name = $request->post('name');
$staff->email = $request->post('email');
$staff->d_id = $request->post('d_id');
$staff->tel = $request->post('tel') + 0;
$staff->iden_id = $request->post('iden_id');
$staff->status = $request->post('status');
if($staff->save()){
return jsonSuc('添加员工成功');
} else {
return jsonErr('添加员工失败');
}
}
public function update(Request $request){
$id = $request->post('id') + 0;
$staff = StaffModel::find($id);
if($staff){
$staff->name = $request->post('name') ?: $staff->name;
$staff->email = $request->post('email') ?: $staff->email;
$staff->d_id = $request->post('d_id') ?: $staff->d_id;
$staff->tel = $request->post('tel') ?: $staff->tel;
$staff->iden_id = $request->post('iden_id') ?: $staff->iden_id;
$staff->status = $request->post('status') ?: $staff->status;
//查询 邮箱是否唯一
$staffCheck = StaffModel::
where([
['email','=',$request->post('email')],
['id','neq',$id]
])->find();
if(!$staffCheck){
if($staff->save()){
return jsonSuc('修改员工信息成功');
} else {
return jsonErr('修改员工信息失败');
}
}
return jsonErr('email已存在');
}
return jsonErr('参数错误');
}
public function delete(Request $request){
$id = $request->post('id');
if(is_int($id + 0) && ($id + 0) > 0) {
$staff =StaffModel::get($id);
if(!$staff){
return jsonErr('删除失败,用户不存在');
}
//如果删除成功
if($staff->delete()){
//删除权限
return $this->role_del($staff);
} else {
return jsonErr('删除失败请重试');
}
} else {
return jsonErr('参数错误');
}
}
//状态修改
public function status(Request $request){
$id = $request->post('id');
$staff= new StaffModel;
$status=$staff->getStaffStatusById($id);
if($status['status']==1){
$status= $staff->setStaffStatus($id,0);
return jsonSuc('状态禁用成功');
}else{
$status= $staff->setStaffStatus($id,1);
return jsonSuc('状态开启成功');
}
}
//数据导入
public function basestation_import(Request $request){
$file = $request->file('file_name');
$upload = new Upload($file,2);
$result = $upload->upload();
$fileurl=$_SERVER['DOCUMENT_ROOT'].$result['src'];
$excel = new Excel;
$excelresult=$excel->basestation_importExecl($fileurl);
if($excelresult['code']==200){
return jsonSuc('导入成功! 总导入条数为 '.$excelresult['data']['total'].' 条'.' 成功导入条数为 '.$excelresult['data']['suc']. ' 条'.' 失败'.$excelresult['data']['er']. '条');
}else{
return jsonErr('导入数据错误');
}
}
//配置员工所属角色匹配角色所属权限
public function role(){
$data = StaffModel::with('roles')->select();
$this->assign('data',$data);
$this->assign('empty','<span class="layui-bg-orange">[没有权限]</span>');
return $this->fetch('role');
}
public function role_edit($id){
$data = StaffModel::with('roles')->field('id,name')->find($id)->toArray();
$roles = RoleModel::field('id,title')->select()->toArray();
//筛选匹配的键
foreach ($roles as $key => $role){
foreach ($data['roles'] as $drole){
if($drole['id'] == $role['id']){
$roles[$key]['checked'] = true;
}
}
if(!isset($roles[$key]['checked'])){
$roles[$key]['checked'] = false;
}
}
$this->assign('roles',$roles);
$this->assign('data',$data);
return $this->fetch('role_edit');
}
public function staff_role_department($id){
$role = StaffModel::get($id)->toArray();
$permissionOwn = PermissionDataModel::where(['s_id'=>$id])->find();
$nodes = (new DepartmentModel)->getNodesTree();
$permissions = $permissionOwn['permission_ids'];
if($permissions){
$jsonpermissions=json_decode($permissions);
$permissions = explode(',',$jsonpermissions->department_id);
$repermissions = explode(',',$jsonpermissions->region_id);
$buspermissions = explode(',',$jsonpermissions->business_id);
$this->assign('buspermissions',$buspermissions);
$this->assign('repermissions',$repermissions);
$this->assign('permissions',$permissions);
}else{
$this->assign('buspermissions',[]);
$this->assign('repermissions',[]);
$this->assign('permissions',[]);
}
$regionnodes = getNodesTree((new ElectricMeter)->regions());
$businessnodes= (new ElectricMeter)->business();
$this->assign('data',$role);
$this->assign('nodes',$nodes);
$this->assign('regionnodes',$regionnodes);
$this->assign('businessnodes',$businessnodes);
return $this->fetch('staff_role_department');
}
public function depat_role_update(Request $request){
$id = $request->post('id');
//区域
$repermission=$request->post('repermission');
//业务线
$buspermission=$request->post('buspermission');
//部门
$result = $this->addDepartPermission($id,$request->post('permission'),$repermission,$buspermission);
if($result){
return jsonSuc('基站数据权限设置成功');
} else {
return jsonErr('基站数据权限设置失败');
}
}
private function addDepartPermission($id, $permission = [],$repermissions = [],$buspermissions = []) {
$DepartPermission = new PermissionDataModel;
$model = $DepartPermission->where(['s_id'=>$id])->find();
if($permission){
$permissions = implode(',',$permission);
$convert['department_id']=$permissions;
}else{
$convert['department_id']='';
}
if($repermissions){
$repermissions = implode(',',$repermissions);
$convert['region_id']=$repermissions;
}else{
$convert['region_id']='';
}
if($buspermissions){
$buspermissions = implode(',',$buspermissions);
$convert['business_id']=$buspermissions;
}else{
$convert['business_id']='';
}
if($model){
$model->permission_ids = json_encode($convert);
return $model->save();
} else {
$DepartPermission->s_id = $id;
$DepartPermission->permission_ids = json_encode($convert);
return $DepartPermission->save();
}
}
/**
* 添加用户权限
* @param Request $request
* @return \think\response\Json
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
* @throws \think\exception\PDOException
*
*/
public function role_update(Request $request){
$id = $request->post('id');
$roles = $request->post('role');
if(!$id){
return jsonErr('参数有错误刷新之后添加');
}
$staffRole = StaffRoleModel::where(['staff_id'=>$id])->find();
//如果没有记录,则更新
if($staffRole) {
//先清除所有权限
StaffRoleModel::where(['staff_id' => $id])->delete();
}
//生成权限数据
$staffRoleList = [];
foreach ($roles as $key => $role){
$staffRoleList[$key]['staff_id'] = $id;
$staffRoleList[$key]['role_id'] = $role;
}
//批量插入权限
if((new StaffRoleModel)->saveAll($staffRoleList)){
return jsonSuc('添加权限成功');
} else {
return jsonErr('添加权限失败');
}
}
public function role_del($staff=''){
if(!empty($staff)){
$id = $staff->id;
} else {
$id = Request::post('id');
}
if(is_int($id + 0) && ($id + 0) > 0){
if(StaffRoleModel::where(['staff_id' => $id])->delete()){
return jsonSuc('删除权限成功');
} else {
return jsonErr('删除失败,用户没有设置权限');
}
} else {
return jsonErr('参数错误');
}
}
/**
* 重置密码
* @param Request $request
* @return \think\response\Json
*/
public function resetPass(Request $request){
$id = $request->post('id') + 0;
$staff = StaffModel::get(['id'=>$id]);
$staffProperty = StaffPropertyModel::where(['staff_id'=>$id])->find();
$pass = createPass();
if($staffProperty){ //之前有密码
$staffProperty->passwd = md5($pass.config('admin.passwd_salt'));
} else { //新用户创建
$staffProperty = new StaffPropertyModel;
$staffProperty->passwd = md5($pass.config('admin.passwd_salt'));
$staffProperty->staff_id = $id;
}
$message =
[
'username' => $staff->email,
'pass' => $pass
];
if($staffProperty->save()){
return jsonSuc('密码重置成功', $message);
} else {
return jsonErr('重置密码失败,请重试');
}
}
public function getIdentity($uid= ''){
if(empty($uid)){
$user = $this->getCurrentUser();
if($this->isAdmin()){
return 'admin';
}
return $user->iden_id;
}
}
public function getCurrentUser(){
$user = Session::get('user');
return StaffModel::get($user['user_id']);
}
public function isAdmin(){
$user = Session::get('user');
return $user['is_admin'];
}
}