Rule.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace app\admin\controller\auth;
  3. use app\admin\model\AdminLog;
  4. use app\common\controller\Backend;
  5. use fast\Tree;
  6. use think\Cache;
  7. /**
  8. * 规则管理
  9. *
  10. * @icon fa fa-list
  11. * @remark 规则通常对应一个控制器的方法,同时左侧的菜单栏数据也从规则中体现,通常建议通过控制台进行生成规则节点
  12. */
  13. class Rule extends Backend
  14. {
  15. protected $model = null;
  16. protected $rulelist = [];
  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. AdminLog::record(__('Add'), $this->model->getLastInsID());
  65. Cache::rm('__menu__');
  66. $this->code = 1;
  67. }
  68. return;
  69. }
  70. return $this->view->fetch();
  71. }
  72. /**
  73. * 编辑
  74. */
  75. public function edit($ids = NULL)
  76. {
  77. $row = $this->model->get(['id' => $ids]);
  78. if (!$row)
  79. $this->error(__('No Results were found'));
  80. if ($this->request->isPost())
  81. {
  82. $this->code = -1;
  83. $params = $this->request->post("row/a");
  84. if ($params)
  85. {
  86. if (!$params['ismenu'] && !$params['pid'])
  87. {
  88. $this->msg = __('The non-menu rule must have parent');
  89. return;
  90. }
  91. $row->save($params);
  92. AdminLog::record(__('Edit'), $ids);
  93. Cache::rm('__menu__');
  94. $this->code = 1;
  95. }
  96. return;
  97. }
  98. $this->view->assign("row", $row);
  99. return $this->view->fetch();
  100. }
  101. /**
  102. * 删除
  103. */
  104. public function del($ids = "")
  105. {
  106. $this->code = -1;
  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. AdminLog::record(__('Del'), $ids);
  119. Cache::rm('__menu__');
  120. $this->code = 1;
  121. }
  122. }
  123. return;
  124. }
  125. /**
  126. * 批量更新
  127. * @internal
  128. */
  129. public function multi($ids = "")
  130. {
  131. // 节点禁止批量操作
  132. $this->code = -1;
  133. }
  134. }