Role.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace app\controller\admin;
  3. use laytp\controller\Backend;
  4. use laytp\library\CommonFun;
  5. use think\facade\Config;
  6. use think\facade\Db;
  7. use think\facade\Request;
  8. /**
  9. * 角色控制器
  10. */
  11. class Role extends Backend
  12. {
  13. protected $noNeedAuth = ['getMenuIds'];
  14. public $model;
  15. public function _initialize()
  16. {
  17. $this->model = new \app\model\admin\Role();
  18. }
  19. /**
  20. * 列表
  21. * all_data参数的值为true时,表示查询表中所有数据集,否则进行分页查询
  22. * @return mixed
  23. */
  24. public function index()
  25. {
  26. $where = $this->buildSearchParams();
  27. $order = $this->buildOrder();
  28. $data = $this->model->where($where)->order($order);
  29. $paging = $this->request->param('paging', false);
  30. if ($paging) {
  31. $limit = $this->request->param('limit', Config::get('paginate.limit'));
  32. $data = $data->paginate($limit)->toArray();
  33. $data['data'] = $this->getSelectedData($data['data']);
  34. } else {
  35. $data = $data->select()->toArray();
  36. }
  37. return $this->success('数据获取成功', $data);
  38. }
  39. /**
  40. * 添加,同时添加lt_plugin_core_role和lt_plugin_core_role_menu两个表的数据
  41. * @return \think\response\Json
  42. */
  43. public function add()
  44. {
  45. Db::startTrans();
  46. try {
  47. $post = CommonFun::filterPostData($this->request->post());
  48. $roleInfo = $this->model->getByName($post['name']);
  49. if ($roleInfo) throw new \Exception('角色名已存在');
  50. $menuIds = explode(',', $post['menu_ids']);
  51. unset($post['menu_ids']);
  52. $saveMenu = $this->model->save($post);
  53. if (!$saveMenu) throw new \Exception('保存角色基本信息失败');
  54. $saveAllData = [];
  55. foreach ($menuIds as $menu_id) {
  56. $saveAllData[] = [
  57. 'admin_role_id' => $this->model->id,
  58. 'admin_menu_id' => $menu_id,
  59. ];
  60. }
  61. $menu = new \app\model\admin\menu\Role();
  62. $saveAllMenu = $menu->saveAll($saveAllData);
  63. if (!$saveAllMenu) throw new \Exception('保存角色权限失败');
  64. Db::commit();
  65. return $this->success('操作成功');
  66. } catch (\Exception $e) {
  67. Db::rollback();
  68. return $this->error('数据库异常,操作失败');
  69. }
  70. }
  71. /**
  72. * 编辑,同时编辑lt_plugin_core_role和lt_plugin_core_role_menu两个表的数据
  73. * @return bool|\think\response\Json
  74. */
  75. public function edit()
  76. {
  77. $id = $this->request->param('id');
  78. $postData = Request::only(['id', 'name', 'menu_ids']);
  79. $post = CommonFun::filterPostData($postData);
  80. $roleInfo = $this->model->getByName($post['name']);
  81. if ($roleInfo && ($roleInfo['id'] != $id)) {
  82. return $this->error('角色名已存在');
  83. }
  84. Db::startTrans();
  85. try {
  86. $menuIds = explode(',', $post['menu_ids']);
  87. unset($post['menu_ids']);
  88. $updateRes = $this->model->where('id', '=', $id)->update($post);
  89. if (!is_numeric($updateRes)) throw new \Exception('保存角色基本信息失败');
  90. $delRes = \app\model\admin\menu\Role::where('admin_role_id', '=', $id)->delete();
  91. if (!is_numeric($delRes)) throw new \Exception('删除角色权限失败');
  92. $saveAllData = [];
  93. foreach ($menuIds as $menu_id) {
  94. $saveAllData[] = [
  95. 'admin_role_id' => $id,
  96. 'admin_menu_id' => $menu_id,
  97. ];
  98. }
  99. $menu = new \app\model\admin\menu\Role();
  100. $menu->saveAll($saveAllData);
  101. Db::commit();
  102. return $this->success('操作成功');
  103. } catch (\Exception $e) {
  104. Db::rollback();
  105. return $this->error('数据库异常,操作失败');
  106. }
  107. }
  108. /**
  109. * 真实删除
  110. * lt_admin_role、lt_admin_menu_role、lt_admin_role_user三表数据都要真实删除
  111. * @return \think\response\Json
  112. */
  113. public function trueDel()
  114. {
  115. $ids = $this->request->param('ids');
  116. Db::startTrans();
  117. try {
  118. $roles = $this->model->onlyTrashed()->where('id', 'in', $ids)->select();
  119. foreach ($roles as $key => $item) {
  120. $delRes = $item->force()->delete();
  121. if (!$delRes) throw new \Exception('角色删除失败');
  122. }
  123. $delRes = \app\model\admin\menu\Role::where('admin_role_id', 'in', $ids)->delete();
  124. if (!is_numeric($delRes)) throw new \Exception('角色权限删除失败');
  125. $delRes = \app\model\admin\role\User::where('admin_role_id', 'in', $ids)->delete();
  126. if (!is_numeric($delRes)) throw new \Exception('角色用户删除失败');
  127. Db::commit();
  128. return $this->success('操作成功');
  129. } catch (\Exception $e) {
  130. Db::rollback();
  131. return $this->exceptionError($e);
  132. }
  133. }
  134. /**
  135. * 获取编辑页面权限设置应该选中的菜单id
  136. * 只能获取最低级别的菜单id,有子菜单的菜单id不能返回
  137. * 原因:比如tree.setChecked('auth_node',1);会选中整棵树,因为layui的树组件,模拟点击了树的首节点
  138. * @return false|string|\think\response\Json
  139. * @throws \think\db\exception\DataNotFoundException
  140. * @throws \think\db\exception\DbException
  141. * @throws \think\db\exception\ModelNotFoundException
  142. */
  143. public function getMenuIds()
  144. {
  145. $id = $this->request->param('id');
  146. $menuIds = \app\model\admin\menu\Role::where('admin_role_id', '=', $id)->column('admin_menu_id');
  147. $auth = [];
  148. foreach ($menuIds as $menuId) {
  149. $hasChild = \app\model\admin\Menu::where('pid', '=', $menuId)->find() ? true : false;
  150. if (!$hasChild) {
  151. $auth[] = $menuId;
  152. }
  153. }
  154. return $this->success('获取成功', $auth);
  155. }
  156. }