AdminCate.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 中闽 < 1464674022@qq.com >
  5. * Date: 2023/02/05
  6. * Time: 20:33
  7. */
  8. namespace app\admin\controller;
  9. use app\admin\controller\base\Permissions;
  10. use think\Db;
  11. class AdminCate extends Permissions
  12. {
  13. /**
  14. * 管理员权限分组列表
  15. * @return mixed
  16. */
  17. public function index()
  18. {
  19. $model = new \app\admin\model\AdminCate;
  20. $post = $this->request->param();
  21. $where = [];
  22. if (isset($post['keywords']) and !empty($post['keywords'])) {
  23. $where['name'] = ['like', '%' . $post['keywords'] . '%'];
  24. }
  25. if (isset($post['create_time']) and !empty($post['create_time'])) {
  26. $min_time = strtotime($post['create_time']);
  27. $max_time = $min_time + 24 * 60 * 60;
  28. $where['create_time'] = [['>=', $min_time], ['<=', $max_time]];
  29. }
  30. $cate = $model->where($where)->order('create_time desc')->paginate(15, false, ['query' => $this->request->param()]);
  31. $this->assign('cate', $cate);
  32. return $this->fetch();
  33. }
  34. /**
  35. * 管理员角色添加和修改操作
  36. * @return mixed
  37. */
  38. public function publish()
  39. {
  40. $id = $this->request->param('id', 0, 'intval');
  41. $model = new \app\admin\model\AdminCate();
  42. if ($id > 0) {
  43. if ($this->request->isPost()) {
  44. $post = $this->request->post();
  45. $validate = new \think\Validate([
  46. ['name', 'require', '角色名称不能为空'],
  47. ]);
  48. if (!$validate->check($post)) {
  49. $this->error('提交失败:' . $validate->getError());
  50. }
  51. $name = $model->where(['name' => $post['name'], 'id' => ['neq', $post['id']]])->select();
  52. if (!empty($name)) {
  53. $this->error('提交失败:该角色名已存在');
  54. }
  55. //处理选中的权限菜单id,转为字符串
  56. if (!empty($post['admin_menu_id'])) {
  57. $post['permissions'] = implode(',', $post['admin_menu_id']);
  58. } else {
  59. $post['permissions'] = '0';
  60. }
  61. if (false == $model->allowField(true)->save($post, ['id' => $id])) {
  62. $this->error('修改失败');
  63. } else {
  64. $this->success('修改角色信息成功', 'index');
  65. }
  66. } else {
  67. $info['cate'] = $model->where('id', $id)->find();
  68. if (!empty($info['cate']['permissions'])) {
  69. //将菜单id字符串拆分成数组
  70. $info['cate']['permissions'] = explode(',', $info['cate']['permissions']);
  71. }
  72. $menus = Db::name('admin_menu')->where('type', 1)->order('orders asc')->select();
  73. $info['menu'] = $this->menulist($menus);
  74. $this->assign('info', $info);
  75. return $this->fetch();
  76. }
  77. } else {
  78. if ($this->request->isPost()) {
  79. $post = $this->request->post();
  80. $validate = new \think\Validate([
  81. ['name', 'require', '角色名称不能为空'],
  82. ]);
  83. if (!$validate->check($post)) {
  84. $this->error('提交失败:' . $validate->getError());
  85. }
  86. $name = $model->where('name', $post['name'])->find();
  87. if (!empty($name)) {
  88. $this->error('提交失败:该角色名已存在');
  89. }
  90. //处理选中的权限菜单id,转为字符串
  91. if (!empty($post['admin_menu_id'])) {
  92. $post['permissions'] = implode(',', $post['admin_menu_id']);
  93. } else {
  94. $post['permissions'] = '0';
  95. }
  96. if (false == $model->allowField(true)->save($post)) {
  97. $this->error('添加角色失败');
  98. } else {
  99. $this->success('添加角色成功', 'index');
  100. }
  101. } else {
  102. $menus = Db::name('admin_menu')->where('type', 1)->order('orders asc')->select();
  103. $info['menu'] = $this->menulist($menus);
  104. $this->assign('info', $info);
  105. return $this->fetch();
  106. }
  107. }
  108. }
  109. public function preview()
  110. {
  111. $id = $this->request->param('id', 0, 'intval');
  112. $model = new \app\admin\model\AdminCate();
  113. $info['cate'] = $model->where('id', $id)->find();
  114. if (!empty($info['cate']['permissions'])) {
  115. //将菜单id字符串拆分成数组
  116. $info['cate']['permissions'] = explode(',', $info['cate']['permissions']);
  117. }
  118. $menus = Db::name('admin_menu')->where('type', 1)->order('orders asc')->select();
  119. $info['menu'] = $this->menulist($menus);
  120. $this->assign('info', $info);
  121. return $this->fetch();
  122. }
  123. protected function menulist($menu, $id = 0, $level = 0)
  124. {
  125. static $menus = array();
  126. $size = count($menus) - 1;
  127. foreach ($menu as $value) {
  128. if ($value['pid'] == $id) {
  129. $value['level'] = $level + 1;
  130. $value['str'] = $level == 0 ? "" : str_repeat('&emsp;&emsp;', $level) . '└ ';
  131. if ($level == 0) {
  132. $menus[] = $value;
  133. } else {
  134. $menus[$size]['list'][] = $value;
  135. }
  136. $this->menulist($menu, $value['id'], $value['level']);
  137. }
  138. }
  139. return $menus;
  140. }
  141. /**
  142. * 管理员角色删除
  143. */
  144. public function delete()
  145. {
  146. if ($this->request->isAjax()) {
  147. $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
  148. if ($id > 0) {
  149. if ($id == 1) {
  150. $this->error('超级管理员角色不能删除');
  151. }
  152. if (false == Db::name('admin_cate')->where('id', $id)->delete()) {
  153. $this->error('删除失败');
  154. } else {
  155. $this->success('删除成功', 'index');
  156. }
  157. } else {
  158. $this->error('id不正确');
  159. }
  160. }
  161. }
  162. }