AccessTokenServeService.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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 app\common\repositories\system\config\ConfigValueRepository;
  13. use think\exception\ValidateException;
  14. use think\facade\Cache;
  15. /**
  16. * Class AccessTokenServeService
  17. * @package crmeb\services
  18. */
  19. class AccessTokenServeService extends HttpService
  20. {
  21. /**
  22. * 配置
  23. * @var string
  24. */
  25. protected $account;
  26. /**
  27. * @var string
  28. */
  29. protected $secret;
  30. /**
  31. * @var Cache|null
  32. */
  33. protected $cache;
  34. /**
  35. * @var string
  36. */
  37. protected $accessToken;
  38. /**
  39. * @var string
  40. */
  41. protected $cacheTokenPrefix = "_crmeb_plat";
  42. /**
  43. * @var string
  44. */
  45. protected $apiHost = 'http://sms.crmeb.net/api/v2/';
  46. /**
  47. * 沙盒地址
  48. * @var string
  49. */
  50. protected $sandBoxApi = 'https://api_v2.crmeb.net/api/';
  51. /**
  52. * 沙盒模式
  53. * @var bool
  54. */
  55. protected $sandBox = false;
  56. /**
  57. * 登录接口
  58. */
  59. const USER_LOGIN = "user/login";
  60. protected $merId;
  61. /**
  62. * AccessTokenServeService constructor.
  63. * @param string $account
  64. * @param string $secret
  65. * @param Cache|null $cache
  66. */
  67. public function __construct($config)
  68. {
  69. $this->account = $config['account'] ?? '';
  70. $this->secret = $config['secret'] ?? '';
  71. $this->merId = $config['merId'] ?? 0 ;
  72. }
  73. /**
  74. * 获取配置
  75. * @return array
  76. */
  77. public function getConfig()
  78. {
  79. return [
  80. 'access_key' => $this->account,
  81. 'secret_key' => $this->secret
  82. ];
  83. }
  84. /**
  85. * 获取缓存token
  86. * @return mixed
  87. * @throws \Psr\SimpleCache\InvalidArgumentException
  88. */
  89. public function getToken()
  90. {
  91. $accessTokenKey = md5($this->account . '_' . $this->secret . $this->cacheTokenPrefix);
  92. $cacheToken = Cache::get($accessTokenKey);
  93. if (!$cacheToken) {
  94. $getToken = $this->getTokenFromServer();
  95. Cache::set($accessTokenKey, $getToken['access_token'], 300);
  96. $cacheToken = $getToken['access_token'];
  97. }
  98. $this->accessToken = $cacheToken;
  99. return $cacheToken;
  100. }
  101. /**
  102. * 从服务器获取token
  103. * @return mixed
  104. */
  105. public function getTokenFromServer()
  106. {
  107. $params = $this->getConfig();
  108. if (!$this->account || !$this->secret ) {
  109. $msg = $this->merId ? '请先配置:设置 》一号通 》配置管理 》一号通' : '请先配置:设置 》第三方接口 》一号通';
  110. throw new ValidateException($msg);
  111. }
  112. $response = $this->postRequest($this->get(self::USER_LOGIN), $params);
  113. $response = json_decode($response, true);
  114. if (!$response) {
  115. throw new ValidateException('获取token失败');
  116. }
  117. if ($response['status'] === 200) {
  118. return $response['data'];
  119. } else {
  120. throw new ValidateException($response['msg']);
  121. }
  122. }
  123. /**
  124. * 请求
  125. * @param string $url
  126. * @param array $data
  127. * @param string $method
  128. * @param bool $isHeader
  129. * @return array|mixed
  130. */
  131. public function httpRequest(string $url, array $data = [], string $method = 'POST', bool $isHeader = true,$header = [])
  132. {
  133. if ($isHeader) {
  134. $this->getToken();
  135. if (!$this->accessToken) {
  136. throw new ValidateException('配置已更改或token已失效');
  137. }
  138. $header = array_merge(['Authorization:Bearer-' . $this->accessToken] , $header);
  139. }
  140. $res = $this->request($this->get($url), $method, $data, $header);
  141. if (!$res) {
  142. throw new ValidateException('平台错误:发生异常,请稍后重试');
  143. }
  144. $result = json_decode($res, true) ?: false;
  145. if (!isset($result['status']) || $result['status'] != 200) {
  146. if ($result['msg'] == '接口请求失败:用户或密码错误') {
  147. app()->make(CrmebServeServices::class)->logout();
  148. }
  149. throw new ValidateException(isset($result['msg']) ? '平台错误:' . $result['msg'] : '平台错误:发生异常,请稍后重试');
  150. }
  151. return $result['data'] ?? [];
  152. }
  153. /**
  154. * @param string $apiUrl
  155. * @return string
  156. */
  157. public function get(string $apiUrl = '')
  158. {
  159. $host = $this->sandBox ? $this->sandBoxApi : $this->apiHost;
  160. return $host . $apiUrl;
  161. }
  162. }