AccessToken.php 3.9 KB

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