| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
- namespace App\Http\Controllers;
- use App\Logistic;
- use Exception;
- use Illuminate\Http\Request;
- use Illuminate\Http\Response;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\Gate;
- use Illuminate\Support\Facades\Validator;
- use Illuminate\Support\Str;
- class LogisticController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return Response
- */
- public function index()
- {
- if(!Gate::allows('承运商-查询')){ return view("exception.authority"); }
- $logistics=Logistic::orderBy('id','desc')->paginate(35);
- return view('maintenance.logistic.index',['logistics'=>$logistics]);
- }
- public function create()
- {
- if(!Gate::allows('承运商-录入')){ return view("exception.authority"); }
- return view('maintenance.logistic.create');
- }
- public function store(Request $request)
- {
- if(!Gate::allows('承运商-录入')){ return view("exception.authority"); }
- $this->validatorCreate($request->all())->validate();
- $request->offsetSet("is_bunched",$request->input("is_bunched") ? 'Y' : 'N');
- $logistic=new Logistic($request->all());
- $logistic->save();
- app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
- return redirect('maintenance/logistic/create')->with('successTip',"成功录入承运商“{$request->input('name')}”");
- }
- protected function validatorCreate(array $data)
- {
- return Validator::make($data, [
- 'type' => ["required"],
- 'name' => ['required', 'string', 'max:50', 'unique:logistics'],
- 'code' => ['nullable', 'string', 'max:50', 'unique:logistics,code'],
- 'delivery_fee' => ['nullable', 'numeric', 'min:0'],
- ]);
- }
- protected function validatorUpdate(array $data,$id)
- {
- return Validator::make($data, [
- 'type' => ["required"],
- 'name' => ['required', 'string', 'max:50',"unique:logistics,name,$id"],
- 'code' => ['nullable', 'string', 'max:50',"unique:logistics,code,$id"],
- 'delivery_fee' => ['nullable', 'numeric', 'min:0'],
- ]);
- }
- public function edit(Logistic $logistic)
- {
- if(!Gate::allows('承运商-编辑')){ return view("exception.authority"); }
- $logistic = $logistic->toArray();
- if ($logistic["tag"]){
- $tag = explode(",",$logistic["tag"]);
- $map = array_flip(Logistic::TAGS);
- foreach ($tag as &$t)$t = $map[$t];
- $logistic["tag"] = implode(",",$tag);
- }
- $key = 'LogisticAll_'.implode("",["id","name","tag"]).Str::studly("物流");
- Cache::pull($key);
- return view('maintenance.logistic.edit',['logistic'=>$logistic]);
- }
- public function update(Request $request, Logistic $logistic)
- {
- if(!Gate::allows('承运商-编辑')){ return redirect(url('/')); }
- $this->validatorUpdate($request->all(),$logistic->id)->validate();
- $request->offsetSet("is_bunched",$request->input("is_bunched") ? 'Y' : 'N');
- $logistic->fill($request->all());
- $logistic->update();
- app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
- return redirect('maintenance/logistic/')->with('successTip',"成功修改承运商“{$logistic['name']}”!");
- }
- public function destroy(Logistic $logistic)
- {
- if(!Gate::allows('承运商-删除')){ return view("exception.authority"); }
- app('LogService')->log(__METHOD__,__FUNCTION__,$logistic->toJson(),Auth::user()['id']);
- $re=$logistic->delete();
- return ['success'=>$re];
- }
- public function get()
- {
- $type = \request("type");
- $column = ['id','name'];
- if (!$type)$column[] = "type";
- $logistics = app("LogisticService")->getSelection($column,$type);
- return ["success"=>true,"data"=>$logistics];
- }
- }
|