LingYunGongApi.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace common\helpers\http;
  3. use yii\base\BaseObject;
  4. use yii\httpclient\Client;
  5. class LingYunGongApi {
  6. const CACHE_TOKEN_KEY = 'cashTokenKey_%s';
  7. /**
  8. * 请求方法
  9. */
  10. const HTTP_METHOD_POST = 'post';
  11. const HTTP_METHOD_GET = 'get';
  12. /**
  13. * http client
  14. * @var null
  15. */
  16. private static $_client = null;
  17. /**
  18. * 获取http client 对象
  19. * @return null|Client
  20. */
  21. public static function client(){
  22. if(is_null(self::$_client)){
  23. self::$_client = new Client();
  24. }
  25. return self::$_client;
  26. }
  27. /**
  28. * request
  29. * @return \yii\httpclient\Request
  30. */
  31. public static function request(){
  32. return self::client()->createRequest();
  33. }
  34. /**
  35. * POST请求
  36. * @param $url
  37. * @param $data
  38. * @param array $headers
  39. * @return mixed
  40. */
  41. public static function post($url, $data, array $headers=[]){
  42. $query = self::request()
  43. ->setMethod(self::HTTP_METHOD_POST);
  44. if( $headers ) {
  45. $query->addHeaders($headers);
  46. }
  47. $query->setFormat(Client::FORMAT_JSON);
  48. $query->setUrl($url)
  49. ->setData($data);
  50. unset($data);
  51. return $query->send();
  52. }
  53. /**
  54. * GET请求
  55. * @param $url
  56. * @param array $data
  57. * @param array $headers
  58. * @return \yii\httpclient\Response
  59. */
  60. public static function get($url, array $data = [], array $headers=[]){
  61. $query = self::request()
  62. ->setMethod(self::HTTP_METHOD_GET);
  63. if( $headers ) {
  64. $query->addHeaders($headers);
  65. }
  66. $query->setUrl($url);
  67. if($data){
  68. $query->setData($data);
  69. }
  70. unset($data);
  71. return $query->send();
  72. }
  73. /**
  74. * 获取token
  75. * @return mixed|string
  76. */
  77. public static function getAuthToken() {
  78. $authTokenClientId = \Yii::$app->params['http']['lingYunGongApi']['authToken']['clientId'];
  79. $cacheKey = sprintf(self::CACHE_TOKEN_KEY, $authTokenClientId);
  80. $cache = \Yii::$app->cache;
  81. $token = $cache->get($cacheKey);
  82. if( $token ) return $token;
  83. $tokenData = self::_apiAuthToken();
  84. if ( !$tokenData ) return '';
  85. $token = $tokenData['access_token'] ?? '';
  86. $expiresIn = $tokenData['expires_in'] ?? 0;
  87. // $tokenType = $tokenData['token_type'];
  88. $cacheTime = $expiresIn - 60;
  89. if( $cacheTime > 0 && $token ) {
  90. \Yii::$app->cache->set($cacheKey, $token, $cacheTime);
  91. }
  92. return $token;
  93. }
  94. /**
  95. * api接口获取token
  96. * @return false
  97. */
  98. private static function _apiAuthToken() {
  99. $host = \Yii::$app->params['http']['lingYunGongApi']['host'];
  100. $authTokenPath = \Yii::$app->params['http']['lingYunGongApi']['authToken']['path'];
  101. $authTokenClientId = \Yii::$app->params['http']['lingYunGongApi']['authToken']['clientId'];
  102. $requestUrl = $host . $authTokenPath;
  103. $data = [
  104. 'grant_type' => 'client_credential',
  105. 'client_secret' => 'secret!',
  106. 'client_id' => $authTokenClientId,
  107. ];
  108. $response = self::post($requestUrl, $data, ['content-type' => 'application/json']);
  109. if($response->isOk){
  110. return $response->data;
  111. } else {
  112. return false;
  113. }
  114. }
  115. /**
  116. * 判断是否上传了身份证信息
  117. * @param string $idCard
  118. */
  119. public static function hasIdCardInfo(string $idCard) {
  120. $token = self::getAuthToken();
  121. if( !$token ) return false;
  122. $host = \Yii::$app->params['http']['lingYunGongApi']['host'];
  123. $authTokenPath = \Yii::$app->params['http']['lingYunGongApi']['hasIdCardInfoPath'];
  124. $requestUrl = $host . sprintf($authTokenPath, $idCard);
  125. $response = self::get($requestUrl, [], [
  126. 'Authorization' => sprintf('Bearer %s', $token)
  127. ]);
  128. if($response->isOk){
  129. return $response->data;
  130. } else {
  131. return false;
  132. }
  133. }
  134. }