AccessToken.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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\printer;
  12. use app\services\other\CacheServices;
  13. use crmeb\services\HttpService;
  14. use think\facade\Config;
  15. use think\helper\Str;
  16. use think\facade\Cache;
  17. /**
  18. *
  19. * Class AccessToken
  20. * @package crmeb\services\printer
  21. */
  22. class AccessToken extends HttpService
  23. {
  24. /**
  25. * token
  26. * @var array
  27. */
  28. protected $accessToken;
  29. /**
  30. * 请求接口
  31. * @var string
  32. */
  33. protected $apiUrl;
  34. /**
  35. * @var string
  36. */
  37. protected $clientId;
  38. /**
  39. * 终端号码
  40. * @var string
  41. */
  42. protected $machineCode;
  43. /**
  44. * 开发者id
  45. * @var string
  46. */
  47. protected $partner;
  48. /**
  49. * 驱动类型
  50. * @var string
  51. */
  52. protected $name;
  53. /**
  54. * 配置文件名
  55. * @var string
  56. */
  57. protected $configFile;
  58. /**
  59. * api key
  60. * @var string
  61. */
  62. protected $apiKey;
  63. public function __construct(array $config = [], string $name, string $configFile)
  64. {
  65. $this->clientId = $config['clientId'] ?? null;
  66. $this->apiKey = $config['apiKey'] ?? null;
  67. $this->partner = $config['partner'] ?? null;
  68. $this->machineCode = $config['terminal'] ?? null;
  69. $this->name = $name;
  70. $this->configFile = $configFile;
  71. $this->apiUrl = Config::get($this->configFile . '.stores.' . $this->name . '.apiUrl', 'https://open-api.10ss.net/');
  72. }
  73. /**
  74. * 获取token
  75. * @return mixed|null|string
  76. * @throws \Exception
  77. */
  78. public function getAccessToken()
  79. {
  80. if (isset($this->accessToken[$this->name])) {
  81. return $this->accessToken[$this->name];
  82. }
  83. $action = 'get' . Str::studly($this->name) . 'AccessToken';
  84. if (method_exists($this, $action)) {
  85. return $this->{$action}();
  86. } else {
  87. throw new \RuntimeException(__CLASS__ . '->' . $action . '(),Method not worn in');
  88. }
  89. }
  90. /**
  91. * 获取易联云token
  92. * @return mixed|null|string
  93. * @throws \Exception
  94. */
  95. protected function getYiLianYunAccessToken()
  96. {
  97. /** @var CacheServices $cacheServices */
  98. if(!$this->accessToken[$this->name] = Cache::get('YLY_access_token_'.$this->clientId)){
  99. $token = $this->getToken();
  100. Cache::set('YLY_access_token_'.$this->clientId,$token, 18 * 86400);
  101. $this->accessToken[$this->name] = $token;
  102. }
  103. if (!$this->accessToken[$this->name])
  104. throw new \Exception('获取access_token获取失败');
  105. return $this->accessToken[$this->name];
  106. }
  107. protected function getToken () {
  108. $data = [
  109. 'client_id' => $this->clientId,
  110. 'grant_type' => 'client_credentials',
  111. 'sign' => strtolower(md5($this->clientId . time() . $this->apiKey)),
  112. 'scope' => 'all',
  113. 'timestamp' => time(),
  114. 'id' => $this->createUuid(),
  115. ];
  116. $request = self::postRequest($this->apiUrl . 'oauth/oauth',$data );
  117. $request = json_decode($request, true);
  118. $request['error'] = $request['error'] ?? 0;
  119. $request['error_description'] = $request['error_description'] ?? '';
  120. if ($request['error'] == 0 && $request['error_description'] == 'success') {
  121. return $request['body']['access_token'] ?? '';
  122. }
  123. return '';
  124. }
  125. /**
  126. * 获取请求链接
  127. * @return string
  128. */
  129. public function getApiUrl(string $url = '')
  130. {
  131. return $url ? $this->apiUrl . $url : $this->apiUrl;
  132. }
  133. /**
  134. * 生成UUID4
  135. * @return string
  136. */
  137. public function createUuid()
  138. {
  139. return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff),
  140. mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff));
  141. }
  142. /**
  143. * 获取属性
  144. * @param $name
  145. * @return mixed
  146. */
  147. public function __get($name)
  148. {
  149. if (in_array($name, ['clientId', 'apiKey', 'accessToken', 'partner', 'terminal', 'machineCode'])) {
  150. return $this->{$name};
  151. }
  152. }
  153. }