AccessToken.php 4.1 KB

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