LogisticController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. $request->offsetSet("is_bunched",$request->input("is_bunched") ? 'Y' : 'N');
  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. $request->offsetSet("is_bunched",$request->input("is_bunched") ? 'Y' : 'N');
  93. $logistic->fill($request->all());
  94. $logistic->update();
  95. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  96. return redirect('maintenance/logistic/')->with('successTip',"成功修改物流公司“{$logistic['name']}”!");
  97. }
  98. /**
  99. * Remove the specified resource from storage.
  100. *
  101. * @param Logistic $logistic
  102. * @return array|Response
  103. * @throws Exception
  104. */
  105. public function destroy(Logistic $logistic)
  106. {
  107. if(!Gate::allows('物流公司-删除')){ return redirect(url('/')); }
  108. app('LogService')->log(__METHOD__,__FUNCTION__,$logistic->toJson(),Auth::user()['id']);
  109. $re=$logistic->delete();
  110. return ['success'=>$re];
  111. }
  112. public function get()
  113. {
  114. $type = \request("type");
  115. $column = ['id','name'];
  116. if (!$type)$column[] = "type";
  117. $logistics = app("LogisticService")->getSelection($column,$type);
  118. return ["success"=>true,"data"=>$logistics];
  119. }
  120. }