AuthorityController.php 5.7 KB

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