LogisticController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 view("exception.authority"); }
  20. $logistics=Logistic::orderBy('id','desc')->paginate(35);
  21. return view('maintenance.logistic.index',['logistics'=>$logistics]);
  22. }
  23. public function create()
  24. {
  25. if(!Gate::allows('承运商-录入')){ return view("exception.authority"); }
  26. return view('maintenance.logistic.create');
  27. }
  28. public function store(Request $request)
  29. {
  30. if(!Gate::allows('承运商-录入')){ return view("exception.authority"); }
  31. $this->validatorCreate($request->all())->validate();
  32. $request->offsetSet("is_bunched",$request->input("is_bunched") ? 'Y' : 'N');
  33. $logistic=new Logistic($request->all());
  34. $logistic->save();
  35. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  36. return redirect('maintenance/logistic/create')->with('successTip',"成功录入承运商“{$request->input('name')}”");
  37. }
  38. protected function validatorCreate(array $data)
  39. {
  40. return Validator::make($data, [
  41. 'type' => ["required"],
  42. 'name' => ['required', 'string', 'max:50', 'unique:logistics'],
  43. 'code' => ['nullable', 'string', 'max:50', 'unique:logistics,code'],
  44. 'delivery_fee' => ['nullable', 'numeric', 'min:0'],
  45. ]);
  46. }
  47. protected function validatorUpdate(array $data,$id)
  48. {
  49. return Validator::make($data, [
  50. 'type' => ["required"],
  51. 'name' => ['required', 'string', 'max:50',"unique:logistics,name,$id"],
  52. 'code' => ['nullable', 'string', 'max:50',"unique:logistics,code,$id"],
  53. 'delivery_fee' => ['nullable', 'numeric', 'min:0'],
  54. ]);
  55. }
  56. /**
  57. * Display the specified resource.
  58. *
  59. * @param Logistic $logistic
  60. * @return Response
  61. */
  62. public function show(Logistic $logistic)
  63. {
  64. //
  65. }
  66. /**
  67. * Show the form for editing the specified resource.
  68. *
  69. * @param Logistic $logistic
  70. * @return Response
  71. */
  72. public function edit(Logistic $logistic)
  73. {
  74. if(!Gate::allows('承运商-编辑')){ return view("exception.authority"); }
  75. return view('maintenance.logistic.edit',['logistic'=>$logistic]);
  76. }
  77. public function update(Request $request, Logistic $logistic)
  78. {
  79. if(!Gate::allows('承运商-编辑')){ return redirect(url('/')); }
  80. $this->validatorUpdate($request->all(),$logistic->id)->validate();
  81. $request->offsetSet("is_bunched",$request->input("is_bunched") ? 'Y' : 'N');
  82. $logistic->fill($request->all());
  83. $logistic->update();
  84. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  85. return redirect('maintenance/logistic/')->with('successTip',"成功修改承运商“{$logistic['name']}”!");
  86. }
  87. /**
  88. * Remove the specified resource from storage.
  89. *
  90. * @param Logistic $logistic
  91. * @return array|Response
  92. * @throws Exception
  93. */
  94. public function destroy(Logistic $logistic)
  95. {
  96. if(!Gate::allows('承运商-删除')){ return view("exception.authority"); }
  97. app('LogService')->log(__METHOD__,__FUNCTION__,$logistic->toJson(),Auth::user()['id']);
  98. $re=$logistic->delete();
  99. return ['success'=>$re];
  100. }
  101. public function get()
  102. {
  103. $type = \request("type");
  104. $column = ['id','name'];
  105. if (!$type)$column[] = "type";
  106. $logistics = app("LogisticService")->getSelection($column,$type);
  107. return ["success"=>true,"data"=>$logistics];
  108. }
  109. }