create(['content' => json_encode($request->all()),'interface' => 'GoodScan']); app('LogService')->log(__METHOD__, 'GoodScan weightApi add' . __FUNCTION__, json_encode($request->getContent())); $requestInput = []; foreach ($request->input() as $key => $item) { $requestInput[strtolower($key)] = $item; } $errors = $this->validatorWeight($requestInput)->errors(); // 参数校验 $weight = $requestInput['weight'] ?? ''; if ($weight == '-9.9') { // 称重异常校验 app('LogService')->log(__METHOD__, 'GoodScan weightApi (Error)' . __FUNCTION__, '异方接口称重伤上传异常[异常值为-9.9,电子秤故障或未连接]' . json_encode($request->getContent())); return json_encode(['code' => 500, 'error' => 'weight=-9.9'], JSON_UNESCAPED_UNICODE); } if (count($errors) > 0) { app('LogService')->log(__METHOD__, 'error' . __FUNCTION__, json_encode($request->getContent()) . '||' . json_encode($errors), null); return json_encode(['code' => 500, 'error' => $errors], JSON_UNESCAPED_UNICODE); } /** @var GoodScanWeightService $serivce */ $service = app(GoodScanWeightService::class); $response = $service->new($requestInput); if ($response['code'] == 500) LogService::log(__CLASS__, __METHOD__, '称重失败:' . json_encode($response['error'])); else if ($response['code'] == 0) LogService::log(__CLASS__, __METHOD__, '称重成功:' . json_encode($requestInput)); return json_encode($response, JSON_UNESCAPED_UNICODE); } public function validatorWeight(array $request): \Illuminate\Contracts\Validation\Validator { return Validator::make($request, [ 'code' => ['required', 'max:191'], 'l' => ['required', 'numeric', 'min:0'], 'w' => ['required', 'numeric', 'min:0'], 'h' => ['required', 'numeric', 'min:0'], 'weight' => ['required', 'numeric', 'min:0'], 'hid' => ['required', 'string', 'min:0'], 'picture' => ['nullable'], ], [ 'required' => ':attribute 为必填项', 'max' => ':attribute 字符过多或数值过大', 'min' => ':attribute 不得为负', 'numeric' => ':attribute 应为数字', 'string' => ':attribute 应为字符串', ], []); } }