CommodityController.php 6.1 KB

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