AccessTokenServeService.php 4.6 KB

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