Rule.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. $ruledata[$v['id']] = $v['title'];
  28. }
  29. $this->view->assign('ruledata', $ruledata);
  30. }
  31. /**
  32. * 查看
  33. */
  34. public function index()
  35. {
  36. if ($this->request->isAjax())
  37. {
  38. $list = $this->rulelist;
  39. $total = count($this->rulelist);
  40. $result = array("total" => $total, "rows" => $list);
  41. return json($result);
  42. }
  43. return $this->view->fetch();
  44. }
  45. /**
  46. * 添加
  47. */
  48. public function add()
  49. {
  50. if ($this->request->isPost())
  51. {
  52. $this->code = -1;
  53. $params = $this->request->post("row/a");
  54. if ($params)
  55. {
  56. $this->model->create($params);
  57. AdminLog::record(__('Add'), $this->model->getLastInsID());
  58. Cache::rm('__menu__');
  59. $this->code = 1;
  60. }
  61. return;
  62. }
  63. return $this->view->fetch();
  64. }
  65. /**
  66. * 编辑
  67. */
  68. public function edit($ids = NULL)
  69. {
  70. $row = $this->model->get(['id' => $ids]);
  71. if (!$row)
  72. $this->error(__('No Results were found'));
  73. if ($this->request->isPost())
  74. {
  75. $this->code = -1;
  76. $params = $this->request->post("row/a");
  77. if ($params)
  78. {
  79. $row->save($params);
  80. AdminLog::record(__('Edit'), $ids);
  81. Cache::rm('__menu__');
  82. $this->code = 1;
  83. }
  84. return;
  85. }
  86. $this->view->assign("row", $row);
  87. return $this->view->fetch();
  88. }
  89. /**
  90. * 删除
  91. */
  92. public function del($ids = "")
  93. {
  94. $this->code = -1;
  95. if ($ids)
  96. {
  97. $delIds = [];
  98. foreach (explode(',', $ids) as $k => $v)
  99. {
  100. $delIds = array_merge($delIds, Tree::instance()->getChildrenIds($v, TRUE));
  101. }
  102. $delIds = array_unique($delIds);
  103. $count = $this->model->where('id', 'in', $delIds)->delete();
  104. if ($count)
  105. {
  106. AdminLog::record(__('Del'), $ids);
  107. Cache::rm('__menu__');
  108. $this->code = 1;
  109. }
  110. }
  111. return;
  112. }
  113. /**
  114. * 批量更新
  115. * @internal
  116. */
  117. public function multi($ids = "")
  118. {
  119. // 节点禁止批量操作
  120. $this->code = -1;
  121. }
  122. }