Rule.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace app\admin\controller\pcauth;
  3. use app\admin\model\PcAuthRule;
  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\PcAuthRule
  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('PcAuthRule');
  28. // 必须将结果集转换为数组
  29. $ruleList = \think\Db::name("pc_auth_rule")->field('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. $this->view->assign("typeList", $this->model->gettypeList());
  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->validate()->save($params);
  75. if ($result === false) {
  76. $this->error($this->model->getError());
  77. }
  78. Cache::rm('__menu__');
  79. $this->success();
  80. }
  81. $this->error();
  82. }
  83. return $this->view->fetch();
  84. }
  85. /**
  86. * 编辑
  87. */
  88. public function edit($ids = null)
  89. {
  90. $row = $this->model->get(['id' => $ids]);
  91. if (!$row) {
  92. $this->error(__('No Results were found'));
  93. }
  94. if ($this->request->isPost()) {
  95. $this->token();
  96. $params = $this->request->post("row/a", [], 'strip_tags');
  97. if ($params) {
  98. if (!$params['ismenu'] && !$params['pid']) {
  99. $this->error(__('The non-menu rule must have parent'));
  100. }
  101. if ($params['pid'] == $row['id']) {
  102. $this->error(__('Can not change the parent to self'));
  103. }
  104. if ($params['pid'] != $row['pid']) {
  105. $childrenIds = Tree::instance()->init(collection(PcAuthRule::select())->toArray())->getChildrenIds($row['id']);
  106. if (in_array($params['pid'], $childrenIds)) {
  107. $this->error(__('Can not change the parent to child'));
  108. }
  109. }
  110. //这里需要针对name做唯一验证
  111. $ruleValidate = \think\Loader::validate('PcAuthRule');
  112. /*$ruleValidate->rule([
  113. 'name' => 'require|unique:PcAuthRule,name,' . $row->id,
  114. ]);*/
  115. $result = $row->validate()->save($params);
  116. if ($result === false) {
  117. $this->error($row->getError());
  118. }
  119. Cache::rm('__menu__');
  120. $this->success();
  121. }
  122. $this->error();
  123. }
  124. $this->view->assign("row", $row);
  125. return $this->view->fetch();
  126. }
  127. /**
  128. * 删除
  129. */
  130. public function del($ids = "")
  131. {
  132. if (!$this->request->isPost()) {
  133. $this->error(__("Invalid parameters"));
  134. }
  135. $ids = $ids ? $ids : $this->request->post("ids");
  136. if ($ids) {
  137. $delIds = [];
  138. foreach (explode(',', $ids) as $k => $v) {
  139. $delIds = array_merge($delIds, Tree::instance()->getChildrenIds($v, true));
  140. }
  141. $delIds = array_unique($delIds);
  142. $count = $this->model->where('id', 'in', $delIds)->delete();
  143. if ($count) {
  144. Cache::rm('__menu__');
  145. $this->success();
  146. }
  147. }
  148. $this->error();
  149. }
  150. }