AuthorityController.php 5.6 KB

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