AdminCate.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. public function publish()
  35. {
  36. $id = $this->request->param('id', 0, 'intval');
  37. $model = new \app\admin\model\AdminCate();
  38. if ($id > 0) {
  39. if ($this->request->isPost()) {
  40. $post = $this->request->post();
  41. $validate = new \think\Validate([
  42. ['name', 'require', '角色名称不能为空'],
  43. ]);
  44. if (!$validate->check($post)) {
  45. $this->error('提交失败:' . $validate->getError());
  46. }
  47. $name = $model->where(['name' => $post['name'], 'id' => ['neq', $post['id']]])->select();
  48. if (!empty($name)) {
  49. $this->error('提交失败:该角色名已存在');
  50. }
  51. //处理选中的权限菜单id,转为字符串
  52. if (!empty($post['admin_menu_id'])) {
  53. $post['permissions'] = implode(',', $post['admin_menu_id']);
  54. } else {
  55. $post['permissions'] = '0';
  56. }
  57. if (false == $model->allowField(true)->save($post, ['id' => $id])) {
  58. $this->error('修改失败');
  59. } else {
  60. $this->success('修改角色信息成功', 'index');
  61. }
  62. } else {
  63. $info['cate'] = $model->where('id', $id)->find();
  64. if (!empty($info['cate']['permissions'])) {
  65. //将菜单id字符串拆分成数组
  66. $info['cate']['permissions'] = explode(',', $info['cate']['permissions']);
  67. }
  68. $menus = Db::name('admin_menu')->where('type', 1)->order('orders asc')->select();
  69. $info['menu'] = $this->menulist($menus);
  70. $this->assign('info', $info);
  71. return $this->fetch();
  72. }
  73. } else {
  74. if ($this->request->isPost()) {
  75. $post = $this->request->post();
  76. $validate = new \think\Validate([
  77. ['name', 'require', '角色名称不能为空'],
  78. ]);
  79. if (!$validate->check($post)) {
  80. $this->error('提交失败:' . $validate->getError());
  81. }
  82. $name = $model->where('name', $post['name'])->find();
  83. if (!empty($name)) {
  84. $this->error('提交失败:该角色名已存在');
  85. }
  86. //处理选中的权限菜单id,转为字符串
  87. if (!empty($post['admin_menu_id'])) {
  88. $post['permissions'] = implode(',', $post['admin_menu_id']);
  89. } else {
  90. $post['permissions'] = '0';
  91. }
  92. if (false == $model->allowField(true)->save($post)) {
  93. $this->error('添加角色失败');
  94. } else {
  95. $this->success('添加角色成功', 'index');
  96. }
  97. } else {
  98. $menus = Db::name('admin_menu')->where('type', 1)->order('orders asc')->select();
  99. $info['menu'] = $this->menulist($menus);
  100. $this->assign('info', $info);
  101. return $this->fetch();
  102. }
  103. }
  104. }
  105. public function preview()
  106. {
  107. $id = $this->request->param('id', 0, 'intval');
  108. $model = new \app\admin\model\AdminCate();
  109. $info['cate'] = $model->where('id', $id)->find();
  110. if (!empty($info['cate']['permissions'])) {
  111. //将菜单id字符串拆分成数组
  112. $info['cate']['permissions'] = explode(',', $info['cate']['permissions']);
  113. }
  114. $menus = Db::name('admin_menu')->where('type', 1)->order('orders asc')->select();
  115. $info['menu'] = $this->menulist($menus);
  116. $this->assign('info', $info);
  117. return $this->fetch();
  118. }
  119. protected function menulist($menu, $id = 0, $level = 0)
  120. {
  121. static $menus = array();
  122. $size = count($menus) - 1;
  123. foreach ($menu as $value) {
  124. if ($value['pid'] == $id) {
  125. $value['level'] = $level + 1;
  126. $value['str'] = $level == 0 ? "" : str_repeat('&emsp;&emsp;', $level) . '└ ';
  127. if ($level == 0) {
  128. $menus[] = $value;
  129. } else {
  130. $menus[$size]['list'][] = $value;
  131. }
  132. $this->menulist($menu, $value['id'], $value['level']);
  133. }
  134. }
  135. return $menus;
  136. }
  137. public function delete()
  138. {
  139. if ($this->request->isAjax()) {
  140. $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
  141. if ($id > 0) {
  142. if ($id == 1) {
  143. $this->error('超级管理员角色不能删除');
  144. }
  145. if (false == Db::name('admin_cate')->where('id', $id)->delete()) {
  146. $this->error('删除失败');
  147. } else {
  148. $this->success('删除成功', 'index');
  149. }
  150. } else {
  151. $this->error('id不正确');
  152. }
  153. }
  154. }
  155. }