AuthorityController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Authority;
  4. use App\Owner;
  5. use Exception;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Http\Response;
  8. use Illuminate\Support\Facades\Auth;
  9. use Illuminate\Support\Facades\Gate;
  10. use Illuminate\Support\Facades\Validator;
  11. class AuthorityController extends Controller
  12. {
  13. /**
  14. * Display a listing of the resource.
  15. *
  16. * @return string
  17. */
  18. public function index()
  19. {
  20. /*if(!Gate::allows('权限-查询')){ return redirect(url('/')); }
  21. $authorities=Authority::orderBy('id','asc')->paginate(100);
  22. return view('maintenance.authority.index',['authorities'=>$authorities]);*/
  23. $authorities = app("AuthorityService")->format(Authority::query()->get());
  24. return view('maintenance.authority.index',['authorities'=>$authorities]);
  25. }
  26. /**
  27. * Show the form for creating a new resource.
  28. *
  29. * @return string
  30. */
  31. public function create()
  32. {
  33. if(!Gate::allows('权限-录入')){ return redirect(url('/')); }
  34. $owners=Owner::all();
  35. return view('maintenance.authority.create',compact('owners'));
  36. }
  37. // /**
  38. // * Store a newly created resource in storage.
  39. // *
  40. // * @param Request $request
  41. // * @return string
  42. // */
  43. // public function store(Request $request)
  44. // {
  45. // if(!Gate::allows('权限-录入')){ return redirect(url('/')); }
  46. // $inputs=$request->all();
  47. // $inputs['combinedName']=$request->input('name').'_'.$request->input('id_owner');
  48. // $this->validatorCreate($inputs)->validate();
  49. // $successName= $request->input('name')??'';
  50. // $inputs['name']=$inputs['combinedName'];
  51. //
  52. // if($request->input('id_owner')??''){
  53. // $owner=Owner::find($inputs['id_owner']);
  54. // if(isset($inputs['remark'])){
  55. // $inputs['remark'].="(key: {$inputs['combinedName']})";
  56. // }else{
  57. // $inputs['remark']="(key: {$inputs['combinedName']})";
  58. // }
  59. // $inputs['alias_name']=$request->input('name')."_(货主:$owner->name)";
  60. // $successName.="(货主:$owner->name)";
  61. // }else{
  62. // $inputs['alias_name']=$request->input('name');
  63. // }
  64. // $authority=new Authority($inputs);
  65. // $authority->save();
  66. //
  67. // app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  68. // return redirect('maintenance/authority/create')->with('successTip',"成功录入权限“{$successName}”");
  69. // }
  70. public function store(Request $request)
  71. {
  72. if(!Gate::allows('权限-录入')){ return redirect(url('/')); }
  73. $this->validatorCreate($request->all())->validate();
  74. $owner=Owner::find($request->input('id_owner'));
  75. $authority=new Authority(['alias_name'=>"(货主:{$owner['name']})",'name'=>"_{$owner['id']}",'remark'=>"(key: _{$owner['id']})"]);
  76. $authority->save();
  77. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  78. return redirect('maintenance/authority/create')->with('successTip',"成功录入权限: (货主:{$owner['name']})");
  79. }
  80. protected function validatorCreate(array $data)
  81. {
  82. $data['name']="_{$data['id_owner']}";
  83. return Validator::make($data, [
  84. 'id_owner' => ['required', 'exists:owners,id'],
  85. 'name' => ['unique:authorities,name'],
  86. ],[
  87. 'id_owner.required' => '必须选一个货主',
  88. 'id_owner.exists' => '当前货主不存在',
  89. 'name.unique' => '该货主对应权限已添加过',
  90. ]);
  91. }
  92. protected function validatorUpdate(array $data)
  93. {
  94. return Validator::make($data, [
  95. 'name' => ['required', 'string', 'max:50'],
  96. ]);
  97. }
  98. /**
  99. * Display the specified resource.
  100. *
  101. * @param Authority $authority
  102. * @return Response
  103. */
  104. public function show(Authority $authority)
  105. {
  106. //
  107. }
  108. /**
  109. * Show the form for editing the specified resource.
  110. *
  111. * @param \App\Http\Controllers\Authority $authority
  112. * @return Response
  113. */
  114. public function edit(Authority $authority)
  115. {
  116. if(!Gate::allows('权限-编辑')){ return redirect(url('/')); }
  117. $owners=Owner::all();
  118. return view('maintenance.authority.edit',compact('owners','authority'));
  119. }
  120. /**
  121. * Update the specified resource in storage.
  122. *
  123. * @param Request $request
  124. * @param \App\Http\Controllers\Authority $authority
  125. * @return Response
  126. */
  127. public function update(Request $request, Authority $authority)
  128. {
  129. if(!Gate::allows('权限-编辑')){ return redirect(url('/')); }
  130. $this->validatorUpdate($request->all())->validate();
  131. $authority->fill($request->all());
  132. $authority->update();
  133. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  134. return redirect('maintenance/authority/')->with('successTip',"成功修改权限“{$authority['name']}”!");
  135. }
  136. /**
  137. * Remove the specified resource from storage.
  138. *
  139. * @param \App\Http\Controllers\Authority $authority
  140. * @return array|Response
  141. * @throws Exception
  142. */
  143. public function destroy(Authority $authority)
  144. {
  145. if(!Gate::allows('权限-删除')){ return redirect(url('/')); }
  146. app('LogService')->log(__METHOD__,__FUNCTION__,$authority->toJson(),Auth::user()['id']);
  147. $re=$authority->delete();
  148. return ['success'=>$re];
  149. }
  150. }