|
|
|
@ -11,18 +11,18 @@
@@ -11,18 +11,18 @@
|
|
|
|
|
|
|
|
|
|
namespace Baidu\OpenPlatform; |
|
|
|
|
|
|
|
|
|
use Baidu\Kernel\Encryptor; |
|
|
|
|
use Baidu\Kernel\ServiceContainer; |
|
|
|
|
use Baidu\OpenPlatform\Authorizer\MiniProgram\Application as MiniProgram; |
|
|
|
|
use Baidu\MiniProgram\Encryptor; |
|
|
|
|
use Baidu\OpenPlatform\Authorizer\Auth\AccessToken; |
|
|
|
|
use Baidu\OpenPlatform\Authorizer\MiniProgram\Application as MiniProgram; |
|
|
|
|
use Baidu\OpenPlatform\Authorizer\MiniProgram\Auth\Client; |
|
|
|
|
use Baidu\OpenPlatform\Server\Guard; |
|
|
|
|
use Baidu\OpenPlatform\Authorizer\Server\Guard; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Class Application. |
|
|
|
|
* |
|
|
|
|
* @property \Baidu\OpenPlatform\Server\Guard $server |
|
|
|
|
* @property \Baidu\OpenPlatform\Auth\AccessToken $access_token |
|
|
|
|
* @property \Baidu\OpenPlatform\Server\Guard $server |
|
|
|
|
* @property \Baidu\OpenPlatform\Auth\AccessToken $access_token |
|
|
|
|
* @property \Baidu\OpenPlatform\CodeTemplate\Client $code_template |
|
|
|
|
* |
|
|
|
|
* @method mixed handleAuthorize(string $authCode = null) |
|
|
|
@ -31,6 +31,7 @@ use Baidu\OpenPlatform\Server\Guard;
@@ -31,6 +31,7 @@ use Baidu\OpenPlatform\Server\Guard;
|
|
|
|
|
* @method mixed setAuthorizerOption(string $appId, string $name, string $value) |
|
|
|
|
* @method mixed getAuthorizers(int $offset = 0, int $count = 500) |
|
|
|
|
* @method mixed createPreAuthorizationCode() |
|
|
|
|
* @method mixed getAuthorizerToken(array $credentials) |
|
|
|
|
*/ |
|
|
|
|
class Application extends ServiceContainer |
|
|
|
|
{ |
|
|
|
@ -50,12 +51,22 @@ class Application extends ServiceContainer
@@ -50,12 +51,22 @@ class Application extends ServiceContainer
|
|
|
|
|
protected $defaultConfig = [ |
|
|
|
|
'http' => [ |
|
|
|
|
'timeout' => 5.0, |
|
|
|
|
'base_uri' => 'https://openapi.baidu.com/', |
|
|
|
|
'base_uri' => 'https://api.weixin.qq.com/', |
|
|
|
|
], |
|
|
|
|
]; |
|
|
|
|
public function miniProgram(string $appId, string $refreshToken = null, AccessToken $accessToken = null): MiniProgram |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Creates the miniProgram application. |
|
|
|
|
* |
|
|
|
|
* @param string|null $refreshToken |
|
|
|
|
* @param \Baidu\OpenPlatform\Authorizer\Auth\AccessToken|null $accessToken |
|
|
|
|
* |
|
|
|
|
* @return \Baidu\OpenPlatform\Authorizer\MiniProgram\Application |
|
|
|
|
*/ |
|
|
|
|
public function miniProgram(string $refreshToken = null, AccessToken $accessToken = null): MiniProgram |
|
|
|
|
{ |
|
|
|
|
return new MiniProgram($this['config'], $this->getReplaceServices($accessToken) + [ |
|
|
|
|
return new MiniProgram($this->getAuthorizerConfig($refreshToken), $this->getReplaceServices($accessToken) + [ |
|
|
|
|
'encryptor' => function () { |
|
|
|
|
return new Encryptor($this['config']['app_id'], $this['config']['token'], $this['config']['aes_key']); |
|
|
|
|
}, |
|
|
|
@ -67,7 +78,49 @@ class Application extends ServiceContainer
@@ -67,7 +78,49 @@ class Application extends ServiceContainer
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @param AccessToken|null $accessToken |
|
|
|
|
* Return the pre-authorization login page url. |
|
|
|
|
* |
|
|
|
|
* @param string $callbackUrl |
|
|
|
|
* @param string|array|null $optional |
|
|
|
|
* |
|
|
|
|
* @return string |
|
|
|
|
*/ |
|
|
|
|
public function getPreAuthorizationUrl(string $callbackUrl, $optional = []): string |
|
|
|
|
{ |
|
|
|
|
// 兼容旧版 API 设计 |
|
|
|
|
if (\is_string($optional)) { |
|
|
|
|
$optional = [ |
|
|
|
|
'pre_auth_code' => $optional, |
|
|
|
|
]; |
|
|
|
|
} else { |
|
|
|
|
$optional['pre_auth_code'] = $this->createPreAuthorizationCode()['pre_auth_code']; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$queries = \array_merge($optional, [ |
|
|
|
|
'client_id' => $this['config']['app_key'], |
|
|
|
|
'redirect_uri' => $callbackUrl, |
|
|
|
|
]); |
|
|
|
|
|
|
|
|
|
return 'https://smartprogram.baidu.com/mappconsole/tp/authorization?'.http_build_query($queries); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @param string $appId |
|
|
|
|
* @param string|null $refreshToken |
|
|
|
|
* |
|
|
|
|
* @return array |
|
|
|
|
*/ |
|
|
|
|
protected function getAuthorizerConfig(string $refreshToken = null): array |
|
|
|
|
{ |
|
|
|
|
return $this['config']->merge([ |
|
|
|
|
'component_app_id' => $this['config']['app_id'], |
|
|
|
|
'refresh_token' => $refreshToken, |
|
|
|
|
])->toArray(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @param \Baidu\OpenPlatform\Authorizer\Auth\AccessToken|null $accessToken |
|
|
|
|
* |
|
|
|
|
* @return array |
|
|
|
|
*/ |
|
|
|
|
protected function getReplaceServices(AccessToken $accessToken = null): array |
|
|
|
@ -92,26 +145,15 @@ class Application extends ServiceContainer
@@ -92,26 +145,15 @@ class Application extends ServiceContainer
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @param string $callbackUrl |
|
|
|
|
* @param array $optional |
|
|
|
|
* @return string |
|
|
|
|
* Handle dynamic calls. |
|
|
|
|
* |
|
|
|
|
* @param string $method |
|
|
|
|
* @param array $args |
|
|
|
|
* |
|
|
|
|
* @return mixed |
|
|
|
|
*/ |
|
|
|
|
public function getPreAuthorizationUrl(string $callbackUrl, $optional = []): string |
|
|
|
|
public function __call($method, $args) |
|
|
|
|
{ |
|
|
|
|
// 兼容旧版 API 设计 |
|
|
|
|
if (\is_string($optional)) { |
|
|
|
|
$optional = [ |
|
|
|
|
'pre_auth_code' => $optional, |
|
|
|
|
]; |
|
|
|
|
} else { |
|
|
|
|
$optional['pre_auth_code'] = $this->createPreAuthorizationCode()['data']['pre_auth_code']; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$queries = \array_merge($optional, [ |
|
|
|
|
'client_id' => $this['config']['app_key'], |
|
|
|
|
'redirect_uri' => $callbackUrl, |
|
|
|
|
]); |
|
|
|
|
|
|
|
|
|
return 'https://smartprogram.baidu.com/mappconsole/tp/authorization?' . http_build_query($queries); |
|
|
|
|
return $this->base->$method(...$args); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |