Rule.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // | Original reference: https://gitee.com/karson/fastadmin
  12. // +----------------------------------------------------------------------
  13. // +----------------------------------------------------------------------
  14. // | 后台菜单管理
  15. // +----------------------------------------------------------------------
  16. namespace app\admin\controller\auth;
  17. use app\admin\model\AuthRule as AuthRuleModel;
  18. use app\common\controller\Backend;
  19. use think\exception\ValidateException;
  20. use think\facade\Cache;
  21. use think\facade\Db;
  22. use util\Tree;
  23. class Rule extends Backend
  24. {
  25. protected $modelClass = null;
  26. protected function initialize()
  27. {
  28. parent::initialize();
  29. $this->modelClass = new AuthRuleModel;
  30. }
  31. //后台菜单首页
  32. public function index()
  33. {
  34. if ($this->request->isAjax()) {
  35. $ruleList = AuthRuleModel::order('listorder DESC,id ASC')->select()->toArray();
  36. $tree = new Tree();
  37. $tree->icon = ['', '', ''];
  38. $tree->nbsp = '';
  39. $tree->init($ruleList);
  40. $list = $tree->getTreeArray(0);
  41. $total = count($list);
  42. $result = ["code" => 0, "count" => $total, "data" => $list];
  43. return json($result);
  44. }
  45. return $this->fetch();
  46. }
  47. //新增
  48. public function add()
  49. {
  50. if ($this->request->isPost()) {
  51. $this->token();
  52. $params = $this->request->post("row/a", [], 'strip_tags');
  53. if ($params) {
  54. if (!$params['ismenu'] && !$params['parentid']) {
  55. $this->error('非菜单规则节点必须有父级');
  56. }
  57. try {
  58. $this->validate($params, 'app\admin\validate\AuthRule');
  59. $this->modelClass->save($params);
  60. } catch (ValidateException | \Exception $e) {
  61. $this->error($e->getMessage());
  62. }
  63. $this->success('新增成功');
  64. }
  65. $this->error('参数不能为空');
  66. }
  67. $tree = new Tree();
  68. $parentid = $this->request->param('parentid/d', 0);
  69. $ruleList = AuthRuleModel::where('ismenu', 1)->order('listorder DESC,id ASC')->select()->toArray();
  70. $tree->init($ruleList);
  71. $select_categorys = $tree->getTree(0, '', $parentid);
  72. $this->assign("select_categorys", $select_categorys);
  73. return $this->fetch();
  74. }
  75. //编辑
  76. public function edit()
  77. {
  78. $id = $this->request->param('id/d', 0);
  79. $row = $this->modelClass->find($id);
  80. if (!$row) {
  81. $this->error('记录未找到');
  82. }
  83. if ($this->request->isPost()) {
  84. $this->token();
  85. $params = $this->request->post("row/a", [], 'strip_tags');
  86. if ($params) {
  87. if (!$params['ismenu'] && !$params['parentid']) {
  88. $this->error('非菜单规则节点必须有父级');
  89. }
  90. if ($params['parentid'] == $row['id']) {
  91. $this->error('父级不能是它自己');
  92. }
  93. if ($params['parentid'] != $row['parentid']) {
  94. $childrenIds = Tree::instance()->init(AuthRuleModel::select()->toArray())->getChildrenIds($row['id']);
  95. if (in_array($params['parentid'], $childrenIds)) {
  96. $this->error('父级不能是它的子级');
  97. }
  98. }
  99. try {
  100. $this->validate($params, 'app\admin\validate\AuthRule');
  101. $row->save($params);
  102. } catch (ValidateException | \Exception $e) {
  103. $this->error($e->getMessage());
  104. }
  105. $this->success('编辑成功');
  106. }
  107. $this->error('参数不能为空');
  108. }
  109. $tree = new Tree();
  110. $result = AuthRuleModel::where('ismenu', 1)->order('listorder DESC,id ASC')->select()->toArray();
  111. $ruleList = [];
  112. foreach ($result as $r) {
  113. $r['selected'] = $r['id'] == $row->parentid ? 'selected' : '';
  114. $ruleList[] = $r;
  115. }
  116. $tree->init($ruleList);
  117. $select_categorys = $tree->getTree(0, '', $row->parentid);
  118. $this->assign("select_categorys", $select_categorys);
  119. $this->assign("data", $row);
  120. return $this->fetch();
  121. }
  122. //删除
  123. public function del()
  124. {
  125. if (false === $this->request->isPost()) {
  126. $this->error('未知参数');
  127. }
  128. $ids = $this->request->param('id/a', null);
  129. if (empty($ids)) {
  130. $this->error('参数错误!');
  131. }
  132. if (!is_array($ids)) {
  133. $ids = [0 => $ids];
  134. }
  135. if ($ids) {
  136. // 必须将结果集转换为数组
  137. $ruleList = Db::name("auth_rule")->field('id,parentid')->order('listorder DESC,id ASC')->select();
  138. Tree::instance()->init($ruleList);
  139. $delIds = [];
  140. foreach ($ids as $k => $v) {
  141. $delIds = array_merge($delIds, Tree::instance()->getChildrenIds($v, true));
  142. }
  143. $delIds = array_unique($delIds);
  144. $count = $this->modelClass->where('id', 'in', $delIds)->delete();
  145. if ($count) {
  146. Cache::delete('__menu__');
  147. $this->success("操作成功!");
  148. }
  149. }
  150. $this->error('没有数据删除!');
  151. }
  152. }