PackageController.php 2.2 KB

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