AccessTokenServeService.php 3.9 KB

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