Rule.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. Tree::instance()->init(collection($this->model->order('weigh', 'desc')->select())->toArray());
  23. $this->rulelist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
  24. $ruledata = [0 => __('None')];
  25. foreach ($this->rulelist as $k => $v)
  26. {
  27. if (!$v['ismenu'])
  28. continue;
  29. $ruledata[$v['id']] = $v['title'];
  30. }
  31. $this->view->assign('ruledata', $ruledata);
  32. }
  33. /**
  34. * 查看
  35. */
  36. public function index()
  37. {
  38. if ($this->request->isAjax())
  39. {
  40. $list = $this->rulelist;
  41. $total = count($this->rulelist);
  42. $result = array("total" => $total, "rows" => $list);
  43. return json($result);
  44. }
  45. return $this->view->fetch();
  46. }
  47. /**
  48. * 添加
  49. */
  50. public function add()
  51. {
  52. if ($this->request->isPost())
  53. {
  54. $params = $this->request->post("row/a", [], 'strip_tags');
  55. if ($params)
  56. {
  57. if (!$params['ismenu'] && !$params['pid'])
  58. {
  59. $this->error(__('The non-menu rule must have parent'));
  60. }
  61. $this->model->create($params);
  62. Cache::rm('__menu__');
  63. $this->success();
  64. }
  65. $this->error();
  66. }
  67. return $this->view->fetch();
  68. }
  69. /**
  70. * 编辑
  71. */
  72. public function edit($ids = NULL)
  73. {
  74. $row = $this->model->get(['id' => $ids]);
  75. if (!$row)
  76. $this->error(__('No Results were found'));
  77. if ($this->request->isPost())
  78. {
  79. $params = $this->request->post("row/a", [], 'strip_tags');
  80. if ($params)
  81. {
  82. if (!$params['ismenu'] && !$params['pid'])
  83. {
  84. $this->error(__('The non-menu rule must have parent'));
  85. }
  86. $row->save($params);
  87. Cache::rm('__menu__');
  88. $this->success();
  89. }
  90. $this->error();
  91. }
  92. $this->view->assign("row", $row);
  93. return $this->view->fetch();
  94. }
  95. /**
  96. * 删除
  97. */
  98. public function del($ids = "")
  99. {
  100. if ($ids)
  101. {
  102. $delIds = [];
  103. foreach (explode(',', $ids) as $k => $v)
  104. {
  105. $delIds = array_merge($delIds, Tree::instance()->getChildrenIds($v, TRUE));
  106. }
  107. $delIds = array_unique($delIds);
  108. $count = $this->model->where('id', 'in', $delIds)->delete();
  109. if ($count)
  110. {
  111. Cache::rm('__menu__');
  112. $this->success();
  113. }
  114. }
  115. $this->error();
  116. }
  117. }