LogisticController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Logistic;
  4. use App\Services\UserService;
  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 LogisticController extends Controller
  12. {
  13. /**
  14. * Display a listing of the resource.
  15. *
  16. * @return Response
  17. */
  18. public function index()
  19. {
  20. if(!Gate::allows('物流公司-查询')){ return redirect(url('/')); }
  21. $logistics=Logistic::orderBy('id','desc')->paginate(35);
  22. return view('maintenance.logistic.index',['logistics'=>$logistics]);
  23. }
  24. /**
  25. * Show the form for creating a new resource.
  26. *
  27. * @return Response
  28. */
  29. public function create()
  30. {
  31. if(!Gate::allows('物流公司-录入')){ return redirect(url('/')); }
  32. return view('maintenance.logistic.create');
  33. }
  34. /**
  35. * Store a newly created resource in storage.
  36. *
  37. * @param Request $request
  38. * @return Response
  39. */
  40. public function store(Request $request)
  41. {
  42. if(!Gate::allows('物流公司-录入')){ return redirect(url('/')); }
  43. $this->validatorCreate($request->all())->validate();
  44. $logistic=new Logistic($request->all());
  45. $logistic->save();
  46. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  47. return redirect('maintenance/logistic/create')->with('successTip',"成功录入物流公司“{$request->input('name')}”");
  48. }
  49. protected function validatorCreate(array $data)
  50. {
  51. return Validator::make($data, [
  52. 'type' => ["required"],
  53. 'name' => ['required', 'string', 'max:50', 'unique:logistics'],
  54. 'code' => ['nullable', 'string', 'max:50', 'unique:logistics,code'],
  55. 'delivery_fee' => ['nullable', 'numeric', 'min:0'],
  56. ]);
  57. }
  58. protected function validatorUpdate(array $data,$id)
  59. {
  60. return Validator::make($data, [
  61. 'type' => ["required"],
  62. 'name' => ['required', 'string', 'max:50',"unique:logistics,name,$id"],
  63. 'code' => ['nullable', 'string', 'max:50',"unique:logistics,code,$id"],
  64. 'delivery_fee' => ['nullable', 'numeric', 'min:0'],
  65. ]);
  66. }
  67. /**
  68. * Display the specified resource.
  69. *
  70. * @param Logistic $logistic
  71. * @return Response
  72. */
  73. public function show(Logistic $logistic)
  74. {
  75. //
  76. }
  77. /**
  78. * Show the form for editing the specified resource.
  79. *
  80. * @param Logistic $logistic
  81. * @return Response
  82. */
  83. public function edit(Logistic $logistic)
  84. {
  85. if(!Gate::allows('物流公司-编辑')){ return redirect(url('/')); }
  86. return view('maintenance.logistic.edit',['logistic'=>$logistic]);
  87. }
  88. public function update(Request $request, Logistic $logistic)
  89. {
  90. if(!Gate::allows('物流公司-编辑')){ return redirect(url('/')); }
  91. $this->validatorUpdate($request->all(),$logistic->id)->validate();
  92. $logistic->fill($request->all());
  93. $logistic->update();
  94. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  95. return redirect('maintenance/logistic/')->with('successTip',"成功修改物流公司“{$logistic['name']}”!");
  96. }
  97. /**
  98. * Remove the specified resource from storage.
  99. *
  100. * @param Logistic $logistic
  101. * @return array|Response
  102. * @throws Exception
  103. */
  104. public function destroy(Logistic $logistic)
  105. {
  106. if(!Gate::allows('物流公司-删除')){ return redirect(url('/')); }
  107. app('LogService')->log(__METHOD__,__FUNCTION__,$logistic->toJson(),Auth::user()['id']);
  108. $re=$logistic->delete();
  109. return ['success'=>$re];
  110. }
  111. public function get()
  112. {
  113. $type = \request("type");
  114. $column = ['id','name'];
  115. if (!$type)$column[] = "type";
  116. $logistics = app("LogisticService")->getSelection($column,$type);
  117. return ["success"=>true,"data"=>$logistics];
  118. }
  119. }