You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace crmeb\services;
  3. use crmeb\exceptions\ApiException;
  4. /**
  5. * Class AccessTokenServeService
  6. */
  7. class AccessTokenServeService extends HttpService
  8. {
  9. /**
  10. * 配置
  11. * @var string
  12. */
  13. protected $account;
  14. /**
  15. * @var string
  16. */
  17. protected $secret;
  18. /**
  19. * @var Cache|null
  20. */
  21. protected $cache;
  22. /**
  23. * @var string
  24. */
  25. protected $accessToken;
  26. /**
  27. * @var string
  28. */
  29. protected $cacheTokenPrefix = "_crmeb_plat";
  30. /**
  31. * @var string
  32. */
  33. protected $apiHost = '';//???
  34. /**
  35. * 登录接口
  36. */
  37. const USER_LOGIN = "user/login";
  38. /**
  39. * AccessTokenServeService constructor.
  40. * @param string $account
  41. * @param string $secret
  42. * @param Cache|null $cache
  43. */
  44. public function __construct(string $account, string $secret, $cache = null)
  45. {
  46. if (!$cache) {
  47. /** @var CacheService $cache */
  48. $cache = app()->make(CacheService::class);
  49. }
  50. $this->account = $account;
  51. $this->secret = $secret;
  52. $this->cache = $cache;
  53. }
  54. /**
  55. * 获取配置
  56. * @return array
  57. */
  58. public function getConfig()
  59. {
  60. return [
  61. 'account' => $this->account,
  62. 'secret' => $this->secret
  63. ];
  64. }
  65. /**
  66. * 获取缓存token
  67. * @return mixed
  68. * @throws \Psr\SimpleCache\InvalidArgumentException
  69. */
  70. public function getToken()
  71. {
  72. $accessTokenKey = md5($this->account . '_' . $this->secret . $this->cacheTokenPrefix);
  73. $cacheToken = $this->cache->get($accessTokenKey);
  74. if (!$cacheToken) {
  75. $getToken = $this->getTokenFromServer();
  76. $this->cache->set($accessTokenKey, $getToken['access_token'], 300);
  77. $cacheToken = $getToken['access_token'];
  78. }
  79. $this->accessToken = $cacheToken;
  80. return $cacheToken;
  81. }
  82. /**
  83. * 从服务器获取token
  84. * @return mixed
  85. */
  86. public function getTokenFromServer()
  87. {
  88. $params = [
  89. 'account' => $this->account,
  90. 'secret' => $this->secret,
  91. ];
  92. if (!$this->account || !$this->secret) {
  93. throw new ApiException('请先登录一号通平台!');
  94. }
  95. $response = $this->postRequest($this->get(self::USER_LOGIN), $params);
  96. $response = json_decode($response, true);
  97. if (!$response) {
  98. throw new ApiException('获取token失败');
  99. }
  100. if ($response['status'] === 200) {
  101. return $response['data'];
  102. } else {
  103. throw new ApiException($response['msg']);
  104. }
  105. }
  106. /**
  107. * 请求
  108. * @param string $url
  109. * @param array $data
  110. * @param string $method
  111. * @param bool $isHeader
  112. * @return array|mixed
  113. */
  114. public function httpRequest(string $url, array $data = [], string $method = 'POST', bool $isHeader = true)
  115. {
  116. $header = [];
  117. if ($isHeader) {
  118. $this->getToken();
  119. if (!$this->accessToken) {
  120. throw new ApiException('配置已更改或token已失效');
  121. }
  122. $header = ['Authorization:Bearer-' . $this->accessToken];
  123. }
  124. $res = $this->request($this->get($url), $method, $data, $header);
  125. if (!$res) {
  126. throw new ApiException('平台错误:发生异常,请稍后重试');
  127. }
  128. $result = json_decode($res, true) ?: false;
  129. if (!isset($result['status']) || $result['status'] != 200) {
  130. throw new ApiException(isset($result['msg']) ? '平台错误:' . $result['msg'] : '平台错误:发生异常,请稍后重试');
  131. }
  132. return $result['data'] ?? [];
  133. }
  134. /**
  135. * @param string $apiUrl
  136. * @return string
  137. */
  138. public function get(string $apiUrl = '')
  139. {
  140. return $this->apiHost . $apiUrl;
  141. }
  142. }