Rule.php 4.1 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')->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. $this->view->assign('ruledata', $ruledata);
  42. }
  43. /**
  44. * 查看
  45. */
  46. public function index()
  47. {
  48. if ($this->request->isAjax())
  49. {
  50. $list = $this->rulelist;
  51. $total = count($this->rulelist);
  52. $result = array("total" => $total, "rows" => $list);
  53. return json($result);
  54. }
  55. return $this->view->fetch();
  56. }
  57. /**
  58. * 添加
  59. */
  60. public function add()
  61. {
  62. if ($this->request->isPost())
  63. {
  64. $params = $this->request->post("row/a", [], 'strip_tags');
  65. if ($params)
  66. {
  67. if (!$params['ismenu'] && !$params['pid'])
  68. {
  69. $this->error(__('The non-menu rule must have parent'));
  70. }
  71. $result = $this->model->validate()->save($params);
  72. if ($result === FALSE)
  73. {
  74. $this->error($this->model->getError());
  75. }
  76. Cache::rm('__menu__');
  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. if ($this->request->isPost())
  92. {
  93. $params = $this->request->post("row/a", [], 'strip_tags');
  94. if ($params)
  95. {
  96. if (!$params['ismenu'] && !$params['pid'])
  97. {
  98. $this->error(__('The non-menu rule must have parent'));
  99. }
  100. //这里需要针对name做唯一验证
  101. $ruleValidate = \think\Loader::validate('AuthRule');
  102. $ruleValidate->rule([
  103. 'name' => 'require|format|unique:AuthRule,name,' . $row->id,
  104. ]);
  105. $result = $row->validate()->save($params);
  106. if ($result === FALSE)
  107. {
  108. $this->error($row->getError());
  109. }
  110. Cache::rm('__menu__');
  111. $this->success();
  112. }
  113. $this->error();
  114. }
  115. $this->view->assign("row", $row);
  116. return $this->view->fetch();
  117. }
  118. /**
  119. * 删除
  120. */
  121. public function del($ids = "")
  122. {
  123. if ($ids)
  124. {
  125. $delIds = [];
  126. foreach (explode(',', $ids) as $k => $v)
  127. {
  128. $delIds = array_merge($delIds, Tree::instance()->getChildrenIds($v, TRUE));
  129. }
  130. $delIds = array_unique($delIds);
  131. $count = $this->model->where('id', 'in', $delIds)->delete();
  132. if ($count)
  133. {
  134. Cache::rm('__menu__');
  135. $this->success();
  136. }
  137. }
  138. $this->error();
  139. }
  140. }