Rule.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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->order('weigh', 'desc')->order('id', 'asc')->select())->toArray();
  30. foreach ($ruleList as $k => &$v) {
  31. $v['title'] = __($v['title']);
  32. $v['remark'] = __($v['remark']);
  33. }
  34. unset($v);
  35. Tree::instance()->init($ruleList);
  36. $this->rulelist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
  37. $ruledata = [0 => __('None')];
  38. foreach ($this->rulelist as $k => &$v) {
  39. if (!$v['ismenu']) {
  40. continue;
  41. }
  42. $ruledata[$v['id']] = $v['title'];
  43. }
  44. unset($v);
  45. $this->view->assign('ruledata', $ruledata);
  46. }
  47. /**
  48. * 查看
  49. */
  50. public function index()
  51. {
  52. if ($this->request->isAjax()) {
  53. $list = $this->rulelist;
  54. $total = count($this->rulelist);
  55. $result = array("total" => $total, "rows" => $list);
  56. return json($result);
  57. }
  58. return $this->view->fetch();
  59. }
  60. /**
  61. * 添加
  62. */
  63. public function add()
  64. {
  65. if ($this->request->isPost()) {
  66. $this->token();
  67. $params = $this->request->post("row/a", [], 'strip_tags');
  68. if ($params) {
  69. if (!$params['ismenu'] && !$params['pid']) {
  70. $this->error(__('The non-menu rule must have parent'));
  71. }
  72. $result = $this->model->validate()->save($params);
  73. if ($result === false) {
  74. $this->error($this->model->getError());
  75. }
  76. Cache::rm('__menu__');
  77. $this->success();
  78. }
  79. $this->error();
  80. }
  81. return $this->view->fetch();
  82. }
  83. /**
  84. * 编辑
  85. */
  86. public function edit($ids = null)
  87. {
  88. $row = $this->model->get(['id' => $ids]);
  89. if (!$row) {
  90. $this->error(__('No Results were found'));
  91. }
  92. if ($this->request->isPost()) {
  93. $this->token();
  94. $params = $this->request->post("row/a", [], 'strip_tags');
  95. if ($params) {
  96. if (!$params['ismenu'] && !$params['pid']) {
  97. $this->error(__('The non-menu rule must have parent'));
  98. }
  99. if ($params['pid'] != $row['pid']) {
  100. $childrenIds = Tree::instance()->init(collection(AuthRule::select())->toArray())->getChildrenIds($row['id']);
  101. if (in_array($params['pid'], $childrenIds)) {
  102. $this->error(__('Can not change the parent to child'));
  103. }
  104. }
  105. //这里需要针对name做唯一验证
  106. $ruleValidate = \think\Loader::validate('AuthRule');
  107. $ruleValidate->rule([
  108. 'name' => 'require|format|unique:AuthRule,name,' . $row->id,
  109. ]);
  110. $result = $row->validate()->save($params);
  111. if ($result === false) {
  112. $this->error($row->getError());
  113. }
  114. Cache::rm('__menu__');
  115. $this->success();
  116. }
  117. $this->error();
  118. }
  119. $this->view->assign("row", $row);
  120. return $this->view->fetch();
  121. }
  122. /**
  123. * 删除
  124. */
  125. public function del($ids = "")
  126. {
  127. if ($ids) {
  128. $delIds = [];
  129. foreach (explode(',', $ids) as $k => $v) {
  130. $delIds = array_merge($delIds, Tree::instance()->getChildrenIds($v, true));
  131. }
  132. $delIds = array_unique($delIds);
  133. $count = $this->model->where('id', 'in', $delIds)->delete();
  134. if ($count) {
  135. Cache::rm('__menu__');
  136. $this->success();
  137. }
  138. }
  139. $this->error();
  140. }
  141. }