AccessTokenServeService.php 4.0 KB

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