CommodityController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Commodity;
  4. use App\Imports\CommodityImport;
  5. use Exception;
  6. use Illuminate\Database\Eloquent\Builder;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Http\Response;
  9. use Illuminate\Support\Facades\Auth;
  10. use Illuminate\Support\Facades\Gate;
  11. use Illuminate\Support\Facades\Validator;
  12. use Maatwebsite\Excel\Facades\Excel;
  13. class CommodityController extends Controller
  14. {
  15. /**
  16. * Display a listing of the resource.
  17. *
  18. * @return Response
  19. */
  20. public function index()
  21. {
  22. if(!Gate::allows('商品信息-查询')){ return redirect(url('/')); }
  23. $commodities=Commodity::orderBy('id','desc')->paginate(50);
  24. return view('maintenance.commodity.index',['commodities'=>$commodities]);
  25. }
  26. /**
  27. * Show the form for creating a new resource.
  28. *
  29. * @return Response
  30. */
  31. public function create()
  32. {
  33. if(!Gate::allows('商品信息-录入')){ return redirect(url('/')); }
  34. return view('maintenance.commodity.create');
  35. }
  36. /**
  37. * Store a newly created resource in storage.
  38. *
  39. * @param Request $request
  40. * @return Response
  41. */
  42. public function store(Request $request)
  43. {
  44. if(!Gate::allows('商品信息-录入')){ return redirect(url('/')); }
  45. $this->validatorCreate($request->all())->validate();
  46. $commodity=new Commodity($request->all());
  47. $commodity->save();
  48. $commodity->newBarcode($request->input('barcode'));
  49. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  50. return redirect('maintenance/commodity/create')->with('successTip',"成功录入商品信息:“{$request->input('name')}”");
  51. }
  52. protected function validatorCreate(array $data)
  53. {
  54. return Validator::make($data, [
  55. 'name' => ['required', 'string', 'max:50'],
  56. // 'barcode' => ['required', 'string', 'max:50', 'unique:commodities'],
  57. ]);
  58. }
  59. protected function validatorUpdate(array $data)
  60. {
  61. return Validator::make($data, [
  62. 'name' => ['required', 'string', 'max:50'],
  63. 'barcode' => ['required', 'string', 'max:50'],
  64. ]);
  65. }
  66. /**
  67. * Display the specified resource.
  68. *
  69. * @param Commodity $commodity
  70. * @return Response
  71. */
  72. public function show(Commodity $commodity)
  73. {
  74. //
  75. }
  76. /**
  77. * Show the form for editing the specified resource.
  78. *
  79. * @param Commodity $commodity
  80. * @return Response
  81. */
  82. public function edit(Commodity $commodity)
  83. {
  84. if(!Gate::allows('商品信息-编辑')){ return redirect(url('/')); }
  85. return view('maintenance.commodity.edit',['commodity'=>$commodity]);
  86. }
  87. /**
  88. * Update the specified resource in storage.
  89. *
  90. * @param Request $request
  91. * @param Commodity $commodity
  92. * @return Response
  93. */
  94. public function update(Request $request, Commodity $commodity)
  95. {
  96. if(!Gate::allows('商品信息-编辑')){ return redirect(url('/')); }
  97. $this->validatorUpdate($request->all())->validate();
  98. $commodity->fill($request->all());
  99. $commodity->update();
  100. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  101. return redirect('maintenance/commodity/')->with('successTip',"成功修改商品信息:“{$commodity['name']}”!");
  102. }
  103. /**
  104. * Remove the specified resource from storage.
  105. *
  106. * @param Commodity $commodity
  107. * @return array|Response
  108. * @throws Exception
  109. */
  110. public function destroy(Commodity $commodity)
  111. {
  112. if(!Gate::allows('商品信息-删除')){ return redirect(url('/')); }
  113. $this->log(__METHOD__,__FUNCTION__,$commodity->toJson(),Auth::user()['id']);
  114. $re=$commodity->delete();
  115. return ['success'=>$re];
  116. }
  117. public function import()
  118. {
  119. return view('maintenance.commodity.import');
  120. }
  121. public function importExcel(Request $request)
  122. {
  123. $isOverride = $request->input('isOverride');
  124. try{
  125. ini_set('max_execution_time',2500);
  126. ini_set('memory_limit','1526M');
  127. $extension=$request->file()['file']->getClientOriginalExtension();
  128. $extension[0] = strtoupper($extension[0]);
  129. Excel::import(new CommodityImport($isOverride), $request->file()['file']->path(),null,$extension);
  130. return '<h1 class="text-success">导入成功</h1>';
  131. }catch (Exception $e){
  132. if(strstr($e->getMessage(),'No ReaderType')){return '<h1 class="text-danger">没有上传写权限,请修改php.ini 对应的upload_tmp_dir 目录或其权限</h1>'.$e->getMessage();}
  133. if(strstr($e->getMessage(),'SQLSTATE')){return '<h1 class="text-danger">数据库插入错误,数据不支持,可能有重复或异常字符</h1>'.$e->getMessage();}
  134. if(strstr(strtolower($e->getMessage()),'sku')){return '<h1 class="text-danger">请在第一行将 商品编码 字段名改成“SKU”,不支持中文字段名,并且必须有该列</h1>'.$e->getMessage();}
  135. if(strstr(strtolower($e->getMessage()),'name')){return '<h1 class="text-danger">请在第一行将 商品名称 字段名改成“name”,不支持中文字段名,并且必须有该列</h1>'.$e->getMessage();}
  136. if(strstr(strtolower($e->getMessage()),'barcode')){return '<h1 class="text-danger">请在第一行将 商品条码 字段名改成“barcode”,不支持中文字段名,并且必须有该列</h1>'.$e->getMessage();}
  137. if(strstr(strtolower($e->getMessage()),'owner')){return '<h1 class="text-danger">请在第一行将 货主 字段名改成“owner”,不支持中文字段名,并且必须有该列</h1>'.$e->getMessage();}
  138. return '<h1 class="text-danger">失败</h1>'.$e->getMessage();
  139. }
  140. }
  141. public function apiGetCommodityByBarcode(Request $request)
  142. {
  143. $barcode=$request->input('barcode');
  144. $name = '';
  145. if($barcode){
  146. $commodity=Commodity::whereHas('barcodes', function (Builder $query)use($barcode){
  147. $query->where('code',$barcode);
  148. })->first();
  149. if($commodity&&$commodity['name']) $name=$commodity['name'];
  150. }
  151. return ['success'=>'true','name'=>$name];
  152. }
  153. }