No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Rule.php 6.2KB

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