ServiceProvider.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Libs\TikTok\OpenPlatform\OAuth;
  3. use Overtrue\Socialite\SocialiteManager as Socialite;
  4. use Pimple\Container;
  5. use Pimple\ServiceProviderInterface;
  6. class ServiceProvider implements ServiceProviderInterface
  7. {
  8. /**
  9. * {@inheritdoc}.
  10. */
  11. public function register(Container $app)
  12. {
  13. $app['oauth'] = function ($app) {
  14. $douyin = [
  15. 'douyin' => [
  16. 'client_id' => $app['config']['app_id'],
  17. 'client_secret' => $app['config']['secret'],
  18. 'redirect' => $this->prepareCallbackUrl($app),
  19. ],
  20. ];
  21. $socialite = (new Socialite($douyin))->create('douyin');
  22. $scopes = (array)$app['config']->get('oauth.scopes', ['user_info']);
  23. if (!empty($scopes)) {
  24. $socialite->scopes($scopes);
  25. }
  26. return $socialite;
  27. };
  28. }
  29. /**
  30. * Prepare the OAuth callback url for wechat.
  31. *
  32. * @param Container $app
  33. *
  34. * @return string
  35. */
  36. private function prepareCallbackUrl($app)
  37. {
  38. $callback = $app['config']->get('oauth.callback');
  39. if (0 === stripos($callback, 'http')) {
  40. return $callback;
  41. }
  42. $baseUrl = $app['request']->getSchemeAndHttpHost();
  43. return $baseUrl . '/' . ltrim($callback, '/');
  44. }
  45. }