Kuaishou.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Helper;
  3. use App\Helper\UniPlatform\BaseAPI;
  4. use App\Helper\UniPlatform\BaseUniPlatform;
  5. use Carbon\Carbon;
  6. use GuzzleHttp\Client;
  7. use GuzzleHttp\Exception\GuzzleException;
  8. /**
  9. * Class Kuaishou
  10. *
  11. * @package App\Helper
  12. * @property-read string $appId
  13. * @property-read string $slat
  14. * @property-read string $secret
  15. * @property-read string $token
  16. * @property-read string $accessTokenFile
  17. * @property-read string $accessToken
  18. * @property-read string $noticeUrl
  19. * @property-read string $validTimestamp
  20. */
  21. class Kuaishou extends BaseUniPlatform
  22. {
  23. public function __construct(BaseAPI $api)
  24. {
  25. $this->API = $api;
  26. }
  27. /**
  28. * @param string $code
  29. * @return array|mixed
  30. * @throws \Exception
  31. */
  32. public function login($code = ''): array
  33. {
  34. return $this->post($this->API::LOGIN, [
  35. 'app_id' => $this->appId,
  36. 'app_secret' => $this->secret,
  37. 'js_code' => $code,
  38. ]);
  39. }
  40. protected function createOrder($outOrderNo, $totalAmount): array
  41. {
  42. // TODO: Implement createOrder() method.
  43. }
  44. protected function getAccessToken(): string
  45. {
  46. $res = $this->post($this->API::ACCESS_TOKEN, [
  47. 'app_id' => $this->appId,
  48. 'app_secret' => $this->secret,
  49. 'grant_type' => 'client_credentials',
  50. ]);
  51. if (!isset($res['result']) || $res['result'] != 1) {
  52. throw new \Exception('获取access token 错误');
  53. }
  54. file_put_contents($this->accessTokenFile, json_encode([
  55. 'access_token' => $res['access_token'],
  56. 'expires_at' => Carbon::now()->timestamp + $res['expires_in']
  57. ]));
  58. return $res['access_token'];
  59. }
  60. protected function setAccessFileDir(): void
  61. {
  62. $this->accessTokenDir = storage_path('app/kuaishou');
  63. }
  64. protected function setAccessFilePath(): void
  65. {
  66. $this->accessTokenFile = storage_path('app/kuaishou/kuaishou_access_token.json');
  67. }
  68. /**
  69. * @param string $uri
  70. * @param array $data
  71. * @return array
  72. * @throws \Exception
  73. */
  74. protected function post($uri = '', $data = []): array
  75. {
  76. try {
  77. $client = new Client();
  78. $url = $uri.'?'.http_build_query($data);
  79. $res = $client->post($url, [
  80. 'verify' => false,
  81. 'headers' => ['Content-Type' => 'x-www-form-urlencoded'],
  82. ]);
  83. $stringBody = (string)$res->getBody();
  84. $res = json_decode($stringBody, true);
  85. if(!isset($res['result']) || $res['result'] != 1){
  86. throw new \Exception("请求快手API接口错误,错误码:{$res['result']},错误信息:{$res['error_msg']}");
  87. }
  88. return $res;
  89. } catch (GuzzleException $e) {
  90. \Log::error($e->getMessage());
  91. throw new \Exception($e->getMessage());
  92. }
  93. }
  94. }