Rule.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace app\admin\controller\auth;
  3. use app\admin\model\AuthRule;
  4. use app\common\controller\Backend;
  5. use fast\Tree;
  6. use think\Cache;
  7. /**
  8. * 规则管理
  9. *
  10. * @icon fa fa-list
  11. * @remark 规则通常对应一个控制器的方法,同时左侧的菜单栏数据也从规则中体现,通常建议通过控制台进行生成规则节点
  12. */
  13. class Rule extends Backend
  14. {
  15. /**
  16. * @var \app\admin\model\AuthRule
  17. */
  18. protected $model = null;
  19. protected $rulelist = [];
  20. protected $multiFields = 'ismenu,status';
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. if (!$this->auth->isSuperAdmin()) {
  25. $this->error(__('Access is allowed only to the super management group'));
  26. }
  27. $this->model = model('AuthRule');
  28. // 必须将结果集转换为数组
  29. $ruleList = collection($this->model->field('condition,remark,createtime,updatetime', true)->order('weigh DESC,id ASC')->select())->toArray();
  30. foreach ($ruleList as $k => &$v) {
  31. $v['title'] = __($v['title']);
  32. }
  33. unset($v);
  34. Tree::instance()->init($ruleList);
  35. $this->rulelist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
  36. $ruledata = [0 => __('None')];
  37. foreach ($this->rulelist as $k => &$v) {
  38. if (!$v['ismenu']) {
  39. continue;
  40. }
  41. $ruledata[$v['id']] = $v['title'];
  42. }
  43. unset($v);
  44. $this->view->assign('ruledata', $ruledata);
  45. }
  46. /**
  47. * 查看
  48. */
  49. public function index()
  50. {
  51. if ($this->request->isAjax()) {
  52. $list = $this->rulelist;
  53. $total = count($this->rulelist);
  54. $result = array("total" => $total, "rows" => $list);
  55. return json($result);
  56. }
  57. return $this->view->fetch();
  58. }
  59. /**
  60. * 添加
  61. */
  62. public function add()
  63. {
  64. if ($this->request->isPost()) {
  65. $this->token();
  66. $params = $this->request->post("row/a", [], 'strip_tags');
  67. if ($params) {
  68. if (!$params['ismenu'] && !$params['pid']) {
  69. $this->error(__('The non-menu rule must have parent'));
  70. }
  71. $result = $this->model->validate()->save($params);
  72. if ($result === false) {
  73. $this->error($this->model->getError());
  74. }
  75. Cache::rm('__menu__');
  76. $this->success();
  77. }
  78. $this->error();
  79. }
  80. return $this->view->fetch();
  81. }
  82. /**
  83. * 编辑
  84. */
  85. public function edit($ids = null)
  86. {
  87. $row = $this->model->get(['id' => $ids]);
  88. if (!$row) {
  89. $this->error(__('No Results were found'));
  90. }
  91. if ($this->request->isPost()) {
  92. $this->token();
  93. $params = $this->request->post("row/a", [], 'strip_tags');
  94. if ($params) {
  95. if (!$params['ismenu'] && !$params['pid']) {
  96. $this->error(__('The non-menu rule must have parent'));
  97. }
  98. if ($params['pid'] != $row['pid']) {
  99. $childrenIds = Tree::instance()->init(collection(AuthRule::select())->toArray())->getChildrenIds($row['id']);
  100. if (in_array($params['pid'], $childrenIds)) {
  101. $this->error(__('Can not change the parent to child'));
  102. }
  103. }
  104. //这里需要针对name做唯一验证
  105. $ruleValidate = \think\Loader::validate('AuthRule');
  106. $ruleValidate->rule([
  107. 'name' => 'require|format|unique:AuthRule,name,' . $row->id,
  108. ]);
  109. $result = $row->validate()->save($params);
  110. if ($result === false) {
  111. $this->error($row->getError());
  112. }
  113. Cache::rm('__menu__');
  114. $this->success();
  115. }
  116. $this->error();
  117. }
  118. $this->view->assign("row", $row);
  119. return $this->view->fetch();
  120. }
  121. /**
  122. * 删除
  123. */
  124. public function del($ids = "")
  125. {
  126. if ($ids) {
  127. $delIds = [];
  128. foreach (explode(',', $ids) as $k => $v) {
  129. $delIds = array_merge($delIds, Tree::instance()->getChildrenIds($v, true));
  130. }
  131. $delIds = array_unique($delIds);
  132. $count = $this->model->where('id', 'in', $delIds)->delete();
  133. if ($count) {
  134. Cache::rm('__menu__');
  135. $this->success();
  136. }
  137. }
  138. $this->error();
  139. }
  140. }