Rule.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. protected $model = null;
  15. protected $rulelist = [];
  16. protected $multiFields = 'ismenu,status';
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = model('AuthRule');
  21. // 必须将结果集转换为数组
  22. $ruleList = collection($this->model->order('weigh', 'desc')->select())->toArray();
  23. foreach ($ruleList as $k => &$v)
  24. {
  25. $v['title'] = __($v['title']);
  26. $v['remark'] = __($v['remark']);
  27. }
  28. unset($v);
  29. Tree::instance()->init($ruleList);
  30. $this->rulelist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
  31. $ruledata = [0 => __('None')];
  32. foreach ($this->rulelist as $k => &$v)
  33. {
  34. if (!$v['ismenu'])
  35. continue;
  36. $ruledata[$v['id']] = $v['title'];
  37. }
  38. $this->view->assign('ruledata', $ruledata);
  39. }
  40. /**
  41. * 查看
  42. */
  43. public function index()
  44. {
  45. if ($this->request->isAjax())
  46. {
  47. $list = $this->rulelist;
  48. $total = count($this->rulelist);
  49. $result = array("total" => $total, "rows" => $list);
  50. return json($result);
  51. }
  52. return $this->view->fetch();
  53. }
  54. /**
  55. * 添加
  56. */
  57. public function add()
  58. {
  59. if ($this->request->isPost())
  60. {
  61. $params = $this->request->post("row/a", [], 'strip_tags');
  62. if ($params)
  63. {
  64. if (!$params['ismenu'] && !$params['pid'])
  65. {
  66. $this->error(__('The non-menu rule must have parent'));
  67. }
  68. $result = $this->model->validate()->save($params);
  69. if ($result === FALSE)
  70. {
  71. $this->error($this->model->getError());
  72. }
  73. Cache::rm('__menu__');
  74. $this->success();
  75. }
  76. $this->error();
  77. }
  78. return $this->view->fetch();
  79. }
  80. /**
  81. * 编辑
  82. */
  83. public function edit($ids = NULL)
  84. {
  85. $row = $this->model->get(['id' => $ids]);
  86. if (!$row)
  87. $this->error(__('No Results were found'));
  88. if ($this->request->isPost())
  89. {
  90. $params = $this->request->post("row/a", [], 'strip_tags');
  91. if ($params)
  92. {
  93. if (!$params['ismenu'] && !$params['pid'])
  94. {
  95. $this->error(__('The non-menu rule must have parent'));
  96. }
  97. //这里需要针对name做唯一验证
  98. $ruleValidate = \think\Loader::validate('AuthRule');
  99. $ruleValidate->rule([
  100. 'name' => 'require|format|unique:AuthRule,name,' . $row->id,
  101. ]);
  102. $result = $row->validate()->save($params);
  103. if ($result === FALSE)
  104. {
  105. $this->error($row->getError());
  106. }
  107. Cache::rm('__menu__');
  108. $this->success();
  109. }
  110. $this->error();
  111. }
  112. $this->view->assign("row", $row);
  113. return $this->view->fetch();
  114. }
  115. /**
  116. * 删除
  117. */
  118. public function del($ids = "")
  119. {
  120. if ($ids)
  121. {
  122. $delIds = [];
  123. foreach (explode(',', $ids) as $k => $v)
  124. {
  125. $delIds = array_merge($delIds, Tree::instance()->getChildrenIds($v, TRUE));
  126. }
  127. $delIds = array_unique($delIds);
  128. $count = $this->model->where('id', 'in', $delIds)->delete();
  129. if ($count)
  130. {
  131. Cache::rm('__menu__');
  132. $this->success();
  133. }
  134. }
  135. $this->error();
  136. }
  137. }