UserController.php
17.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
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use cmf\controller\AdminBaseController;
use think\Db;
/**
* Class UserController
* @package app\admin\controller
* @adminMenuRoot(
* 'name' => '管理组',
* 'action' => 'default',
* 'parent' => 'user/AdminIndex/default',
* 'display'=> true,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => '管理组'
* )
*/
class UserController extends AdminBaseController
{
/**
* 管理员列表
* @adminMenu(
* 'name' => '管理员',
* 'parent' => 'default',
* 'display'=> true,
* 'hasView'=> true,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => '管理员管理',
* 'param' => ''
* )
*/
public function index()
{
$where = ["user_type" => 1];
/**搜索条件**/
$userLogin = $this->request->param('user_login');
$userEmail = trim($this->request->param('user_email'));
if ($userLogin) {
$where['user_login'] = ['like', "%$userLogin%"];
}
if ($userEmail) {
$where['user_email'] = ['like', "%$userEmail%"];;
}
$users = Db::name('user')
->where($where)
->order("id DESC")
->paginate(10);
$users->appends(['user_login' => $userLogin, 'user_email' => $userEmail]);
// 获取分页显示
$page = $users->render();
$rolesSrc = Db::name('role')->select();
$roles = [];
foreach ($rolesSrc as $r) {
$roleId = $r['id'];
$roles["$roleId"] = $r;
}
$this->assign("page", $page);
$this->assign("roles", $roles);
$this->assign("users", $users);
return $this->fetch();
}
public function student()
{
/**查询条件**/
$where = ["user_type" => 2];
/**搜索条件**/
$user_nickname = $this->request->param('user_nickname');
$mobile = trim($this->request->param('mobile'));
$id = trim($this->request->param('id'));
if ($id) {
$where['id'] = $id ;
}
if ($user_nickname) {
$where['user_nickname'] = $user_nickname ;
}
if ($mobile) {
$where['mobile'] = $mobile ;
}
/**数据库检索**/
$users = Db::name('user')
->where($where)
->order("id DESC")
->paginate(10);
$page = $users->render();// 获取分页显示
/**检索结果加入搜索条件**/
if ($user_nickname or $mobile) {
$users->appends(['user_nickname' => $user_nickname, 'mobile' => $mobile]);
}
/**数据赋值到模板**/
$this->assign("page", $page);
$this->assign("users", $users);
return $this->fetch();
}
//添加
public function student_add()
{
return $this->fetch();
}
public function student_add_do()
{
$p = $this->request->param();
$datain['mobile'] = $p['mobile'];
$res = DB::name('user')->where($datain)->find();
if ($res) {
$this->error("该手机号数据库已经存在!");
}
$datain['headimgurl'] = '/upload/' . $p['headimgurl'];
$datain['user_nickname'] = $p['user_nickname'];
$datain['sex'] = $p['sex'];
$datain['idnumber'] = $p['idnumber'];
$datain['birthday'] = $p['birthday'];
$datain['kemu'] = $p['kemu'];
$datain['add_type'] = '2';
$datain['student_type'] = '1';
$datain['user_type'] = 2;
$datain['create_time'] = time();
$result = DB::name('user')->insert($datain);
if ($result !== false) {
$this->success("添加成功!",url('student'));
} else {
$this->error("添加失败!");
}
} //添加
public function student_edit()
{
$id = $this->request->param('id', 0, 'intval');
$info = DB::name('user')->where(['id' => $id])->find();
$this->assign('info', $info);
return $this->fetch();
}
public function student_edit_do()
{
$p = $this->request->param();
$datain['user_nickname'] = $p['user_nickname'];
$datain['user_type'] = 2;
$datain['create_time'] = time();
$datain['mobile'] = $p['mobile'];
$datain['id'] = $p['id'];
$datain['headimgurl'] = $p['headimgurl'];
$datain['sex'] = $p['sex'];
$datain['idnumber'] = $p['idnumber'];
$datain['birthday'] = $p['birthday'];
$datain['kemu'] = $p['kemu'];
$garbage = strstr($datain['headimgurl'], "upload");
if (!$garbage) {
$datain['headimgurl'] = '/upload/' . $datain['headimgurl'];
}
$result = DB::name('user')->update($datain);
if ($result !== false) {
$this->success("保存成功",url('student'));
} else {
$this->error("保存失败!");
}
}
public function student_delete()
{
$id = $this->request->param('id', 0, 'intval');
$result = DB::name('user')->where(['id' => $id])->delete();
if ($result !== false) {
$this->success("删除成功!");
} else {
$this->error("删除失败!");
}
}
public function trainer()
{
$where = ["user_type" => 3];
/**搜索条件**/
$userLogin = $this->request->param('user_login');
$userEmail = trim($this->request->param('name'));
if ($userLogin) {
$where['user_login'] = ['like', "%$userLogin%"];
}
if ($userEmail) {
$where['user_nickname'] = ['like', "%$userEmail%"];;
}
$users = Db::name('user')
->where($where)
->order("id DESC")
->paginate(10);
$users->appends(['user_login' => $userLogin, 'user_email' => $userEmail]);
// 获取分页显示
$page = $users->render();
$rolesSrc = Db::name('role')->select();
$roles = [];
foreach ($rolesSrc as $r) {
$roleId = $r['id'];
$roles["$roleId"] = $r;
}
$this->assign("page", $page);
$this->assign("roles", $roles);
$this->assign("users", $users);
return $this->fetch();
}
/**
* 管理员添加
* @adminMenu(
* 'name' => '管理员添加',
* 'parent' => 'index',
* 'display'=> false,
* 'hasView'=> true,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => '管理员添加',
* 'param' => ''
* )
*/
public function add()
{
$roles = Db::name('role')->where(['status' => 1])->order("id DESC")->select();
$this->assign("roles", $roles);
return $this->fetch();
}
/*********添加教练**********/
//添加
public function coach_add()
{
return $this->fetch();
}
public function coach_add_do()
{
$p = $this->request->param();
$datain['mobile'] = $p['mobile'];
$res = DB::name('user')->where($datain)->find();
if ($res) {
$this->error("该手机号数据库已经存在!");
}
$datain['headimgurl'] = '/upload/' . $p['headimgurl'];
$datain['user_nickname'] = $p['user_nickname'];
$datain['add_type'] = '2';
$datain['sex'] = $p['sex'];
$datain['idnumber'] = $p['idnumber'];
$datain['birthday'] = $p['birthday'];
$datain['more'] = $p['more'];
$datain['user_type'] = 3;
$datain['create_time'] = time();
$result = DB::name('user')->insert($datain);
if ($result !== false) {
$this->success("添加成功!",url('trainer'));
} else {
$this->error("添加失败!");
}
} //添加
public function coach_edit()
{
$id = $this->request->param('id', 0, 'intval');
$info = DB::name('user')->where(['id' => $id])->find();
$this->assign('info', $info);
return $this->fetch();
}
public function coach_edit_do()
{
$p = $this->request->param();
$datain['user_nickname'] = $p['user_nickname'];
$datain['user_type'] = 3;
$datain['create_time'] = time();
$datain['mobile'] = $p['mobile'];
$datain['id'] = $p['id'];
$datain['headimgurl'] = $p['headimgurl'];
$datain['sex'] = $p['sex'];
$datain['idnumber'] = $p['idnumber'];
$datain['birthday'] = $p['birthday'];
$datain['more'] = $p['more'];
$garbage = strstr($datain['headimgurl'], "upload");
if (!$garbage) {
$datain['headimgurl'] = '/upload/' . $datain['headimgurl'];
}
$result = DB::name('user')->update($datain);
if ($result !== false) {
$this->success("保存成功",url('trainer'));
} else {
$this->error("保存失败!");
}
}
public function coach_delete()
{
$id = $this->request->param('id', 0, 'intval');
$result = DB::name('user')->where(['id' => $id])->delete();
if ($result !== false) {
$this->success("删除成功!");
} else {
$this->error("删除失败!");
}
}
/**
* 管理员添加提交
* @adminMenu(
* 'name' => '管理员添加提交',
* 'parent' => 'index',
* 'display'=> false,
* 'hasView'=> false,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => '管理员添加提交',
* 'param' => ''
* )
*/
public function addPost()
{
if ($this->request->isPost()) {
if (!empty($_POST['role_id']) && is_array($_POST['role_id'])) {
$role_ids = $_POST['role_id'];
unset($_POST['role_id']);
$result = $this->validate($this->request->param(), 'User');
if ($result !== true) {
$this->error($result);
} else {
$_POST['user_pass'] = cmf_password($_POST['user_pass']);
$result = DB::name('user')->insertGetId($_POST);
if ($result !== false) {
//$role_user_model=M("RoleUser");
foreach ($role_ids as $role_id) {
if (cmf_get_current_admin_id() != 1 && $role_id == 1) {
$this->error("为了网站的安全,非网站创建者不可创建超级管理员!");
}
Db::name('RoleUser')->insert(["role_id" => $role_id, "user_id" => $result]);
}
$this->success("添加成功!", url("user/index"));
} else {
$this->error("添加失败!");
}
}
} else {
$this->error("请为此用户指定角色!");
}
}
}
/**
* 管理员编辑
* @adminMenu(
* 'name' => '管理员编辑',
* 'parent' => 'index',
* 'display'=> false,
* 'hasView'=> true,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => '管理员编辑',
* 'param' => ''
* )
*/
public function edit()
{
$id = $this->request->param('id', 0, 'intval');
$roles = DB::name('role')->where(['status' => 1])->order("id DESC")->select();
$this->assign("roles", $roles);
$role_ids = DB::name('RoleUser')->where(["user_id" => $id])->column("role_id");
$this->assign("role_ids", $role_ids);
$user = DB::name('user')->where(["id" => $id])->find();
$this->assign($user);
return $this->fetch();
}
/**
* 管理员编辑提交
* @adminMenu(
* 'name' => '管理员编辑提交',
* 'parent' => 'index',
* 'display'=> false,
* 'hasView'=> false,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => '管理员编辑提交',
* 'param' => ''
* )
*/
public function editPost()
{
if ($this->request->isPost()) {
if (!empty($_POST['role_id']) && is_array($_POST['role_id'])) {
if (empty($_POST['user_pass'])) {
unset($_POST['user_pass']);
} else {
$_POST['user_pass'] = cmf_password($_POST['user_pass']);
}
$role_ids = $this->request->param('role_id/a');
unset($_POST['role_id']);
$result = $this->validate($this->request->param(), 'User.edit');
if ($result !== true) {
// 验证失败 输出错误信息
$this->error($result);
} else {
$result = DB::name('user')->update($_POST);
if ($result !== false) {
$uid = $this->request->param('id', 0, 'intval');
DB::name("RoleUser")->where(["user_id" => $uid])->delete();
foreach ($role_ids as $role_id) {
if (cmf_get_current_admin_id() != 1 && $role_id == 1) {
$this->error("为了网站的安全,非网站创建者不可创建超级管理员!");
}
DB::name("RoleUser")->insert(["role_id" => $role_id, "user_id" => $uid]);
}
$this->success("保存成功!");
} else {
$this->error("保存失败!");
}
}
} else {
$this->error("请为此用户指定角色!");
}
}
}
/**
* 管理员个人信息修改
* @adminMenu(
* 'name' => '个人信息',
* 'parent' => 'index',
* 'display'=> false,
* 'hasView'=> true,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => '管理员个人信息修改',
* 'param' => ''
* )
*/
public function userInfo()
{
$id = cmf_get_current_admin_id();
$user = Db::name('user')->where(["id" => $id])->find();
$this->assign($user);
return $this->fetch();
}
/**
* 管理员个人信息修改提交
* @adminMenu(
* 'name' => '管理员个人信息修改提交',
* 'parent' => 'index',
* 'display'=> false,
* 'hasView'=> false,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => '管理员个人信息修改提交',
* 'param' => ''
* )
*/
public function userInfoPost()
{
if ($this->request->isPost()) {
$data = $this->request->post();
$data['birthday'] = strtotime($data['birthday']);
$data['id'] = cmf_get_current_admin_id();
$create_result = Db::name('user')->update($data);;
if ($create_result !== false) {
$this->success("保存成功!");
} else {
$this->error("保存失败!");
}
}
}
/**
* 管理员删除
*/
public function delete()
{
$id = $this->request->param('id', 0, 'intval');
if ($id == 1) {
$this->error("最高管理员不能删除!");
}
if (Db::name('user')->delete($id) !== false) {
Db::name("RoleUser")->where(["user_id" => $id])->delete();
$this->success("删除成功!");
} else {
$this->error("删除失败!");
}
}
/**
* 停用管理员
*/
public function ban()
{
$id = $this->request->param('id', 0, 'intval');
if (!empty($id)) {
$result = Db::name('user')->where(["id" => $id])->setField('user_status', '0');
if ($result !== false) {
$this->success("停用成功!");
} else {
$this->error('停用失败!');
}
} else {
$this->error('数据传入失败!');
}
}
/**
* 启用管理员
*/
public function cancelBan()
{
$id = $this->request->param('id', 0, 'intval');
if (!empty($id)) {
$result = Db::name('user')->where(["id" => $id])->setField('user_status', '1');
if ($result !== false) {
$this->success("启用成功!");
} else {
$this->error('启用失败!');
}
} else {
$this->error('数据传入失败!');
}
}
}