PackageController.php 2.6 KB

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