ApiAbstract.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace common\logic\middleground\base;
  3. use common\models\common\MiddleMch;
  4. use GuzzleHttp\Client;
  5. use yii\helpers\Json;
  6. abstract class ApiAbstract implements ApiInterface
  7. {
  8. /**
  9. * 中台域名
  10. *
  11. * @var string
  12. */
  13. protected $api_url = 'http://middle-api.qijianshigw.com/';
  14. protected $api_list = [];
  15. protected $mch = null;
  16. public function __construct(?MiddleMch $mch = null)
  17. {
  18. if (!empty($mch)) {
  19. $this->mch = $mch;
  20. }
  21. }
  22. public function getApiUrl(): string
  23. {
  24. // TODO: Implement getApiUrl() method.
  25. return $this->api_url;
  26. }
  27. public function getMethod(string $method): string
  28. {
  29. // TODO: Implement getMethod() method.
  30. return $this->api_list[$method] ?? '';
  31. }
  32. public function getFullUrl(string $method): string
  33. {
  34. // TODO: Implement getFullUrl() method.
  35. return $this->getApiUrl() . $this->getMethod($method);
  36. }
  37. public function getAppId(): string
  38. {
  39. // TODO: Implement getAppId() method.
  40. return $this->mch ? $this->mch->app_id : '';
  41. }
  42. public function getAppSecret(): string
  43. {
  44. // TODO: Implement getAppSecret() method.
  45. return $this->mch ? $this->mch->app_secret : '';
  46. }
  47. protected function createHeader($data)
  48. {
  49. return [
  50. 'api-app-id' => $this->getAppId(),
  51. 'api-req-id' => uniqid(),
  52. 'api-time-stamp' => (string) time(),
  53. 'api-sign' => $this->sign($data),
  54. ];
  55. }
  56. protected function sign($params)
  57. {
  58. // 2.以key做升序排序
  59. ksort($params);
  60. // 3.将key和value按照顺序直接拼接到一起
  61. $str = '';
  62. foreach ($params as $key => $val) {
  63. $str .= $key . $val;
  64. }
  65. // 4.在上一步的结果后直接拼secretKey
  66. $str .= $this->getAppSecret();
  67. // 5.对上一步结果进行sha1加密,得到16进制字符串,进行md5加密,结果转为大写
  68. return strtoupper(md5(sha1($str)));
  69. }
  70. /**
  71. * 请求
  72. * @param string $url
  73. * @param array $data
  74. * @param string $type
  75. * @param int $time
  76. */
  77. protected function request(string $url, array $data, string $method = "get", array $header = [])
  78. {
  79. if ($this->mch) {
  80. $header = array_merge($header, $this->createHeader($data));
  81. }
  82. $res = $this->curl($method, $url, $data, $header);
  83. if ($res['code'] != 200) {
  84. throw new MiddleException($res['error']);
  85. }
  86. if (method_exists($this, 'requestHandel')) {
  87. return $this->requestHandel($res['content']);
  88. }
  89. $content = Json::decode($res['content'], true);
  90. if ($content['code'] !== 200 || $content['success'] != true) {
  91. throw new MiddleException($content['message']);
  92. }
  93. return $content['data'];
  94. }
  95. protected function curl($method, $api, $params = [], $headers = [])
  96. {
  97. if (!$api) {
  98. return ['code' => 404, 'error' => 'api is null'];
  99. }
  100. $client = new Client([
  101. 'timeout' => $headers['timeout'] ?? 5,
  102. ]);
  103. $method = strtoupper($method);
  104. $options = [];
  105. $headers['charset'] = $headers['charset'] ?? 'UTF-8';
  106. $options['headers'] = $headers;
  107. if ($method == 'GET' && $params) {
  108. $options['query'] = $params;
  109. }
  110. if ($method == 'POST') {
  111. $options['headers']['Content-Type'] = $headers['Content-Type'] ?? 'application/json';
  112. if ($options['headers']['Content-Type'] == 'application/json' && $params) {
  113. $options['body'] = \GuzzleHttp\json_encode($params ? $params : (object) []);
  114. }
  115. if ($options['headers']['Content-Type'] == 'application/x-www-form-urlencoded' && $params) {
  116. $options['form_params'] = $params;
  117. }
  118. }
  119. try {
  120. $request = $client->request($method, $api, $options);
  121. $code = $request->getStatusCode();
  122. $content = $request->getBody()->getContents();
  123. return compact('code', 'content');
  124. } catch (\Throwable $e) {
  125. return ['code' => 500, 'error' => $e->getMessage()];
  126. }
  127. }
  128. }