AccessTokenServeService.php 3.8 KB

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