MenuController.php
31.0 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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use app\admin\model\AdminMenuModel;
use cmf\controller\AdminBaseController;
use think\Db;
use tree\Tree;
use mindplay\annotations\Annotations;
class MenuController extends AdminBaseController
{
/**
* 后台菜单管理
* @adminMenu(
* 'name' => '后台菜单',
* 'parent' => 'admin/Setting/default',
* 'display'=> false,
* 'hasView'=> true,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => '后台菜单管理',
* 'param' => ''
* )
*/
public function index()
{
session('admin_menu_index', 'Menu/index');
$result = Db::name('AdminMenu')->order(["list_order" => "ASC"])->select()->toArray();
$tree = new Tree();
$tree->icon = [' │ ', ' ├─ ', ' └─ '];
$tree->nbsp = ' ';
$newMenus = [];
foreach ($result as $m) {
$newMenus[$m['id']] = $m;
}
foreach ($result as $key => $value) {
$result[$key]['parent_id_node'] = ($value['parent_id']) ? ' class="child-of-node-' . $value['parent_id'] . '"' : '';
$result[$key]['style'] = empty($value['parent_id']) ? '' : 'display:none;';
$result[$key]['str_manage'] = '<a href="' . url("Menu/add", ["parent_id" => $value['id'], "menu_id" => $this->request->param("menu_id")])
. '">' . lang('ADD_SUB_MENU') . '</a> <a href="' . url("Menu/edit", ["id" => $value['id'], "menu_id" => $this->request->param("menu_id")])
. '">' . lang('EDIT') . '</a> <a class="js-ajax-delete" href="' . url("Menu/delete", ["id" => $value['id'], "menu_id" => $this->request->param("menu_id")]) . '">' . lang('DELETE') . '</a> ';
$result[$key]['status'] = $value['status'] ? lang('DISPLAY') : lang('HIDDEN');
if (APP_DEBUG) {
$result[$key]['app'] = $value['app'] . "/" . $value['controller'] . "/" . $value['action'];
}
}
$tree->init($result);
$str = "<tr id='node-\$id' \$parent_id_node style='\$style'>
<td style='padding-left:20px;'><input name='list_orders[\$id]' type='text' size='3' value='\$list_order' class='input input-order'></td>
<td>\$id</td>
<td>\$spacer\$name</td>
<td>\$app</td>
<td>\$status</td>
<td>\$str_manage</td>
</tr>";
$category = $tree->getTree(0, $str);
$this->assign("category", $category);
return $this->fetch();
}
/**
* 后台所有菜单列表
* @adminMenu(
* 'name' => '所有菜单',
* 'parent' => 'index',
* 'display'=> false,
* 'hasView'=> true,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => '后台所有菜单列表',
* 'param' => ''
* )
*/
public function lists()
{
session('admin_menu_index', 'Menu/lists');
$result = Db::name('AdminMenu')->order(["app" => "ASC", "controller" => "ASC", "action" => "ASC"])->select();
$this->assign("menus", $result);
return $this->fetch();
}
/**
* 后台菜单添加
* @adminMenu(
* 'name' => '后台菜单添加',
* 'parent' => 'index',
* 'display'=> false,
* 'hasView'=> true,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => '后台菜单添加',
* 'param' => ''
* )
*/
public function add()
{
$tree = new Tree();
$parentId = $this->request->param("parent_id", 0, 'intval');
$result = Db::name('AdminMenu')->order(["list_order" => "ASC"])->select();
$array = [];
foreach ($result as $r) {
$r['selected'] = $r['id'] == $parentId ? 'selected' : '';
$array[] = $r;
}
$str = "<option value='\$id' \$selected>\$spacer \$name</option>";
$tree->init($array);
$selectCategory = $tree->getTree(0, $str);
$this->assign("select_category", $selectCategory);
return $this->fetch();
}
/**
* 后台菜单添加提交保存
* @adminMenu(
* 'name' => '后台菜单添加提交保存',
* 'parent' => 'index',
* 'display'=> false,
* 'hasView'=> false,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => '后台菜单添加提交保存',
* 'param' => ''
* )
*/
public function addPost()
{
if ($this->request->isPost()) {
$result = $this->validate($this->request->param(), 'AdminMenu');
if ($result !== true) {
$this->error($result);
} else {
$data = $this->request->param();
Db::name('AdminMenu')->strict(false)->field(true)->insert($data);
$app = $this->request->param("app");
$controller = $this->request->param("controller");
$action = $this->request->param("action");
$param = $this->request->param("param");
$authRuleName = "$app/$controller/$action";
$menuName = $this->request->param("name");
$findAuthRuleCount = Db::name('auth_rule')->where([
'app' => $app,
'name' => $authRuleName,
'type' => 'admin_url'
])->count();
if (empty($findAuthRuleCount)) {
Db::name('AuthRule')->insert([
"name" => $authRuleName,
"app" => $app,
"type" => "admin_url", //type 1-admin rule;2-user rule
"title" => $menuName,
'param' => $param,
]);
}
$sessionAdminMenuIndex = session('admin_menu_index');
$to = empty($sessionAdminMenuIndex) ? "Menu/index" : $sessionAdminMenuIndex;
$this->_exportAppMenuDefaultLang();
$this->success("添加成功!", url($to));
}
}
}
/**
* 后台菜单编辑
* @adminMenu(
* 'name' => '后台菜单编辑',
* 'parent' => 'index',
* 'display'=> false,
* 'hasView'=> true,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => '后台菜单编辑',
* 'param' => ''
* )
*/
public function edit()
{
$tree = new Tree();
$id = $this->request->param("id", 0, 'intval');
$rs = Db::name('AdminMenu')->where(["id" => $id])->find();
$result = Db::name('AdminMenu')->order(["list_order" => "ASC"])->select();
$array = [];
foreach ($result as $r) {
$r['selected'] = $r['id'] == $rs['parent_id'] ? 'selected' : '';
$array[] = $r;
}
$str = "<option value='\$id' \$selected>\$spacer \$name</option>";
$tree->init($array);
$selectCategory = $tree->getTree(0, $str);
$this->assign("data", $rs);
$this->assign("select_category", $selectCategory);
return $this->fetch();
}
/**
* 后台菜单编辑提交保存
* @adminMenu(
* 'name' => '后台菜单编辑提交保存',
* 'parent' => 'index',
* 'display'=> false,
* 'hasView'=> false,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => '后台菜单编辑提交保存',
* 'param' => ''
* )
*/
public function editPost()
{
if ($this->request->isPost()) {
$id = $this->request->param('id', 0, 'intval');
$oldMenu = Db::name('AdminMenu')->where(['id' => $id])->find();
$result = $this->validate($this->request->param(), 'AdminMenu.edit');
if ($result !== true) {
$this->error($result);
} else {
Db::name('AdminMenu')->strict(false)->field(true)->update($this->request->param());
$app = $this->request->param("app");
$controller = $this->request->param("controller");
$action = $this->request->param("action");
$param = $this->request->param("param");
$authRuleName = "$app/$controller/$action";
$menuName = $this->request->param("name");
$findAuthRuleCount = Db::name('auth_rule')->where([
'app' => $app,
'name' => $authRuleName,
'type' => 'admin_url'
])->count();
if (empty($findAuthRuleCount)) {
$oldApp = $oldMenu['app'];
$oldController = $oldMenu['controller'];
$oldAction = $oldMenu['action'];
$oldName = "$oldApp/$oldController/$oldAction";
$findOldRuleId = Db::name('AuthRule')->where(["name" => $oldName])->value('id');
if (empty($findOldRuleId)) {
Db::name('AuthRule')->insert([
"name" => $authRuleName,
"app" => $app,
"type" => "admin_url",
"title" => $menuName,
"param" => $param
]);//type 1-admin rule;2-user rule
} else {
Db::name('AuthRule')->where(['id' => $findOldRuleId])->update([
"name" => $authRuleName,
"app" => $app,
"type" => "admin_url",
"title" => $menuName,
"param" => $param]);//type 1-admin rule;2-user rule
}
} else {
Db::name('AuthRule')->where([
'app' => $app,
'name' => $authRuleName,
'type' => 'admin_url'
])->update(["title" => $menuName, 'param' => $param]);//type 1-admin rule;2-user rule
}
$this->_exportAppMenuDefaultLang();
$this->success("保存成功!");
}
}
}
/**
* 后台菜单删除
* @adminMenu(
* 'name' => '后台菜单删除',
* 'parent' => 'index',
* 'display'=> false,
* 'hasView'=> false,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => '后台菜单删除',
* 'param' => ''
* )
*/
public function delete()
{
$id = $this->request->param("id", 0, 'intval');
$count = Db::name('AdminMenu')->where(["parent_id" => $id])->count();
if ($count > 0) {
$this->error("该菜单下还有子菜单,无法删除!");
}
if (Db::name('AdminMenu')->delete($id) !== false) {
$this->success("删除菜单成功!");
} else {
$this->error("删除失败!");
}
}
/**
* 后台菜单排序
* @adminMenu(
* 'name' => '后台菜单排序',
* 'parent' => 'index',
* 'display'=> false,
* 'hasView'=> false,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => '后台菜单排序',
* 'param' => ''
* )
*/
public function listOrder()
{
$adminMenuModel = new AdminMenuModel();
parent::listOrders($adminMenuModel);
$this->success("排序更新成功!");
}
/**
* 导入新后台菜单
* @adminMenu(
* 'name' => '导入新后台菜单',
* 'parent' => 'index',
* 'display'=> false,
* 'hasView'=> true,
* 'order' => 10000,
* 'icon' => '',
* 'remark' => '导入新后台菜单',
* 'param' => ''
* )
*/
public function getActions()
{
Annotations::$config['cache'] = false;
$annotationManager = Annotations::getManager();
$annotationManager->registry['adminMenu'] = 'app\admin\annotation\AdminMenuAnnotation';
$annotationManager->registry['adminMenuRoot'] = 'app\admin\annotation\AdminMenuRootAnnotation';
$newMenus = [];
$apps = cmf_scan_dir(APP_PATH . '*', GLOB_ONLYDIR);
$app = $this->request->param('app', '');
if (empty($app)) {
$app = $apps[0];
}
if (!in_array($app, $apps)) {
$this->error('应用' . $app . '不存在!');
}
if ($app == 'admin') {
$filePatten = APP_PATH . $app . '/controller/*Controller.php';
} else {
$filePatten = APP_PATH . $app . '/controller/Admin*Controller.php';
}
$controllers = cmf_scan_dir($filePatten);
if (!empty($controllers)) {
foreach ($controllers as $controller) {
$controller = preg_replace('/\.php$/', '', $controller);
$controllerName = preg_replace('/\Controller$/', '', $controller);
$controllerClass = "app\\$app\\controller\\$controller";
$menuAnnotations = Annotations::ofClass($controllerClass, '@adminMenuRoot');
if (!empty($menuAnnotations)) {
foreach ($menuAnnotations as $menuAnnotation) {
$name = $menuAnnotation->name;
$icon = $menuAnnotation->icon;
$type = 0;//1:有界面可访问菜单,2:无界面可访问菜单,0:只作为菜单
$action = $menuAnnotation->action;
$status = empty($menuAnnotation->display) ? 0 : 1;
$listOrder = floatval($menuAnnotation->order);
$param = $menuAnnotation->param;
$remark = $menuAnnotation->remark;
if (empty($menuAnnotation->parent)) {
$parentId = 0;
} else {
$parent = explode('/', $menuAnnotation->parent);
$countParent = count($parent);
if ($countParent > 3) {
throw new \Exception($controllerClass . ':' . $action . ' @adminMenuRoot parent格式不正确!');
}
$parentApp = $app;
$parentController = $controllerName;
$parentAction = '';
switch ($countParent) {
case 1:
$parentAction = $parent[0];
break;
case 2:
$parentController = $parent[0];
$parentAction = $parent[1];
break;
case 3:
$parentApp = $parent[0];
$parentController = $parent[1];
$parentAction = $parent[2];
break;
}
$findParentAdminMenu = Db::name('admin_menu')->where([
'app' => $parentApp,
'controller' => $parentController,
'action' => $parentAction
])->find();
if (empty($findParentAdminMenu)) {
$parentId = Db::name('admin_menu')->insertGetId([
'app' => $parentApp,
'controller' => $parentController,
'action' => $parentAction,
'name' => '--new--'
]);
} else {
$parentId = $findParentAdminMenu['id'];
}
}
$findAdminMenu = Db::name('admin_menu')->where([
'app' => $app,
'controller' => $controllerName,
'action' => $action
])->find();
if (empty($findAdminMenu)) {
Db::name('admin_menu')->insert([
'parent_id' => $parentId,
'type' => $type,
'status' => $status,
'list_order' => $listOrder,
'app' => $app,
'controller' => $controllerName,
'action' => $action,
'param' => $param,
'name' => $name,
'icon' => $icon,
'remark' => $remark
]);
$menuName = $name;
array_push($newMenus, "$app/$controllerName/$action 已导入");
} else {
if ($findAdminMenu['name'] == '--new--') {
Db::name('admin_menu')->where([
'app' => $app,
'controller' => $controllerName,
'action' => $action
])->update([
'parent_id' => $parentId,
'type' => $type,
'status' => $status,
'list_order' => $listOrder,
'param' => $param,
'name' => $name,
'icon' => $icon,
'remark' => $remark
]);
$menuName = $name;
} else {
// 只关注菜单层级关系,是否有视图
Db::name('admin_menu')->where([
'app' => $app,
'controller' => $controllerName,
'action' => $action
])->update([
'parent_id' => $parentId,
'type' => $type,
]);
$menuName = $findAdminMenu['name'];
}
array_push($newMenus, "$app/$controllerName/$action 层级关系已更新");
}
$authRuleName = "{$app}/{$controllerName}/{$action}";
$findAuthRuleCount = Db::name('auth_rule')->where([
'app' => $app,
'name' => $authRuleName,
'type' => 'admin_url'
])->count();
if ($findAuthRuleCount == 0) {
Db::name('auth_rule')->insert([
'app' => $app,
'name' => $authRuleName,
'type' => 'admin_url',
'param' => $param,
'title' => $menuName
]);
} else {
Db::name('auth_rule')->where([
'app' => $app,
'name' => $authRuleName,
'type' => 'admin_url',
])->update([
'param' => $param,
'title' => $menuName
]);
}
}
}
$reflect = new \ReflectionClass($controllerClass);
$methods = $reflect->getMethods(\ReflectionMethod::IS_PUBLIC);
if (!empty($methods)) {
foreach ($methods as $method) {
if ($method->class == $controllerClass && strpos($method->name, '_') !== 0) {
$menuAnnotations = Annotations::ofMethod($controllerClass, $method->name, '@adminMenu');
if (!empty($menuAnnotations)) {
$menuAnnotation = $menuAnnotations[0];
$name = $menuAnnotation->name;
$icon = $menuAnnotation->icon;
$type = $menuAnnotation->hasView ? 1 : 2;//1:有界面可访问菜单,2:无界面可访问菜单,0:只作为菜单
$action = $method->name;
$status = empty($menuAnnotation->display) ? 0 : 1;
$listOrder = floatval($menuAnnotation->order);
$param = $menuAnnotation->param;
$remark = $menuAnnotation->remark;
if (empty($menuAnnotation->parent)) {
$parentId = 0;
} else {
$parent = explode('/', $menuAnnotation->parent);
$countParent = count($parent);
if ($countParent > 3) {
throw new \Exception($controllerClass . ':' . $action . ' @menuRoot parent格式不正确!');
}
$parentApp = $app;
$parentController = $controllerName;
$parentAction = '';
switch ($countParent) {
case 1:
$parentAction = $parent[0];
break;
case 2:
$parentController = $parent[0];
$parentAction = $parent[1];
break;
case 3:
$parentApp = $parent[0];
$parentController = $parent[1];
$parentAction = $parent[2];
break;
}
$findParentAdminMenu = Db::name('admin_menu')->where([
'app' => $parentApp,
'controller' => $parentController,
'action' => $parentAction
])->find();
if (empty($findParentAdminMenu)) {
$parentId = Db::name('admin_menu')->insertGetId([
'app' => $parentApp,
'controller' => $parentController,
'action' => $parentAction,
'name' => '--new--'
]);
} else {
$parentId = $findParentAdminMenu['id'];
}
}
$findAdminMenu = Db::name('admin_menu')->where([
'app' => $app,
'controller' => $controllerName,
'action' => $action
])->find();
if (empty($findAdminMenu)) {
Db::name('admin_menu')->insert([
'parent_id' => $parentId,
'type' => $type,
'status' => $status,
'list_order' => $listOrder,
'app' => $app,
'controller' => $controllerName,
'action' => $action,
'param' => $param,
'name' => $name,
'icon' => $icon,
'remark' => $remark
]);
$menuName = $name;
array_push($newMenus, "$app/$controllerName/$action 已导入");
} else {
if ($findAdminMenu['name'] == '--new--') {
Db::name('admin_menu')->where([
'app' => $app,
'controller' => $controllerName,
'action' => $action
])->update([
'parent_id' => $parentId,
'type' => $type,
'status' => $status,
'list_order' => $listOrder,
'param' => $param,
'name' => $name,
'icon' => $icon,
'remark' => $remark
]);
$menuName = $name;
} else {
// 只关注菜单层级关系,是否有视图
Db::name('admin_menu')->where([
'app' => $app,
'controller' => $controllerName,
'action' => $action
])->update([
'parent_id' => $parentId,
'type' => $type,
]);
$menuName = $findAdminMenu['name'];
}
array_push($newMenus, "$app/$controllerName/$action 已更新");
}
$authRuleName = "{$app}/{$controllerName}/{$action}";
$findAuthRuleCount = Db::name('auth_rule')->where([
'app' => $app,
'name' => $authRuleName,
'type' => 'admin_url'
])->count();
if ($findAuthRuleCount == 0) {
Db::name('auth_rule')->insert([
'app' => $app,
'name' => $authRuleName,
'type' => 'admin_url',
'param' => $param,
'title' => $menuName
]);
} else {
Db::name('auth_rule')->where([
'app' => $app,
'name' => $authRuleName,
'type' => 'admin_url',
])->update([
'param' => $param,
'title' => $menuName
]);
}
}
}
}
}
}
}
$index = array_search($app, $apps);
$nextIndex = $index + 1;
$nextIndex = $nextIndex >= count($apps) ? 0 : $nextIndex;
if ($nextIndex) {
$this->assign("next_app", $apps[$nextIndex]);
}
$this->assign("app", $app);
$this->assign("new_menus", $newMenus);
return $this->fetch();
}
/**
* 导出后台菜单语言包
*/
private function _exportAppMenuDefaultLang()
{
$menus = Db::name('AdminMenu')->order(["app" => "ASC", "controller" => "ASC", "action" => "ASC"])->select();
$langDir = config('DEFAULT_LANG');
$adminMenuLang = CMF_ROOT . "data/lang/" . $langDir . "/admin_menu.php";
if (!empty($adminMenuLang) && !file_exists_case($adminMenuLang)) {
mkdir(dirname($adminMenuLang), 0777, true);
}
$lang = [];
foreach ($menus as $menu) {
$lang_key = strtoupper($menu['app'] . '_' . $menu['controller'] . '_' . $menu['action']);
$lang[$lang_key] = $menu['name'];
}
$langStr = var_export($lang, true);
$langStr = preg_replace("/\s+\d+\s=>\s(\n|\r)/", "\n", $langStr);
if (!empty($adminMenuLang)) {
file_put_contents($adminMenuLang, "<?php\nreturn $langStr;");
}
}
}