Rule.php 5.3 KB

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