正在显示
9 个修改的文件
包含
138 行增加
和
7 行删除
| @@ -7,6 +7,8 @@ use Lackoxygen\TiktokOpen\Base\Event\Fail; | @@ -7,6 +7,8 @@ use Lackoxygen\TiktokOpen\Base\Event\Fail; | ||
| 7 | use Lackoxygen\TiktokOpen\Base\Event\Request; | 7 | use Lackoxygen\TiktokOpen\Base\Event\Request; |
| 8 | use Lackoxygen\TiktokOpen\Base\Event\Response; | 8 | use Lackoxygen\TiktokOpen\Base\Event\Response; |
| 9 | use Lackoxygen\TiktokOpen\Base\ServiceManager; | 9 | use Lackoxygen\TiktokOpen\Base\ServiceManager; |
| 10 | +use Lackoxygen\TiktokOpen\Base\Signer\NoneSigner; | ||
| 11 | +use Lackoxygen\TiktokOpen\Base\Signer\SignerInterface; | ||
| 10 | use Lackoxygen\TiktokOpen\Base\Traits\BaseClient; | 12 | use Lackoxygen\TiktokOpen\Base\Traits\BaseClient; |
| 11 | use Lackoxygen\TiktokOpen\Wap\Listener; | 13 | use Lackoxygen\TiktokOpen\Wap\Listener; |
| 12 | use GuzzleHttp\RequestOptions; | 14 | use GuzzleHttp\RequestOptions; |
| @@ -20,17 +22,21 @@ class Client extends ServiceManager | @@ -20,17 +22,21 @@ class Client extends ServiceManager | ||
| 20 | 22 | ||
| 21 | private bool $withSession; | 23 | private bool $withSession; |
| 22 | 24 | ||
| 25 | + private SignerInterface $signer; | ||
| 26 | + | ||
| 23 | public function __construct( | 27 | public function __construct( |
| 24 | Application $app, $requestOption = '', $withSession = false | 28 | Application $app, $requestOption = '', $withSession = false |
| 25 | ) | 29 | ) |
| 26 | { | 30 | { |
| 27 | parent::__construct($app); | 31 | parent::__construct($app); |
| 28 | 32 | ||
| 29 | - $this->listen(); | ||
| 30 | - | ||
| 31 | $this->requestOption = $requestOption; | 33 | $this->requestOption = $requestOption; |
| 32 | 34 | ||
| 33 | $this->withSession = $withSession; | 35 | $this->withSession = $withSession; |
| 36 | + | ||
| 37 | + $this->signer = new NoneSigner(); | ||
| 38 | + | ||
| 39 | + $this->listen(); | ||
| 34 | } | 40 | } |
| 35 | 41 | ||
| 36 | protected function listen() | 42 | protected function listen() |
| @@ -68,6 +74,15 @@ class Client extends ServiceManager | @@ -68,6 +74,15 @@ class Client extends ServiceManager | ||
| 68 | ); | 74 | ); |
| 69 | } | 75 | } |
| 70 | 76 | ||
| 77 | + public function asQuery(): Client | ||
| 78 | + { | ||
| 79 | + return new Client( | ||
| 80 | + $this->app, | ||
| 81 | + RequestOptions::QUERY, | ||
| 82 | + $this->withSession | ||
| 83 | + ); | ||
| 84 | + } | ||
| 85 | + | ||
| 71 | public function withSession(): Client | 86 | public function withSession(): Client |
| 72 | { | 87 | { |
| 73 | return new Client( | 88 | return new Client( |
| @@ -77,6 +92,13 @@ class Client extends ServiceManager | @@ -77,6 +92,13 @@ class Client extends ServiceManager | ||
| 77 | ); | 92 | ); |
| 78 | } | 93 | } |
| 79 | 94 | ||
| 95 | + public function signVia(SignerInterface $signer): Client | ||
| 96 | + { | ||
| 97 | + $this->signer = $signer; | ||
| 98 | + | ||
| 99 | + return $this; | ||
| 100 | + } | ||
| 101 | + | ||
| 80 | public function refresh() | 102 | public function refresh() |
| 81 | { | 103 | { |
| 82 | $this->requestOption = ''; | 104 | $this->requestOption = ''; |
| @@ -3,6 +3,7 @@ | @@ -3,6 +3,7 @@ | ||
| 3 | namespace Lackoxygen\TiktokOpen\Base\Client; | 3 | namespace Lackoxygen\TiktokOpen\Base\Client; |
| 4 | 4 | ||
| 5 | use GuzzleHttp\RequestOptions; | 5 | use GuzzleHttp\RequestOptions; |
| 6 | +use Lackoxygen\TiktokOpen\Base\Signer\SignerInterface; | ||
| 6 | 7 | ||
| 7 | class Request | 8 | class Request |
| 8 | { | 9 | { |
| @@ -18,12 +19,15 @@ class Request | @@ -18,12 +19,15 @@ class Request | ||
| 18 | 19 | ||
| 19 | protected bool $withSession; | 20 | protected bool $withSession; |
| 20 | 21 | ||
| 22 | + protected SignerInterface $signer; | ||
| 23 | + | ||
| 21 | public function __construct( | 24 | public function __construct( |
| 22 | string $method, | 25 | string $method, |
| 23 | string $format, | 26 | string $format, |
| 24 | string $path, | 27 | string $path, |
| 25 | array $data, | 28 | array $data, |
| 26 | - bool $withSession | 29 | + bool $withSession, |
| 30 | + SignerInterface $signer | ||
| 27 | ) | 31 | ) |
| 28 | { | 32 | { |
| 29 | $this->setMethod($method); | 33 | $this->setMethod($method); |
| @@ -32,6 +36,7 @@ class Request | @@ -32,6 +36,7 @@ class Request | ||
| 32 | $this->path = $path; | 36 | $this->path = $path; |
| 33 | $this->data = $data; | 37 | $this->data = $data; |
| 34 | $this->withSession = $withSession; | 38 | $this->withSession = $withSession; |
| 39 | + $this->signer = $signer; | ||
| 35 | } | 40 | } |
| 36 | 41 | ||
| 37 | /** | 42 | /** |
| @@ -75,6 +80,11 @@ class Request | @@ -75,6 +80,11 @@ class Request | ||
| 75 | $this->headers = $headers; | 80 | $this->headers = $headers; |
| 76 | } | 81 | } |
| 77 | 82 | ||
| 83 | + public function setHeader(string $key, string $value) | ||
| 84 | + { | ||
| 85 | + $this->headers[$key] = $value; | ||
| 86 | + } | ||
| 87 | + | ||
| 78 | /** | 88 | /** |
| 79 | * @return string | 89 | * @return string |
| 80 | */ | 90 | */ |
| @@ -115,6 +125,14 @@ class Request | @@ -115,6 +125,14 @@ class Request | ||
| 115 | return $this->withSession; | 125 | return $this->withSession; |
| 116 | } | 126 | } |
| 117 | 127 | ||
| 128 | + /** | ||
| 129 | + * @return SignerInterface | ||
| 130 | + */ | ||
| 131 | + public function getSigner(): SignerInterface | ||
| 132 | + { | ||
| 133 | + return $this->signer; | ||
| 134 | + } | ||
| 135 | + | ||
| 118 | protected function defaultFormat(): string | 136 | protected function defaultFormat(): string |
| 119 | { | 137 | { |
| 120 | if ('GET' === $this->method) { | 138 | if ('GET' === $this->method) { |
src/Base/Signer/LifeSigner.php
0 → 100644
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace Lackoxygen\TiktokOpen\Base\Signer; | ||
| 4 | + | ||
| 5 | +use Lackoxygen\TiktokOpen\Base\Client\Request; | ||
| 6 | +use Lackoxygen\TiktokOpen\Base\Config; | ||
| 7 | + | ||
| 8 | +class LifeSigner implements SignerInterface | ||
| 9 | +{ | ||
| 10 | + public function carry(Config $config, Request $request) | ||
| 11 | + { | ||
| 12 | + $payload = $request->getData(); | ||
| 13 | + | ||
| 14 | + ksort($payload); | ||
| 15 | + | ||
| 16 | + $config->getAppSecret() . '&client_key=' . | ||
| 17 | + $config->getAppKey() . '×tamp=' . strval(time()) | ||
| 18 | + . '&http_body=' . \json_encode($payload); | ||
| 19 | + | ||
| 20 | + $request->setHeader( | ||
| 21 | + 'x-life-clientkey', | ||
| 22 | + $config->getAppKey(), | ||
| 23 | + ); | ||
| 24 | + } | ||
| 25 | +} |
src/Base/Signer/NoneSigner.php
0 → 100644
src/Base/Signer/SignerInterface.php
0 → 100644
| @@ -38,7 +38,8 @@ trait BaseClient | @@ -38,7 +38,8 @@ trait BaseClient | ||
| 38 | $this->requestOption, | 38 | $this->requestOption, |
| 39 | $path, | 39 | $path, |
| 40 | $data, | 40 | $data, |
| 41 | - $this->withSession | 41 | + $this->withSession, |
| 42 | + $this->signer | ||
| 42 | ); | 43 | ); |
| 43 | 44 | ||
| 44 | RequestEvent::dispatch($this->app, $request); | 45 | RequestEvent::dispatch($this->app, $request); |
src/Wap/Life/Coupon.php
0 → 100644
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace Lackoxygen\TiktokOpen\Wap\Life; | ||
| 4 | + | ||
| 5 | +use Lackoxygen\TiktokOpen\Base\ServiceManager; | ||
| 6 | +use Lackoxygen\TiktokOpen\Base\Signer\LifeSigner; | ||
| 7 | + | ||
| 8 | +class Coupon extends ServiceManager | ||
| 9 | +{ | ||
| 10 | + public function prepare(string $encryptedData = '', string $code = '') | ||
| 11 | + { | ||
| 12 | + return $this->app['client'] | ||
| 13 | + ->asQuery() | ||
| 14 | + ->withSession() | ||
| 15 | + //->signVia(new LifeSigner()) | ||
| 16 | + ->get('/goodlife/v1/fulfilment/certificate/prepare/', ['encrypted_data' => $encryptedData, 'code' => $code]); | ||
| 17 | + } | ||
| 18 | + | ||
| 19 | + public function verify(array $params) | ||
| 20 | + { | ||
| 21 | + return $this->app['client'] | ||
| 22 | + ->asJson() | ||
| 23 | + ->withSession() | ||
| 24 | + ->signVia(new LifeSigner()) | ||
| 25 | + ->post('goodlife/v1/fulfilment/certificate/verify/', $params); | ||
| 26 | + } | ||
| 27 | +} |
| @@ -5,20 +5,32 @@ namespace Lackoxygen\TiktokOpen\Wap; | @@ -5,20 +5,32 @@ namespace Lackoxygen\TiktokOpen\Wap; | ||
| 5 | use Lackoxygen\TiktokOpen\Base\Event\Request; | 5 | use Lackoxygen\TiktokOpen\Base\Event\Request; |
| 6 | use Lackoxygen\TiktokOpen\Base\Event\Response; | 6 | use Lackoxygen\TiktokOpen\Base\Event\Response; |
| 7 | use Lackoxygen\TiktokOpen\Base\Event\Fail; | 7 | use Lackoxygen\TiktokOpen\Base\Event\Fail; |
| 8 | +use Lackoxygen\TiktokOpen\Base\Signer\NoneSigner; | ||
| 8 | 9 | ||
| 9 | class Listener | 10 | class Listener |
| 10 | { | 11 | { |
| 11 | public function request(Request $event) | 12 | public function request(Request $event) |
| 12 | { | 13 | { |
| 13 | if ($event->request->isWithSession()) { | 14 | if ($event->request->isWithSession()) { |
| 15 | + $accessToken = $event->application['wap.session']->accessToken; | ||
| 14 | $event->request->setData( | 16 | $event->request->setData( |
| 15 | array_merge( | 17 | array_merge( |
| 16 | $event->request->getData(), | 18 | $event->request->getData(), |
| 17 | [ | 19 | [ |
| 18 | - 'access_token' => $event->application['wap.session']->accessToken | 20 | + 'access_token' => $accessToken |
| 19 | ] | 21 | ] |
| 20 | ) | 22 | ) |
| 21 | ); | 23 | ); |
| 24 | + $event->request->setHeader('access-token', $accessToken); | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + $signer = $event->request->getSigner(); | ||
| 28 | + | ||
| 29 | + if (!$signer instanceof NoneSigner) { | ||
| 30 | + $signer->carry( | ||
| 31 | + $event->application['config'], | ||
| 32 | + $event->request | ||
| 33 | + ); | ||
| 22 | } | 34 | } |
| 23 | } | 35 | } |
| 24 | 36 |
| @@ -4,7 +4,7 @@ namespace Lackoxygen\TiktokOpen\Wap; | @@ -4,7 +4,7 @@ namespace Lackoxygen\TiktokOpen\Wap; | ||
| 4 | 4 | ||
| 5 | use Lackoxygen\TiktokOpen\Base\AbstractProvider; | 5 | use Lackoxygen\TiktokOpen\Base\AbstractProvider; |
| 6 | use Lackoxygen\TiktokOpen\Base\Client\Client; | 6 | use Lackoxygen\TiktokOpen\Base\Client\Client; |
| 7 | -use Lackoxygen\TiktokOpen\Wap\{Data\Data, Js\Js, OAuth\OAuth, User\User, Video\Video}; | 7 | +use Lackoxygen\TiktokOpen\Wap\{Data\Data, Js\Js, Life\Coupon, OAuth\OAuth, User\User, Video\Video}; |
| 8 | 8 | ||
| 9 | /** | 9 | /** |
| 10 | * @method OAuth oauth() | 10 | * @method OAuth oauth() |
| @@ -24,7 +24,8 @@ class WapProvider extends AbstractProvider | @@ -24,7 +24,8 @@ class WapProvider extends AbstractProvider | ||
| 24 | 'user' => User::class, | 24 | 'user' => User::class, |
| 25 | 'video' => Video::class, | 25 | 'video' => Video::class, |
| 26 | 'data' => Data::class, | 26 | 'data' => Data::class, |
| 27 | - 'session' => Session::class | 27 | + 'session' => Session::class, |
| 28 | + 'lifeCoupon' => Coupon::class | ||
| 28 | ]; | 29 | ]; |
| 29 | 30 | ||
| 30 | public function __construct(string $alias) | 31 | public function __construct(string $alias) |
-
请 注册 或 登录 后发表评论