Rule.php 3.4 KB

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