AccessTokenServeService.php 4.3 KB

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