LogisticController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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\Cache;
  9. use Illuminate\Support\Facades\Gate;
  10. use Illuminate\Support\Facades\Validator;
  11. use Illuminate\Support\Str;
  12. class LogisticController extends Controller
  13. {
  14. /**
  15. * Display a listing of the resource.
  16. *
  17. * @return Response
  18. */
  19. public function index()
  20. {
  21. if(!Gate::allows('承运商-查询')){ return view("exception.authority"); }
  22. $logistics=Logistic::orderBy('id','desc')->paginate(35);
  23. return view('maintenance.logistic.index',['logistics'=>$logistics]);
  24. }
  25. public function create()
  26. {
  27. if(!Gate::allows('承运商-录入')){ return view("exception.authority"); }
  28. return view('maintenance.logistic.create');
  29. }
  30. public function store(Request $request)
  31. {
  32. if(!Gate::allows('承运商-录入')){ return view("exception.authority"); }
  33. $this->validatorCreate($request->all())->validate();
  34. $request->offsetSet("is_bunched",$request->input("is_bunched") ? 'Y' : 'N');
  35. $logistic=new Logistic($request->all());
  36. $logistic->save();
  37. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  38. return redirect('maintenance/logistic/create')->with('successTip',"成功录入承运商“{$request->input('name')}”");
  39. }
  40. protected function validatorCreate(array $data)
  41. {
  42. return Validator::make($data, [
  43. 'type' => ["required"],
  44. 'name' => ['required', 'string', 'max:50', 'unique:logistics'],
  45. 'code' => ['nullable', 'string', 'max:50', 'unique:logistics,code'],
  46. 'delivery_fee' => ['nullable', 'numeric', 'min:0'],
  47. ]);
  48. }
  49. protected function validatorUpdate(array $data,$id)
  50. {
  51. return Validator::make($data, [
  52. 'type' => ["required"],
  53. 'name' => ['required', 'string', 'max:50',"unique:logistics,name,$id"],
  54. 'code' => ['nullable', 'string', 'max:50',"unique:logistics,code,$id"],
  55. 'delivery_fee' => ['nullable', 'numeric', 'min:0'],
  56. ]);
  57. }
  58. public function edit(Logistic $logistic)
  59. {
  60. if(!Gate::allows('承运商-编辑')){ return view("exception.authority"); }
  61. $logistic = $logistic->toArray();
  62. if ($logistic["tag"]){
  63. $tag = explode(",",$logistic["tag"]);
  64. $map = array_flip(Logistic::TAGS);
  65. foreach ($tag as &$t)$t = $map[$t];
  66. $logistic["tag"] = implode(",",$tag);
  67. }
  68. $key = 'LogisticAll_'.implode("",["id","name","tag"]).Str::studly("物流");
  69. Cache::pull($key);
  70. return view('maintenance.logistic.edit',['logistic'=>$logistic]);
  71. }
  72. public function update(Request $request, Logistic $logistic)
  73. {
  74. if(!Gate::allows('承运商-编辑')){ return redirect(url('/')); }
  75. $this->validatorUpdate($request->all(),$logistic->id)->validate();
  76. $request->offsetSet("is_bunched",$request->input("is_bunched") ? 'Y' : 'N');
  77. $logistic->fill($request->all());
  78. $logistic->update();
  79. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  80. return redirect('maintenance/logistic/')->with('successTip',"成功修改承运商“{$logistic['name']}”!");
  81. }
  82. public function destroy(Logistic $logistic)
  83. {
  84. if(!Gate::allows('承运商-删除')){ return view("exception.authority"); }
  85. app('LogService')->log(__METHOD__,__FUNCTION__,$logistic->toJson(),Auth::user()['id']);
  86. $re=$logistic->delete();
  87. return ['success'=>$re];
  88. }
  89. public function get()
  90. {
  91. $type = \request("type");
  92. $column = ['id','name'];
  93. if (!$type)$column[] = "type";
  94. $logistics = app("LogisticService")->getSelection($column,$type);
  95. return ["success"=>true,"data"=>$logistics];
  96. }
  97. }