ZopClient.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace App\library\zop;
  3. use App\Jobs\LogisticZopSync;
  4. use App\library\zop\ZopHttpUtil;
  5. use App\Services\LogService;
  6. use Illuminate\Support\Facades\Http;
  7. class ZopClient
  8. {
  9. private $zopProperties;
  10. private $httpClient;
  11. /**
  12. * ZopClient constructor.
  13. * @param $zopProperties
  14. */
  15. public function __construct($zopProperties)
  16. {
  17. $this->zopProperties = $zopProperties;
  18. $this->httpClient = new ZopHttpUtil();
  19. }
  20. public function execute($zopRequest)
  21. {
  22. if ($zopRequest->getBody() == null) {
  23. $url = $zopRequest->getUrl();
  24. $params = $zopRequest->getParams();
  25. $fixedParams = array();
  26. foreach ($params as $k => $v) {
  27. if (gettype($v) != "string") {
  28. $fixedParams += [$k => json_encode($v)];
  29. } else {
  30. $fixedParams += [$k => $v];
  31. }
  32. }
  33. $str_to_digest = "";
  34. foreach ($fixedParams as $k => $v) {
  35. $str_to_digest = $str_to_digest . $k . "=" . $v . "&";
  36. }
  37. $str_to_digest = substr($str_to_digest, 0, -1) . $this->zopProperties->getKey();
  38. $data_digest = base64_encode(md5($str_to_digest, TRUE));
  39. $headers = array(
  40. "Content-Type: application/x-www-form-urlencoded; charset=UTF-8",
  41. "x-companyid: " . $this->zopProperties->getCompanyid(),
  42. "x-datadigest: " . $data_digest
  43. );
  44. return $this->httpClient->post($url, $headers, http_build_query($fixedParams), 2000);
  45. } else {
  46. $url = $zopRequest->getUrl();
  47. $body = $zopRequest->getBody();
  48. $str_to_digest = $body . $this->zopProperties->getKey();
  49. $data_digest = base64_encode(md5($str_to_digest, TRUE));
  50. $response = Http::withHeaders([
  51. 'Content-Type' => 'application/json; charset=UTF-8',
  52. 'x-companyid' => $this->zopProperties->getCompanyid(),
  53. 'x-datadigest' => $data_digest,
  54. ])->withBody(json_encode((array)json_decode($body), JSON_UNESCAPED_UNICODE), 'application/json')->post($url);
  55. return $response->body();
  56. }
  57. }
  58. }