Rule.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace app\admin\controller\auth;
  3. use app\common\controller\Backend;
  4. use fast\Tree;
  5. /**
  6. * 规则管理
  7. *
  8. * @icon fa fa-list
  9. * @remark 规则通常对应一个控制器的方法,同时左侧的菜单栏数据也从规则中体现,通常建议通过控制台进行生成规则节点
  10. */
  11. class Rule extends Backend
  12. {
  13. protected $model = null;
  14. protected $rulelist = [];
  15. public function _initialize()
  16. {
  17. parent::_initialize();
  18. $this->model = model('AuthRule');
  19. // 必须将结果集转换为数组
  20. Tree::instance()->init(collection($this->model->order('weigh', 'desc')->select())->toArray());
  21. $this->rulelist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
  22. $ruledata = [];
  23. foreach ($this->rulelist as $k => $v)
  24. {
  25. $ruledata[$v['id']] = $v['title'];
  26. }
  27. $this->view->assign('ruledata', $ruledata);
  28. }
  29. /**
  30. * 查看
  31. */
  32. public function index()
  33. {
  34. if ($this->request->isAjax())
  35. {
  36. $list = $this->rulelist;
  37. $total = count($this->rulelist);
  38. $result = array("total" => $total, "rows" => $list);
  39. return json($result);
  40. }
  41. return $this->view->fetch();
  42. }
  43. /**
  44. * 添加
  45. */
  46. public function add()
  47. {
  48. if ($this->request->isPost())
  49. {
  50. $this->code = -1;
  51. $params = $this->request->post("row/a");
  52. if ($params)
  53. {
  54. $this->model->create($params);
  55. $this->code = 1;
  56. }
  57. return;
  58. }
  59. return $this->view->fetch();
  60. }
  61. /**
  62. * 编辑
  63. */
  64. public function edit($ids = NULL)
  65. {
  66. $row = $this->model->get(['id' => $ids]);
  67. if (!$row)
  68. $this->error(__('No Results were found'));
  69. if ($this->request->isPost())
  70. {
  71. $this->code = -1;
  72. $params = $this->request->post("row/a");
  73. if ($params)
  74. {
  75. $row->save($params);
  76. $this->code = 1;
  77. }
  78. return;
  79. }
  80. $this->view->assign("row", $row);
  81. return $this->view->fetch();
  82. }
  83. /**
  84. * 删除
  85. */
  86. public function del($ids = "")
  87. {
  88. $this->code = -1;
  89. if ($ids)
  90. {
  91. $count = $this->model->where('id', 'in', $ids)->delete();
  92. if ($count)
  93. {
  94. $this->code = 1;
  95. }
  96. }
  97. return;
  98. }
  99. }