<?php
/*
* This file is part of the overtrue/wechat.
*
* (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 EasyWeChat\OpenPlatform;
use EasyWeChat\Kernel\ServiceContainer;
use EasyWeChat\MiniProgram\Encryptor;
use EasyWeChat\OpenPlatform\Authorizer\Auth\AccessToken;
use EasyWeChat\OpenPlatform\Authorizer\MiniProgram\Application as MiniProgram;
use EasyWeChat\OpenPlatform\Authorizer\MiniProgram\Auth\Client;
use EasyWeChat\OpenPlatform\Authorizer\OfficialAccount\Account\Client as AccountClient;
use EasyWeChat\OpenPlatform\Authorizer\OfficialAccount\Application as OfficialAccount;
use EasyWeChat\OpenPlatform\Authorizer\OfficialAccount\OAuth\ComponentDelegate;
use EasyWeChat\OpenPlatform\Authorizer\Server\Guard;
class Application extends ServiceContainer
{
protected $providers = [
Auth\ServiceProvider::class,
Base\ServiceProvider::class,
Server\ServiceProvider::class,
CodeTemplate\ServiceProvider::class,
];
protected $defaultConfig = [
'http' => [
'timeout' => 5.0,
'base_uri' => 'https://api.weixin.qq.com/',
],
];
public function officialAccount(string $appId, string $refreshToken = null, AccessToken $accessToken = null): OfficialAccount
{
$application = new OfficialAccount($this->getAuthorizerConfig($appId, $refreshToken), $this->getReplaceServices($accessToken) + [
'encryptor' => $this['encryptor'],
'account' => function ($app) {
return new AccountClient($app, $this);
},
]);
$application->extend('oauth', function ($socialite) {
return $socialite->component(new ComponentDelegate($this));
});
return $application;
}
public function miniProgram(string $appId, string $refreshToken = null, AccessToken $accessToken = null): MiniProgram
{
return new MiniProgram($this->getAuthorizerConfig($appId, $refreshToken), $this->getReplaceServices($accessToken) + [
'encryptor' => function () {
return new Encryptor($this['config']['app_id'], $this['config']['token'], $this['config']['aes_key']);
},
'auth' => function ($app) {
return new Client($app, $this);
},
]);
}
public function getPreAuthorizationUrl(string $callbackUrl, $optional = []): string
{
if (\is_string($optional)) {
$optional = [
'pre_auth_code' => $optional,
];
} else {
$optional['pre_auth_code'] = $this->createPreAuthorizationCode()['pre_auth_code'];
}
$queries = \array_merge($optional, [
'component_appid' => $this['config']['app_id'],
'redirect_uri' => $callbackUrl,
]);
return 'https://mp.weixin.qq.com/cgi-bin/componentloginpage?'.http_build_query($queries);
}
public function getMobilePreAuthorizationUrl(string $callbackUrl, $optional = []): string
{
if (\is_string($optional)) {
$optional = [
'pre_auth_code' => $optional,
];
} else {
$optional['pre_auth_code'] = $this->createPreAuthorizationCode()['pre_auth_code'];
}
$queries = \array_merge($optional, [
'component_appid' => $this['config']['app_id'],
'redirect_uri' => $callbackUrl,
'action' => 'bindcomponent',
'no_scan' => 1,
]);
return 'https://mp.weixin.qq.com/safe/bindcomponent?'.http_build_query($queries).'#wechat_redirect';
}
protected function getAuthorizerConfig(string $appId, string $refreshToken = null): array
{
return [
'debug' => $this['config']->get('debug', false),
'response_type' => $this['config']->get('response_type'),
'log' => $this['config']->get('log', []),
'app_id' => $appId,
'refresh_token' => $refreshToken,
];
}
protected function getReplaceServices(AccessToken $accessToken = null): array
{
$services = [
'access_token' => $accessToken ?: function ($app) {
return new AccessToken($app, $this);
},
'server' => function ($app) {
return new Guard($app);
},
];
foreach (['cache', 'http_client', 'log', 'logger', 'request'] as $reuse) {
if (isset($this[$reuse])) {
$services[$reuse] = $this[$reuse];
}
}
return $services;
}
public function __call($method, $args)
{
return call_user_func_array([$this['base'], $method], $args);
}
}