RolesController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace App\Http\Controllers\Role;
  3. use App\Http\Controllers\AdminBaseController;
  4. use App\Models\Menu;
  5. use App\Models\Role;
  6. use App\Repositories\Eloquent\RoleRepositoryEloquent;
  7. use App\Servers\MenuServer;
  8. use App\Validators\RoleValidator;
  9. use Illuminate\Contracts\Routing\ResponseFactory;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\DB;
  12. /**
  13. * Class RolesController.
  14. *
  15. * @package namespace App\Http\Controllers;
  16. */
  17. class RolesController extends AdminBaseController
  18. {
  19. /**
  20. * RolesController constructor.
  21. *
  22. * @param RoleRepositoryEloquent $repository
  23. * @param RoleValidator $validator
  24. */
  25. public function __construct(RoleRepositoryEloquent $repository, RoleValidator $validator)
  26. {
  27. parent::__construct($repository, $validator);
  28. }
  29. protected function _indexPost($datum)
  30. {
  31. foreach ( $datum as $value ) {
  32. if ($value->{'id'} ==1) {
  33. $value->skip = 'skip';
  34. } else {
  35. $value->skip = '';
  36. }
  37. }
  38. return $datum;
  39. }
  40. protected function _storePost($request)
  41. {
  42. $name = $request->input('name', '');
  43. $status = $request->input('status', '');
  44. if (empty($name)) {
  45. $this->errorMsg = "角色名称必填";
  46. return false;
  47. }
  48. // 查询角色名称是否存在
  49. $count = Role::where('name', $name)->count();
  50. if ($count > 0) {
  51. $this->errorMsg = "角色名称已存在";
  52. return false;
  53. }
  54. Role::create(['name' => $name,'status'=>$status]);
  55. return true;
  56. }
  57. protected function _updatePost($request, $id)
  58. {
  59. $name = $request->input('name', '');
  60. $status = $request->input('status', '');
  61. if (empty($name)) {
  62. $this->errorMsg = "角色名称必填";
  63. return false;
  64. }
  65. $model = $this->repository->find($id);
  66. if (empty($model)) {
  67. $this->errorMsg = "角色不存在";
  68. return false;
  69. }
  70. // 查询角色名称是否存在
  71. $count = Role::where('name', $name)->where('id','<>',$id)->count();
  72. if ($count > 0) {
  73. $this->errorMsg = "角色名称已存在";
  74. return false;
  75. }
  76. Role::where('id',$id)->update(['name' => $name,'status'=>$status]);
  77. return true;
  78. }
  79. /**
  80. * @param Request $request
  81. * @param ResponseFactory $response
  82. * @param $id
  83. * @return \Illuminate\Http\JsonResponse
  84. *
  85. * 删除一行数据
  86. */
  87. protected function destroy(Request $request, ResponseFactory $response, $id = null)
  88. {
  89. if ($request->isMethod('post') && $request->ajax()) {
  90. $count = DB::table('users')->where('roles_id', $id)->count();
  91. if ($count >= 1) {
  92. $data = [
  93. 'status' => 1,
  94. 'message' => '当前角色已绑定账号不允许删除'
  95. ];
  96. } else {
  97. $deleted = $this->repository->delete($id);
  98. $deleted === false ? $data = [
  99. 'status' => 1,
  100. 'message' => self::ERROR_MSG
  101. ] : $data = [
  102. 'status' => 0,
  103. 'message' => ''
  104. ];
  105. }
  106. if ($request->wantsJson()) {
  107. return $response->json($data);
  108. }
  109. }
  110. }
  111. function getPermissionsView()
  112. {
  113. $id = request()->input('id');
  114. $role = Role::where('id', $id)->first();
  115. $permission=MenuServer::creatServer()->getPermissionsMenu($role->{'role_p_ids'});
  116. $permission_ids= explode(',', $role->{'role_p_ids'});
  117. if(empty($permission_ids))$permission_ids=[0];
  118. return view('admins.roles.permissions',['permission'=>$permission,'permission_ids'=>$permission_ids,'id'=>$id]);
  119. }
  120. function setPermissions(){
  121. $permission_ids=request()->input('permission_ids');
  122. $id=request()->input('id');
  123. if(empty($permission_ids)){
  124. return $this->response('请选择权限', 'error');
  125. }
  126. $role_p_ids=$permission_ids;
  127. if(is_string($permission_ids))$permission_ids=explode(',',$permission_ids);
  128. $p_permission_ids= Menu::where('level', 2)->whereIn('id',$permission_ids)->pluck('p_ids');
  129. foreach ($p_permission_ids as $v){
  130. $p_ids= explode(',',$v);
  131. $permission_ids=array_merge($permission_ids,$p_ids);
  132. }
  133. $permission_ids=array_unique($permission_ids);
  134. $permission_ids=implode(',',$permission_ids);
  135. Role::where('id',$id)->update(['permission_ids'=>$permission_ids,'role_p_ids'=>$role_p_ids]);
  136. return $this->response(self::SUCCESS_MSG, 'success', '', '');
  137. }
  138. }