You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AccessToken.php 4.0KB

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