ByteDance.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Helper;
  3. use App\Helper\Bytedance\ByteDanceAPI;
  4. use GuzzleHttp\Client;
  5. use GuzzleHttp\Exception\GuzzleException;
  6. use PharIo\Manifest\Email;
  7. use think\Exception;
  8. class ByteDance
  9. {
  10. private $appId = null;
  11. private $appKey = null;
  12. private $secret = null;
  13. private $accessTokenFile = null;
  14. private $accessToken = null;
  15. /**
  16. * @param array $config
  17. * @return $this
  18. */
  19. public function factory($config = [])
  20. {
  21. $this->appId = $config['app_id'];
  22. $this->appKey = $config['app_key'];
  23. $this->secret = $config['app_secret'];
  24. $this->accessTokenFile = storage_path('app/bytedance/bytedance_access_token.json');
  25. $this->accessToken = $this->checkAccessToken();
  26. return $this;
  27. }
  28. /**
  29. * @param string $code
  30. * @param string $anonymousCode
  31. * @return array|mixed
  32. * @throws \Exception
  33. */
  34. public function login($code = '',$anonymousCode = '')
  35. {
  36. return $this->post(ByteDanceAPI::LOGIN, [
  37. 'code' => $code,
  38. 'anonymous_code' => $anonymousCode,
  39. ]);
  40. }
  41. /**
  42. * 校验access token 是否过期
  43. * @return mixed
  44. */
  45. private function checkAccessToken() : string
  46. {
  47. try {
  48. $dir = storage_path('app/bytedance');
  49. if (!is_dir($dir)) mkdir($dir, 0755);
  50. if (!is_file($this->accessTokenFile)) touch($this->accessTokenFile);
  51. $accessToken = file_get_contents($this->accessTokenFile);
  52. $accessToken = $accessToken ? json_decode($accessToken,true) : null;
  53. if (empty($accessToken) || $accessToken['expires_at'] < time()) {
  54. $accessToken = $this->getAccessToken();
  55. }else{
  56. $accessToken = $accessToken['access_token'];
  57. }
  58. return $accessToken;
  59. } catch (\Exception $e) {
  60. }
  61. }
  62. /**
  63. * 获取 access token
  64. * @throws \Exception
  65. */
  66. private function getAccessToken() : string
  67. {
  68. $res = $this->post(ByteDanceAPI::ACCESS_TOKEN, [
  69. 'grant_type' => 'client_credential'
  70. ]);
  71. if (!empty($res['err_no'])) {
  72. throw new \Exception('获取access token 错误');
  73. }
  74. file_put_contents($this->accessTokenFile, json_encode([
  75. 'access_token' => $res['access_token'],
  76. 'expires_at' => $res['expiresAt']
  77. ]));
  78. return $res['access_token'];
  79. }
  80. /**
  81. * 接口请求
  82. *
  83. * @param string $uri
  84. * @param array $data
  85. * @return array|mixed
  86. * @throws \Exception
  87. */
  88. private function post($uri = '', $data = []) : array
  89. {
  90. try {
  91. $data = array_merge($data,[
  92. 'appid' => $this->appId,
  93. 'secret' => $this->secret,
  94. ]);
  95. $client = new Client();
  96. $res = $client->post($uri, [
  97. 'verify' => false,
  98. 'headers' => ['Content-Type' => 'application/json'],
  99. 'body' => json_encode($data)
  100. ]);
  101. $stringBody = (string)$res->getBody();
  102. $res = json_decode($stringBody, true);
  103. if(!empty($res['err_no'])){
  104. throw new \Exception("请求字节跳动API接口错误,错误码:{$res['err_no']},错误信息:{$res['err_tips']}");
  105. }
  106. return $res['data'];
  107. } catch (GuzzleException $e) {
  108. trace($e->getMessage(), 'error');
  109. throw new \Exception($e->getMessage());
  110. }
  111. }
  112. }