AccessTokenServeService.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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 string
  30. */
  31. protected $accessToken;
  32. /**
  33. * @var string
  34. */
  35. protected $cacheTokenPrefix = "_crmeb_plat";
  36. /**
  37. * @var string
  38. */
  39. protected $apiHost = 'https://sms.crmeb.net/api/';
  40. /**
  41. * 沙盒地址
  42. * @var string
  43. */
  44. protected $sandBoxApi = 'https://api_v2.crmeb.net/api/';
  45. /**
  46. * 沙盒模式
  47. * @var bool
  48. */
  49. protected $sandBox = false;
  50. /**
  51. * 登录接口
  52. */
  53. const USER_LOGIN = "v2/user/login";
  54. /**
  55. * AccessTokenServeService constructor.
  56. * @param string $account
  57. * @param string $secret
  58. */
  59. public function __construct(string $account, string $secret)
  60. {
  61. $this->account = $account;
  62. $this->secret = $secret;
  63. }
  64. /**
  65. * 获取配置
  66. * @return array
  67. */
  68. public function getConfig()
  69. {
  70. return [
  71. 'access_key' => $this->account,
  72. 'secret_key' => $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 . '_v2_' . $this->secret . $this->cacheTokenPrefix);
  83. $cacheToken = CacheService::get($accessTokenKey);
  84. if (!$cacheToken) {
  85. $getToken = $this->getTokenFromServer();
  86. CacheService::set($accessTokenKey, $getToken['access_token'], $getToken['expires_in'] - 60);
  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. 'access_key' => $this->account,
  100. 'secret_key' => $this->secret,
  101. ];
  102. $response = $this->postRequest($this->get(self::USER_LOGIN), $params);
  103. $response = json_decode($response, true);
  104. if (!$response) {
  105. throw new ApiException(410085, ['msg' => '']);
  106. }
  107. if ($response['status'] === 200) {
  108. return $response['data'];
  109. } else {
  110. throw new ApiException(410085, ['msg' => ':' . $response['msg']]);
  111. }
  112. }
  113. /**
  114. * 请求
  115. * @param string $url
  116. * @param array $data
  117. * @param string $method
  118. * @param bool $isHeader
  119. * @return array|mixed
  120. */
  121. public function httpRequest(string $url, array $data = [], string $method = 'POST', bool $isHeader = true, array $header = [])
  122. {
  123. if ($isHeader) {
  124. $this->getToken();
  125. if (!$this->accessToken) {
  126. throw new ApiException(410086);
  127. }
  128. $header = array_merge($header, ['Authorization:Bearer-' . $this->accessToken]);
  129. }
  130. $res = $this->request($this->get($url), $method, $data, $header);
  131. if (!$res) {
  132. throw new ApiException(410087);
  133. }
  134. $result = json_decode($res, true) ?: false;
  135. if (!isset($result['status']) || $result['status'] != 200) {
  136. throw new ApiException($result['msg']);
  137. }
  138. return $result['data'] ?? [];
  139. }
  140. /**
  141. * @param string $apiUrl
  142. * @return string
  143. */
  144. public function get(string $apiUrl = '')
  145. {
  146. if ($this->sandBox) {
  147. return $this->sandBoxApi . $apiUrl;
  148. }
  149. return $this->apiHost . $apiUrl;
  150. }
  151. }