AccessToken.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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\printer;
  12. use crmeb\services\HttpService;
  13. use think\facade\Config;
  14. use think\facade\Log;
  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. protected $type;
  64. const HOST = [
  65. 'https://open-api.10ss.net/',
  66. 'http://api.feieyun.cn/Api/Open/',
  67. ];
  68. public function __construct(array $config = [], string $name, string $configFile)
  69. {
  70. //打印机编号
  71. $this->machineCode = $config['terminal'] ?? null;
  72. //appkey
  73. $this->clientId = $config['clientId'] ?? null;
  74. //用户
  75. $this->partner = $config['partner'] ?? null;
  76. $this->apiKey = $config['apiKey'] ?? null;
  77. //打印机类型
  78. $this->type = $config['type'] ?? 0;
  79. $this->name = $name;
  80. $this->configFile = $configFile;
  81. $this->apiUrl = self::HOST[$this->type];
  82. }
  83. /**
  84. * 获取token
  85. * @return mixed|null|string
  86. * @throws \Exception
  87. */
  88. public function getAccessToken()
  89. {
  90. if (isset($this->accessToken[$this->name])) {
  91. return $this->accessToken[$this->name];
  92. }
  93. $action = 'get' . Str::studly($this->name) . 'AccessToken';
  94. if (method_exists($this, $action)) {
  95. return $this->{$action}();
  96. } else {
  97. throw new \RuntimeException(__CLASS__ . '->' . $action . '(),Method not worn in');
  98. }
  99. }
  100. /**
  101. * 获取易联云token
  102. * @return mixed|null|string
  103. * @throws \Exception
  104. */
  105. protected function getYiLianYunAccessToken()
  106. {
  107. if(!$this->accessToken[$this->name] = Cache::get('YLY_access_token_'.$this->clientId)){
  108. $token = $this->getToken();
  109. Cache::set('YLY_access_token_'.$this->clientId,$token, 18 * 86400);
  110. $this->accessToken[$this->name] = $token;
  111. }
  112. if (!$this->accessToken[$this->name])
  113. Log::info('易联云打印机获取access_token获取失败:'.$this->clientId);
  114. return $this->accessToken[$this->name];
  115. }
  116. protected function getToken () {
  117. $data = [
  118. 'client_id' => $this->clientId,
  119. 'grant_type' => 'client_credentials',
  120. 'sign' => strtolower(md5($this->clientId . time() . $this->apiKey)),
  121. 'scope' => 'all',
  122. 'timestamp' => time(),
  123. 'id' => $this->createUuid(),
  124. ];
  125. $request = self::postRequest($this->apiUrl . 'oauth/oauth',$data );
  126. $request = json_decode($request, true);
  127. $request['error'] = $request['error'] ?? 0;
  128. $request['error_description'] = $request['error_description'] ?? '';
  129. if ($request['error'] == 0 && $request['error_description'] == 'success') {
  130. return $request['body']['access_token'] ?? '';
  131. }
  132. return '';
  133. }
  134. /**
  135. * 获取请求链接
  136. * @return string
  137. */
  138. public function getApiUrl(string $url = '')
  139. {
  140. return $url ? $this->apiUrl . $url : $this->apiUrl;
  141. }
  142. /**
  143. * 生成UUID4
  144. * @return string
  145. */
  146. public function createUuid()
  147. {
  148. return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff),
  149. mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff));
  150. }
  151. /**
  152. * 获取属性
  153. * @param $name
  154. * @return mixed
  155. */
  156. public function __get($name)
  157. {
  158. if (in_array($name, ['clientId', 'apiKey', 'accessToken', 'partner', 'terminal', 'machineCode'])) {
  159. return $this->{$name};
  160. }
  161. }
  162. }