审查视图

vendor/overtrue/socialite/src/Providers/WeChatOpenPlatformProvider.php 2.9 KB
景龙 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
<?php

/*
 * This file is part of the overtrue/socialite.
 *
 * (c) overtrue <i@overtrue.me>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Overtrue\Socialite\Providers;

use Symfony\Component\HttpFoundation\Request;

/**
 * Class WeChatProvider.
 *
 * @see https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419318590&token=&lang=zh_CN [WeChat - 公众开放平台代公众号 OAuth 文档]
 */
class WeChatOpenPlatformProvider extends WeChatProvider
{
    /**
     * Component AppId.
     *
     * @deprecated 2.0 Will be removed in the future
     *
     * @var string
     */
    protected $componentAppId;

    /**
     * Component Access Token.
     *
     * @deprecated 2.0 Will be removed in the future
     *
     * @var string
     */
    protected $componentAccessToken;

    /**
     * @var \EasyWeChat\OpenPlatform\AccessToken|array
     */
    protected $credentials;

    /**
     * {@inheritdoc}.
     */
    protected $scopes = ['snsapi_base'];

    /**
     * Create a new provider instance.
     * (Overriding).
     *
     * @param \Symfony\Component\HttpFoundation\Request  $request
     * @param string                                     $clientId
     * @param \EasyWeChat\OpenPlatform\AccessToken|array $credentials
     * @param string|null                                $redirectUrl
     */
    public function __construct(Request $request, $clientId, $credentials, $redirectUrl = null)
    {
        parent::__construct($request, $clientId, null, $redirectUrl);

        $this->credentials = $credentials;
        if (is_array($credentials)) {
            list($this->componentAppId, $this->componentAccessToken) = $credentials;
        }
    }

    /**
     * {@inheritdoc}.
     */
    public function getCodeFields($state = null)
    {
        $this->with(['component_appid' => $this->componentAppId()]);

        return parent::getCodeFields($state);
    }

    /**
     * {@inheritdoc}.
     */
    protected function getTokenUrl()
    {
        return $this->baseUrl.'/oauth2/component/access_token';
    }

    /**
     * {@inheritdoc}.
     */
    protected function getTokenFields($code)
    {
        return [
            'appid' => $this->clientId,
            'component_appid' => $this->componentAppId(),
            'component_access_token' => $this->componentAccessToken(),
            'code' => $code,
            'grant_type' => 'authorization_code',
        ];
    }

    /**
     * Get component app id.
     *
     * @return string
     */
    protected function componentAppId()
    {
        return $this->componentAppId ?: $this->credentials->getAppId();
    }

    /**
     * Get component access token.
     *
     * @return string
     */
    protected function componentAccessToken()
    {
        return $this->componentAccessToken ?: $this->credentials->getToken();
    }
}