RoleController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace App\Http\Controllers\Admin\System;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use Spatie\Permission\Models\Role;
  6. use Spatie\Permission\Models\Permission;
  7. use Illuminate\Support\Facades\DB;
  8. class RoleController extends Controller
  9. {
  10. public function index()
  11. {
  12. return view('admin.system.role.index');
  13. }
  14. public function getData(Request $request)
  15. {
  16. $model = Role::select(['id', 'name','status', 'created_at']);
  17. $total = $model->count(); //总数
  18. $limit = $request->has('limit') ? $request->limit : 20;
  19. $page = $request->has('page') ? ($request->page - 1) * $limit : 0;
  20. $data = $model->offset($page)->limit($limit)->get();
  21. return json_encode(['code' => 0, 'data' => $data, 'count' => $total]);
  22. }
  23. public function add(Request $request)
  24. {
  25. $res = Role::create([
  26. 'name' => $request->name
  27. ]);
  28. return $res? $this->responseData(200, '添加成功'):$this->responseData(400, '添加失败');
  29. }
  30. public function update(Request $request)
  31. {
  32. $res = Role::where('id', $request->id)->update([
  33. 'name' => $request->name
  34. ]);
  35. return $res? $this->responseData(200, '添加成功'):$this->responseData(400, '添加失败');
  36. }
  37. public function del(Request $request)
  38. {
  39. $res = Role::where('id', $request->id)->delete();
  40. return $res? $this->responseData(200, '删除成功'):$this->responseData(400, '删除失败');
  41. }
  42. public function showPermissions(Request $request, $id)
  43. {
  44. return view('admin.system.role.permission');
  45. }
  46. public function permissionData(Request $request)
  47. {
  48. $role = Role::where('id', $request->id)->select()->first();
  49. $permissions = $role->getAllPermissions();
  50. if (!$permissions->isEmpty()) {
  51. $permissions = array_column($permissions->toArray(), 'id');
  52. } else {
  53. $permissions = [];
  54. }
  55. // return $permissions;
  56. $data = Permission::select([
  57. 'id', 'pid as parentId', 'name as title','route'
  58. ])->get();
  59. $data->map(function($query) use ($permissions){
  60. if (in_array($query->id, $permissions)) {
  61. $query->checkArr = [["type" => "0", "checked" => "1"]];
  62. } else {
  63. $query->checkArr = [["type" => "0", "checked" => "0"]];
  64. }
  65. return $query;
  66. });
  67. $data = getTree($data);
  68. return [
  69. "status" => [
  70. "code" => 200,
  71. "message" => 'success'
  72. ],
  73. "data" => $data
  74. ];
  75. }
  76. public function changePermission(Request $request)
  77. {
  78. DB::beginTransaction();
  79. try{
  80. $role = Role::where('id', $request->id)->first();
  81. $permission = array_column(json_decode($request->permissions), 'id');
  82. $role->syncPermissions($permission);
  83. DB::commit();
  84. return $this->responseData(200, '更新成功');
  85. } catch(\Exception $e) {
  86. DB::rollBack();
  87. return $this->responseData(400, '更新失败'.$e->getMessage());
  88. }
  89. }
  90. }