Rule.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. $this->model->create($params);
  69. Cache::rm('__menu__');
  70. $this->success();
  71. }
  72. $this->error();
  73. }
  74. return $this->view->fetch();
  75. }
  76. /**
  77. * 编辑
  78. */
  79. public function edit($ids = NULL)
  80. {
  81. $row = $this->model->get(['id' => $ids]);
  82. if (!$row)
  83. $this->error(__('No Results were found'));
  84. if ($this->request->isPost())
  85. {
  86. $params = $this->request->post("row/a", [], 'strip_tags');
  87. if ($params)
  88. {
  89. if (!$params['ismenu'] && !$params['pid'])
  90. {
  91. $this->error(__('The non-menu rule must have parent'));
  92. }
  93. $row->save($params);
  94. Cache::rm('__menu__');
  95. $this->success();
  96. }
  97. $this->error();
  98. }
  99. $this->view->assign("row", $row);
  100. return $this->view->fetch();
  101. }
  102. /**
  103. * 删除
  104. */
  105. public function del($ids = "")
  106. {
  107. if ($ids)
  108. {
  109. $delIds = [];
  110. foreach (explode(',', $ids) as $k => $v)
  111. {
  112. $delIds = array_merge($delIds, Tree::instance()->getChildrenIds($v, TRUE));
  113. }
  114. $delIds = array_unique($delIds);
  115. $count = $this->model->where('id', 'in', $delIds)->delete();
  116. if ($count)
  117. {
  118. Cache::rm('__menu__');
  119. $this->success();
  120. }
  121. }
  122. $this->error();
  123. }
  124. }