AccessToken.php 5.1 KB

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