ApiService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2020/6/3
  6. * Time: 上午10:14
  7. */
  8. namespace app\main\service;
  9. use app\main\constants\ErrorCodeConstants;
  10. use app\main\model\object\ReturnObject;
  11. use GuzzleHttp\Client;
  12. use GuzzleHttp\Exception\GuzzleException;
  13. use think\Config;
  14. /**
  15. * Class ApiService
  16. * @package app\main\service
  17. */
  18. class ApiService extends BaseService
  19. {
  20. /**
  21. * @var ApiService
  22. */
  23. protected static $self = NULL;
  24. /**
  25. * @var Client
  26. */
  27. public $client;
  28. /**
  29. * @return ApiService
  30. */
  31. public static function instance()
  32. {
  33. if (self::$self == NULL) {
  34. self::$self = new self();
  35. }
  36. return self::$self;
  37. }
  38. /**
  39. * @param string $base_uri
  40. * @param array $header
  41. * @return ReturnObject
  42. */
  43. public function jsonRequest($base_uri = '', $header = ['content-type' => 'application/json'])
  44. {
  45. $config = [
  46. 'connect_timeout' => 3,
  47. 'timeout' => 30,
  48. 'http_errors' => true, //抛出异常 true是 false否
  49. 'verify' => false,
  50. 'headers' => [
  51. 'charset' => 'utf-8'
  52. ],
  53. ];
  54. if ($base_uri) {
  55. $config['base_uri'] = $base_uri;
  56. }
  57. if ($header) {
  58. $config['headers'] = array_merge($config['headers'], $header);
  59. }
  60. $this->client = new Client($config);
  61. return $this->getReturn();
  62. }
  63. /**
  64. * 发送API请求
  65. * @param $uri
  66. * @param $data
  67. * @return ReturnObject|ApiService
  68. */
  69. public function post($uri, $data)
  70. {
  71. try {
  72. $result = $this->client->request('POST', (string)$uri, [
  73. 'body' => json_encode($data)
  74. ]);
  75. LogService::info('uri:'.$uri);
  76. LogService::info('body:'.json_encode($data));
  77. if ($result->getStatusCode() == 200) {
  78. $content = $result->getBody()->getContents();
  79. } else {
  80. $content = '';
  81. }
  82. return $this->setData($content)->getReturn();
  83. } catch (GuzzleException $e) {
  84. return $this->setCode($e->getCode())->setMsg($e->getMessage())->getReturn();
  85. } catch (\Exception $e) {
  86. return $this->setCode($e->getCode())->setMsg($e->getMessage())->getReturn();
  87. }
  88. }
  89. /**
  90. * @param $code
  91. * @param $params
  92. * @return ReturnObject|ApiService
  93. */
  94. public function getCollectFromApi($code, $params)
  95. {
  96. ApiService::instance()->jsonRequest(Config::get('api.external_uri'));
  97. $result = ApiService::instance()->post($code, $params);
  98. $data = [];
  99. if ($result->code == ErrorCodeConstants::SUCCESS) {
  100. if ($result->data) {
  101. $decode = json_decode($result->data, true);
  102. if ($decode && array_key_exists('retCode', $decode) && $decode['retCode'] == 0) {
  103. $data = $decode['data'];
  104. }
  105. }
  106. }
  107. return $this->setData($data)->getReturn();
  108. }
  109. /**
  110. * @param $code
  111. * @param $params
  112. * @return ReturnObject|ApiService
  113. */
  114. public function getDataFromApi($code, $params)
  115. {
  116. ApiService::instance()->jsonRequest(Config::get('api.service_uri'));
  117. $result = ApiService::instance()->post($code, $params);
  118. $data = [];
  119. if ($result->code == ErrorCodeConstants::SUCCESS) {
  120. if ($result->data) {
  121. $decode = json_decode($result->data, true);
  122. if ($decode && array_key_exists('status', $decode) && in_array($decode['status'], [0, 104])) {
  123. if (array_key_exists('data', $decode)) {
  124. $data = $decode['data'];
  125. }
  126. } else {
  127. LogService::error($result->data);
  128. }
  129. }
  130. }
  131. return $this->setData($data)->getReturn();
  132. }
  133. public function checkApiOn()
  134. {
  135. return Config::get('api.service_on');
  136. }
  137. }