LogisticController.php 4.0 KB

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