Rule.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace app\admin\controller\auth;
  3. use app\common\controller\Backend;
  4. use fast\Tree;
  5. use think\Cache;
  6. /**
  7. * 规则管理
  8. *
  9. * @icon fa fa-list
  10. * @remark 规则通常对应一个控制器的方法,同时左侧的菜单栏数据也从规则中体现,通常建议通过控制台进行生成规则节点
  11. */
  12. class Rule extends Backend
  13. {
  14. protected $model = null;
  15. protected $rulelist = [];
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. $this->model = model('AuthRule');
  20. // 必须将结果集转换为数组
  21. Tree::instance()->init(collection($this->model->order('weigh', 'desc')->select())->toArray());
  22. $this->rulelist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
  23. $ruledata = [0 => __('None')];
  24. foreach ($this->rulelist as $k => $v)
  25. {
  26. if (!$v['ismenu'])
  27. continue;
  28. $ruledata[$v['id']] = $v['title'];
  29. }
  30. $this->view->assign('ruledata', $ruledata);
  31. }
  32. /**
  33. * 查看
  34. */
  35. public function index()
  36. {
  37. if ($this->request->isAjax())
  38. {
  39. $list = $this->rulelist;
  40. $total = count($this->rulelist);
  41. $result = array("total" => $total, "rows" => $list);
  42. return json($result);
  43. }
  44. return $this->view->fetch();
  45. }
  46. /**
  47. * 添加
  48. */
  49. public function add()
  50. {
  51. if ($this->request->isPost())
  52. {
  53. $this->code = -1;
  54. $params = $this->request->post("row/a");
  55. if ($params)
  56. {
  57. if (!$params['ismenu'] && !$params['pid'])
  58. {
  59. $this->msg = __('The non-menu rule must have parent');
  60. return;
  61. }
  62. $this->model->create($params);
  63. Cache::rm('__menu__');
  64. $this->code = 1;
  65. }
  66. return;
  67. }
  68. return $this->view->fetch();
  69. }
  70. /**
  71. * 编辑
  72. */
  73. public function edit($ids = NULL)
  74. {
  75. $row = $this->model->get(['id' => $ids]);
  76. if (!$row)
  77. $this->error(__('No Results were found'));
  78. if ($this->request->isPost())
  79. {
  80. $this->code = -1;
  81. $params = $this->request->post("row/a");
  82. if ($params)
  83. {
  84. if (!$params['ismenu'] && !$params['pid'])
  85. {
  86. $this->msg = __('The non-menu rule must have parent');
  87. return;
  88. }
  89. $row->save($params);
  90. Cache::rm('__menu__');
  91. $this->code = 1;
  92. }
  93. return;
  94. }
  95. $this->view->assign("row", $row);
  96. return $this->view->fetch();
  97. }
  98. /**
  99. * 删除
  100. */
  101. public function del($ids = "")
  102. {
  103. $this->code = -1;
  104. if ($ids)
  105. {
  106. $delIds = [];
  107. foreach (explode(',', $ids) as $k => $v)
  108. {
  109. $delIds = array_merge($delIds, Tree::instance()->getChildrenIds($v, TRUE));
  110. }
  111. $delIds = array_unique($delIds);
  112. $count = $this->model->where('id', 'in', $delIds)->delete();
  113. if ($count)
  114. {
  115. Cache::rm('__menu__');
  116. $this->code = 1;
  117. }
  118. }
  119. return;
  120. }
  121. /**
  122. * 批量更新
  123. * @internal
  124. */
  125. public function multi($ids = "")
  126. {
  127. // 节点禁止批量操作
  128. $this->code = -1;
  129. }
  130. }