AccessTokenServeService.php 4.4 KB

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