Rule.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace app\admin\controller\auth;
  3. use app\common\controller\Backend;
  4. use fast\Tree;
  5. use think\Cache;
  6. /**
  7. * 规则管理
  8. *
  9. * @icon fa fa-list
  10. * @remark 规则通常对应一个控制器的方法,同时左侧的菜单栏数据也从规则中体现,通常建议通过控制台进行生成规则节点
  11. */
  12. class Rule extends Backend
  13. {
  14. /**
  15. * @var \app\admin\model\AuthRule
  16. */
  17. protected $model = null;
  18. protected $rulelist = [];
  19. protected $multiFields = 'ismenu,status';
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = model('AuthRule');
  24. // 必须将结果集转换为数组
  25. $ruleList = collection($this->model->order('weigh', 'desc')->order('id', 'asc')->select())->toArray();
  26. foreach ($ruleList as $k => &$v)
  27. {
  28. $v['title'] = __($v['title']);
  29. $v['remark'] = __($v['remark']);
  30. }
  31. unset($v);
  32. Tree::instance()->init($ruleList);
  33. $this->rulelist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
  34. $ruledata = [0 => __('None')];
  35. foreach ($this->rulelist as $k => &$v)
  36. {
  37. if (!$v['ismenu'])
  38. continue;
  39. $ruledata[$v['id']] = $v['title'];
  40. }
  41. unset($v);
  42. $this->view->assign('ruledata', $ruledata);
  43. }
  44. /**
  45. * 查看
  46. */
  47. public function index()
  48. {
  49. if ($this->request->isAjax())
  50. {
  51. $list = $this->rulelist;
  52. $total = count($this->rulelist);
  53. $result = array("total" => $total, "rows" => $list);
  54. return json($result);
  55. }
  56. return $this->view->fetch();
  57. }
  58. /**
  59. * 添加
  60. */
  61. public function add()
  62. {
  63. if ($this->request->isPost())
  64. {
  65. $params = $this->request->post("row/a", [], 'strip_tags');
  66. if ($params)
  67. {
  68. if (!$params['ismenu'] && !$params['pid'])
  69. {
  70. $this->error(__('The non-menu rule must have parent'));
  71. }
  72. $result = $this->model->validate()->save($params);
  73. if ($result === FALSE)
  74. {
  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. if ($this->request->isPost())
  93. {
  94. $params = $this->request->post("row/a", [], 'strip_tags');
  95. if ($params)
  96. {
  97. if (!$params['ismenu'] && !$params['pid'])
  98. {
  99. $this->error(__('The non-menu rule must have parent'));
  100. }
  101. //这里需要针对name做唯一验证
  102. $ruleValidate = \think\Loader::validate('AuthRule');
  103. $ruleValidate->rule([
  104. 'name' => 'require|format|unique:AuthRule,name,' . $row->id,
  105. ]);
  106. $result = $row->validate()->save($params);
  107. if ($result === FALSE)
  108. {
  109. $this->error($row->getError());
  110. }
  111. Cache::rm('__menu__');
  112. $this->success();
  113. }
  114. $this->error();
  115. }
  116. $this->view->assign("row", $row);
  117. return $this->view->fetch();
  118. }
  119. /**
  120. * 删除
  121. */
  122. public function del($ids = "")
  123. {
  124. if ($ids)
  125. {
  126. $delIds = [];
  127. foreach (explode(',', $ids) as $k => $v)
  128. {
  129. $delIds = array_merge($delIds, Tree::instance()->getChildrenIds($v, TRUE));
  130. }
  131. $delIds = array_unique($delIds);
  132. $count = $this->model->where('id', 'in', $delIds)->delete();
  133. if ($count)
  134. {
  135. Cache::rm('__menu__');
  136. $this->success();
  137. }
  138. }
  139. $this->error();
  140. }
  141. }