PackageController.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace App\Http\Controllers\api\thirdPart\weight;
  3. use App\Http\Controllers\Controller;
  4. use App\Services\LogService;
  5. use App\Services\weight\HaoChuangWeightService;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Carbon;
  8. use Illuminate\Support\Facades\Validator;
  9. class PackageController extends Controller
  10. {
  11. public function new_(Request $requestInitial)
  12. {
  13. $request = [];
  14. foreach ($requestInitial->all() as $k => $v) {
  15. $request[strtolower($k)] = $v;
  16. }
  17. $weight_at = Carbon::now()->format(Carbon::DEFAULT_TO_STRING_FORMAT);
  18. $errors = $this->validatorWeight($request)->errors();
  19. if (count($errors) > 0) {
  20. app('LogService')->log(__METHOD__, 'error' . __FUNCTION__, json_encode($request) . '||' . json_encode($errors), null);
  21. return json_encode(["msg" => $errors, "code" => 500, "data" => null]);
  22. }
  23. $request['weight_at'] = $weight_at;
  24. /** @var HaoChuangWeightService $service */
  25. $service = app(HaoChuangWeightService::class);
  26. $response = $service->new($request);
  27. if ($response['code'] == 500)
  28. LogService::log(__CLASS__, $service->name, '称重失败!' . json_encode($request['msg'] ?? '') . json_encode($request));
  29. else
  30. LogService::log(__CLASS__, $service->name, '称重成功!' . json_encode($request));
  31. return json_encode($response);
  32. }
  33. public function validatorWeight(array $request): \Illuminate\Contracts\Validation\Validator
  34. {
  35. return Validator::make($request, [
  36. 'id' => ['nullable', 'max:30'],
  37. 'barcode' => ['required', 'max:191'],
  38. 'weight' => ['required', 'numeric', 'min:0'],
  39. 'length' => ['nullable', 'numeric', 'min:0'],
  40. 'width' => ['nullable', 'numeric', 'min:0'],
  41. 'height' => ['nullable', 'numeric', 'min:0'],
  42. ], [
  43. 'required' => ':attribute 为必填项',
  44. 'max' => ':attribute 字符过多或数值过大',
  45. 'min' => ':attribute 不得为负',
  46. 'numeric' => ':attribute 应为数字',
  47. ], []);
  48. }
  49. }