Rule.php 5.0 KB

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