UserOwnerGroupController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Http\Response;
  5. use Illuminate\Support\Facades\Gate;
  6. use Illuminate\Support\Facades\Validator;
  7. class UserOwnerGroupController extends Controller
  8. {
  9. /**
  10. * Display a listing of the resource.
  11. *
  12. * @return Response
  13. */
  14. public function index()
  15. {
  16. if(!Gate::allows('项目组-查询')){ return redirect('denied'); }
  17. $groups = app('UserOwnerGroupService')->paginate();
  18. return response()->view("maintenance.userOwnerGroup.index",compact("groups"));
  19. }
  20. /**
  21. * Show the form for creating a new resource.
  22. *
  23. * @return Response
  24. */
  25. public function create()
  26. {
  27. if(!Gate::allows('项目组-录入')){ return redirect('denied'); }
  28. return response()->view("maintenance.userOwnerGroup.create");
  29. }
  30. /**
  31. * Store a newly created resource in storage.
  32. *
  33. * @param Request $request
  34. * @return Response
  35. */
  36. public function store(Request $request)
  37. {
  38. if(!Gate::allows('项目组-录入')){ return redirect('denied'); }
  39. $this->validator($request->input())->validate();
  40. app('UserOwnerGroupService')->create([
  41. "name"=>$request->input("name"),
  42. ]);
  43. return response()->redirectTo("maintenance/userOwnerGroup")->with("successTip","成功创建项目组“".$request->input("name")."”");
  44. }
  45. /**
  46. * Show the form for editing the specified resource.
  47. *
  48. * @param int $id
  49. * @return Response
  50. */
  51. public function edit($id)
  52. {
  53. if(!Gate::allows('项目组-编辑')){ return redirect('denied'); }
  54. $group = app('UserOwnerGroupService')->find($id);
  55. return response()->view('maintenance.userOwnerGroup.create',compact("group"));
  56. }
  57. /**
  58. * Update the specified resource in storage.
  59. *
  60. * @param Request $request
  61. * @param int $id
  62. * @return Response
  63. */
  64. public function update(Request $request, $id)
  65. {
  66. if(!Gate::allows('项目组-编辑')){ return redirect('denied'); }
  67. $this->validator($request->input(),$id)->validate();
  68. $result = app('UserOwnerGroupService')->update(["id"=>$id],[
  69. "name"=>$request->input("name"),
  70. ]);
  71. if ($result == 1){
  72. return response()->redirectTo("maintenance/userOwnerGroup")->with("successTip","成功修改项目组“".$request->input("name")."”的信息");
  73. }
  74. return response()->view("exception.default",["code"=>"509"]);
  75. }
  76. /**
  77. * Remove the specified resource from storage.
  78. *
  79. * @param int $id
  80. * @return array
  81. */
  82. public function destroy($id)
  83. {
  84. if(!Gate::allows('项目组-删除')){ return ["success"=>false,"data"=>"无权操作!"]; }
  85. $result = app('UserOwnerGroupService')->destroy($id);
  86. if ($result == 1)return ["success"=>true];
  87. return ["success"=>false,"data"=>"删除了“".$result."”行"];
  88. }
  89. private function validator(array $params, $id = null)
  90. {
  91. return Validator::make($params,[
  92. 'name'=>['required',$id?"unique:user_owner_groups,name,$id":'unique:user_owner_groups,name','max:20'],
  93. ],[
  94. 'required'=>':attribute 为必填项',
  95. 'max'=>':attribute 字符过多或输入值过大',
  96. 'unique'=>':attribute 已存在',
  97. ],[
  98. 'name'=>'项目组名称',
  99. ]);
  100. }
  101. }