PackageController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace App\Http\Controllers\api\thirdPart\hengli;
  3. use App\Http\Controllers\api\thirdPart\weight\WeightBaseController;
  4. use Illuminate\Http\Request;
  5. class PackageController extends WeightBaseController
  6. {
  7. // 参数
  8. protected $weight = 'weight'; // 重量
  9. protected $length = 'length'; // 长
  10. protected $width = 'width'; // 宽
  11. protected $height = 'height'; // 高
  12. protected $code = 'code'; // 快递单号
  13. protected $weight_at = 'weight_at'; // 称重时间
  14. protected $hid = 'hid'; // 称重设备id
  15. protected $name = 'HengLi'; // 名称
  16. public function new_(Request $request)
  17. {
  18. app('LogService')->log(__METHOD__, $this->name, "记录上传日志:" . json_encode($request->all()) , null);
  19. return $this->new($request);
  20. }
  21. public function conversionRequest($request)
  22. {
  23. $params = [];
  24. $arr = $request->all();
  25. $data = array_key_first($arr);
  26. $data = explode(',', $data);
  27. foreach ($data as $key => $value) {
  28. switch ($key) {
  29. case '0':
  30. $params['hid'] = $value;
  31. break;
  32. case '1':
  33. $params['code'] = $this->processCodeStr($value);
  34. break;
  35. case '2':
  36. $params['weight'] = $value;
  37. break;
  38. case '3':
  39. $params['weight_at'] = $value;
  40. break;
  41. case '4':
  42. $params['length'] = $value;
  43. break;
  44. case '5':
  45. $params['width'] = $value;
  46. break;
  47. case '6':
  48. $params['height'] = $value;
  49. break;
  50. default:
  51. break;
  52. }
  53. }
  54. return $params;
  55. }
  56. public function getWeightValue($params)
  57. {
  58. $value = $this->getValue($this->weight, $params);
  59. return str_replace('_', '.', $value);
  60. }
  61. // 信息返回
  62. // 返回称重成功信息
  63. public function getSuccessMessage($params, $orderPackage): array
  64. {
  65. return ['success' => true, 'message' => '称重成功'];
  66. }
  67. // 返回包裹未找到异常
  68. public function getNotFindOrderPackageMessage($params, $orderPackage): string
  69. {
  70. return json_encode(['success' => false, 'message' => '未找打包裹信息', JSON_UNESCAPED_UNICODE]);
  71. }
  72. // 返回富勒信息未找到异常
  73. public function getNotFindOrderHeaderMessage($params, $orderPackage): string
  74. {
  75. return json_encode(['success' => false, 'message' => '富勒信息未找到'], JSON_UNESCAPED_UNICODE);
  76. }
  77. // 返回称重下发错误
  78. public function getWeightMessage($orderPackage, $e)
  79. {
  80. return json_encode(['success' => false, 'message' => $e->getMessage], JSON_UNESCAPED_UNICODE);
  81. }
  82. public function validator(Request $request): array
  83. {
  84. $params = $this->conversionRequest($request);
  85. $errors = [];
  86. $weight = $this->getWeightValue($params);
  87. if (empty($weight) || $weight == '') {
  88. $errors['weight'] = '称重重量为空';
  89. }
  90. $code = $this->getCodeValue($params);
  91. $code = trim($code, "'");
  92. if (empty($code) || $code == '') {
  93. $errors['code'] = '快递单号为空';
  94. }
  95. $hid = $this->getValue($this->hid, $params);
  96. if (empty($hid) || $hid == '') {
  97. $errors['hid'] = '称重设备号不能为空';
  98. }
  99. return $errors;
  100. }
  101. }