| Code Coverage
| Classes and Traits | Functions and Methods | Lines
Total | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 12 | CRAP | 0.00% covered (danger) | 0.00% | 0 / 50
CommodityController | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 12 | 812.00 | 0.00% covered (danger) | 0.00% | 0 / 50
index | 0.00% covered (danger) | 0.00% | 0 / 1 | 6.00 | 0.00% covered (danger) | 0.00% | 0 / 3
create | 0.00% covered (danger) | 0.00% | 0 / 1 | 6.00 | 0.00% covered (danger) | 0.00% | 0 / 2
store | 0.00% covered (danger) | 0.00% | 0 / 1 | 6.00 | 0.00% covered (danger) | 0.00% | 0 / 6
validatorCreate | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 2
validatorUpdate | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 2
show | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 1
edit | 0.00% covered (danger) | 0.00% | 0 / 1 | 6.00 | 0.00% covered (danger) | 0.00% | 0 / 2
update | 0.00% covered (danger) | 0.00% | 0 / 1 | 6.00 | 0.00% covered (danger) | 0.00% | 0 / 6
destroy | 0.00% covered (danger) | 0.00% | 0 / 1 | 6.00 | 0.00% covered (danger) | 0.00% | 0 / 4
import | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 1
importExcel | 0.00% covered (danger) | 0.00% | 0 / 1 | 72.00 | 0.00% covered (danger) | 0.00% | 0 / 15
apiGetCommodityByBarcode | 0.00% covered (danger) | 0.00% | 0 / 1 | 20.00 | 0.00% covered (danger) | 0.00% | 0 / 6

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