Library.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace App\Master\Framework\Library;
  3. use GuzzleHttp\Client;
  4. use GuzzleHttp\RequestOptions;
  5. use Hyperf\Guzzle\HandlerStackFactory;
  6. /**
  7. * 扩展返回工具
  8. */
  9. class Library
  10. {
  11. protected string $base_uri = 'http://127.0.0.1:9501';
  12. protected string $message = 'error';
  13. protected mixed $data = [];
  14. /**
  15. * post json 请求
  16. *
  17. * @param string $uri
  18. * @param array $params
  19. * @param array $header
  20. * @return \Psr\Http\Message\ResponseInterface
  21. * @throws \GuzzleHttp\Exception\GuzzleException
  22. */
  23. protected function postJson(string $uri, array $params = [], array $header = [])
  24. {
  25. $factory = new HandlerStackFactory();
  26. $stack = $factory->create();
  27. $client = new Client([
  28. // guzzle http里的配置信息
  29. 'base_uri' => $this->base_uri,
  30. 'handler' => $stack,
  31. 'timeout' => 5,
  32. // swoole的配置信息,内容会覆盖guzzle http里的配置信息
  33. 'swoole' => [
  34. 'timeout' => 10,
  35. 'socket_buffer_size' => 1024 * 1024 * 2,
  36. ],
  37. ]);
  38. return $client->post($uri, [
  39. RequestOptions::JSON => $params,
  40. RequestOptions::VERIFY => false,
  41. RequestOptions::HEADERS => array_merge(
  42. $header,
  43. [
  44. 'Content-Type' => 'application/json'
  45. ]
  46. ),
  47. ]);
  48. }
  49. protected function getJson(string $uri, array $params = [], array $header = [])
  50. {
  51. $factory = new HandlerStackFactory();
  52. $stack = $factory->create();
  53. $client = new Client([
  54. // guzzle http里的配置信息
  55. 'base_uri' => $this->base_uri,
  56. 'handler' => $stack,
  57. 'timeout' => 5,
  58. // swoole的配置信息,内容会覆盖guzzle http里的配置信息
  59. 'swoole' => [
  60. 'timeout' => 10,
  61. 'socket_buffer_size' => 1024 * 1024 * 2,
  62. ],
  63. ]);
  64. return $client->get($uri, [
  65. RequestOptions::QUERY => $params,
  66. RequestOptions::VERIFY => false,
  67. RequestOptions::HEADERS => array_merge(
  68. $header,
  69. [
  70. 'Content-Type' => 'application/json'
  71. ]
  72. ),
  73. ]);
  74. }
  75. /**
  76. * 返回成功结果
  77. * @param string $message
  78. * @param mixed $data
  79. * @return bool
  80. */
  81. protected function success(string $message = 'success', mixed $data = []): bool
  82. {
  83. $this->message = $message;
  84. $this->data = $data;
  85. return true;
  86. }
  87. /**
  88. * 返回失败结果
  89. * @param string $message
  90. * @param mixed $data
  91. * @return bool
  92. */
  93. protected function error(string $message = 'error', mixed $data = []): bool
  94. {
  95. $this->message = $message;
  96. $this->data = $data;
  97. return false;
  98. }
  99. /**
  100. * 获取成功数据
  101. * @return mixed
  102. */
  103. public function getData(): mixed
  104. {
  105. return $this->data;
  106. }
  107. /**
  108. * 获取消息
  109. * @return string
  110. */
  111. public function getMessage(): string
  112. {
  113. return $this->message;
  114. }
  115. }