PackageController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. if(mb_stristr($value,'-')){
  34. $arr = [];
  35. preg_match('/[A-Z|a-z|\d]+/',$value,$arr);
  36. $params['code'] = $arr[0];
  37. }else{
  38. $params['code'] = $value;
  39. }
  40. break;
  41. case '2':
  42. $params['weight'] = $value;
  43. break;
  44. case '3':
  45. $params['weight_at'] = $value;
  46. break;
  47. default:
  48. break;
  49. }
  50. }
  51. return $params;
  52. }
  53. public function getWeightValue($params)
  54. {
  55. $value = $this->getValue($this->weight, $params);
  56. return str_replace('_', '.', $value);
  57. }
  58. // 信息返回
  59. // 返回称重成功信息
  60. public function getSuccessMessage($params, $orderPackage): array
  61. {
  62. return ['success' => true, 'message' => '称重成功'];
  63. }
  64. // 返回包裹未找到异常
  65. public function getNotFindOrderPackageMessage($params, $orderPackage): string
  66. {
  67. return json_encode(['success' => false, 'message' => '未找打包裹信息', JSON_UNESCAPED_UNICODE]);
  68. }
  69. // 返回富勒信息未找到异常
  70. public function getNotFindOrderHeaderMessage($params, $orderPackage): string
  71. {
  72. return json_encode(['success' => false, 'message' => '富勒信息未找到'], JSON_UNESCAPED_UNICODE);
  73. }
  74. // 返回称重下发错误
  75. public function getWeightMessage($orderPackage, $e)
  76. {
  77. return json_encode(['success' => false, 'message' => $e->getMessage], JSON_UNESCAPED_UNICODE);
  78. }
  79. public function validator(Request $request): array
  80. {
  81. $params = $this->conversionRequest($request);
  82. $errors = [];
  83. $weight = $this->getWeightValue($params);
  84. if (empty($weight) || $weight == '') {
  85. $errors['weight'] = '称重重量为空';
  86. }
  87. $code = $this->getCodeValue($params);
  88. $code = trim($code, "'");
  89. if (empty($code) || $code == '') {
  90. $errors['code'] = '快递单号为空';
  91. }
  92. $hid = $this->getValue($this->hid, $params);
  93. if (empty($hid) || $hid == '') {
  94. $errors['hid'] = '称重设备号不能为空';
  95. }
  96. return $errors;
  97. }
  98. }