Rule.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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)->icon = ['&nbsp;&nbsp;&nbsp;&nbsp;', '&nbsp;&nbsp;&nbsp;&nbsp;', '&nbsp;&nbsp;&nbsp;&nbsp;'];
  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. $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['id']) {
  100. $this->error(__('Can not change the parent to self'));
  101. }
  102. if ($params['pid'] != $row['pid']) {
  103. $childrenIds = Tree::instance()->init(collection(AuthRule::select())->toArray())->getChildrenIds($row['id']);
  104. if (in_array($params['pid'], $childrenIds)) {
  105. $this->error(__('Can not change the parent to child'));
  106. }
  107. }
  108. //这里需要针对name做唯一验证
  109. $ruleValidate = \think\Loader::validate('AuthRule');
  110. $ruleValidate->rule([
  111. 'name' => 'require|unique:AuthRule,name,' . $row->id,
  112. ]);
  113. $result = $row->validate()->save($params);
  114. if ($result === false) {
  115. $this->error($row->getError());
  116. }
  117. $this->success();
  118. }
  119. $this->error();
  120. }
  121. $this->view->assign("row", $row);
  122. return $this->view->fetch();
  123. }
  124. /**
  125. * 删除
  126. */
  127. public function del($ids = "")
  128. {
  129. if (!$this->request->isPost()) {
  130. $this->error(__("Invalid parameters"));
  131. }
  132. $ids = $ids ? $ids : $this->request->post("ids");
  133. if ($ids) {
  134. $delIds = [];
  135. foreach (explode(',', $ids) as $k => $v) {
  136. $delIds = array_merge($delIds, Tree::instance()->getChildrenIds($v, true));
  137. }
  138. $delIds = array_unique($delIds);
  139. $count = $this->model->where('id', 'in', $delIds)->delete();
  140. if ($count) {
  141. Cache::rm('__menu__');
  142. $this->success();
  143. }
  144. }
  145. $this->error();
  146. }
  147. }