PackageController.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Http\Controllers\api\thirdPart\goodscan;
  3. use App\Services\LogService;
  4. use App\Services\weight\GoodScanWeightService;
  5. use App\WeightLog;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Validator;
  8. class PackageController
  9. {
  10. public function new_(Request $request)
  11. {
  12. WeightLog::query()->create(['content' => json_encode($request->all()),'interface' => 'GoodScan']);
  13. app('LogService')->log(__METHOD__, 'GoodScan weightApi add' . __FUNCTION__, json_encode($request->getContent()));
  14. $requestInput = [];
  15. foreach ($request->input() as $key => $item) {
  16. $requestInput[strtolower($key)] = $item;
  17. }
  18. $errors = $this->validatorWeight($requestInput)->errors(); // 参数校验
  19. $weight = $requestInput['weight'] ?? '';
  20. if ($weight == '-9.9') { // 称重异常校验
  21. app('LogService')->log(__METHOD__, 'GoodScan weightApi (Error)' . __FUNCTION__, '异方接口称重伤上传异常[异常值为-9.9,电子秤故障或未连接]' . json_encode($request->getContent()));
  22. return json_encode(['code' => 500, 'error' => 'weight=-9.9'], JSON_UNESCAPED_UNICODE);
  23. }
  24. if (count($errors) > 0) {
  25. app('LogService')->log(__METHOD__, 'error' . __FUNCTION__, json_encode($request->getContent()) . '||' . json_encode($errors), null);
  26. return json_encode(['code' => 500, 'error' => $errors], JSON_UNESCAPED_UNICODE);
  27. }
  28. /** @var GoodScanWeightService $serivce */
  29. $service = app(GoodScanWeightService::class);
  30. $response = $service->new($requestInput);
  31. if ($response['code'] == 500)
  32. LogService::log(__CLASS__, __METHOD__, '称重失败:' . json_encode($response['error']));
  33. else if ($response['code'] == 0)
  34. LogService::log(__CLASS__, __METHOD__, '称重成功:' . json_encode($requestInput));
  35. return json_encode($response, JSON_UNESCAPED_UNICODE);
  36. }
  37. public function validatorWeight(array $request): \Illuminate\Contracts\Validation\Validator
  38. {
  39. return Validator::make($request, [
  40. 'code' => ['required', 'max:191'],
  41. 'l' => ['required', 'numeric', 'min:0'],
  42. 'w' => ['required', 'numeric', 'min:0'],
  43. 'h' => ['required', 'numeric', 'min:0'],
  44. 'weight' => ['required', 'numeric', 'min:0'],
  45. 'hid' => ['required', 'string', 'min:0'],
  46. 'picture' => ['nullable'],
  47. ], [
  48. 'required' => ':attribute 为必填项',
  49. 'max' => ':attribute 字符过多或数值过大',
  50. 'min' => ':attribute 不得为负',
  51. 'numeric' => ':attribute 应为数字',
  52. 'string' => ':attribute 应为字符串',
  53. ], []);
  54. }
  55. }