Browse Source

更新

master
wlb 2 years ago
parent
commit
35a37a8f35
  1. 1
      .gitignore
  2. 4
      src/Kernel/AccessToken.php
  3. 2
      src/Kernel/BaseClient.php
  4. 2
      src/Kernel/Extension.php
  5. 3
      src/Kernel/ServerGuard.php
  6. 2
      src/MiniProgram/Server/ServiceProvider.php
  7. 100
      src/OpenPlatform/Application.php
  8. 4
      src/OpenPlatform/Auth/ServiceProvider.php
  9. 38
      src/OpenPlatform/Authorizer/Auth/AccessToken.php
  10. 38
      src/OpenPlatform/Authorizer/MiniProgram/Account/Client.php
  11. 2
      src/OpenPlatform/Authorizer/MiniProgram/Account/ServiceProvider.php
  12. 2
      src/OpenPlatform/Authorizer/MiniProgram/Auth/ServiceProvider.php
  13. 56
      src/OpenPlatform/Authorizer/MiniProgram/Code/Client.php
  14. 2
      src/OpenPlatform/Authorizer/MiniProgram/Code/ServiceProvider.php
  15. 2
      src/OpenPlatform/Authorizer/MiniProgram/Domain/ServiceProvider.php
  16. 32
      src/OpenPlatform/Authorizer/Server/Guard.php
  17. 91
      src/OpenPlatform/Base/Client.php
  18. 2
      src/OpenPlatform/Base/ServiceProvider.php
  19. 2
      src/OpenPlatform/CodeTemplate/ServiceProvider.php
  20. 5
      src/OpenPlatform/Server/Guard.php
  21. 2
      src/OpenPlatform/Server/Handlers/VerifyTicketRefreshed.php
  22. 4
      src/OpenPlatform/Server/ServiceProvider.php

1
.gitignore vendored

@ -1,3 +1,4 @@ @@ -1,3 +1,4 @@
/.git
/.idea
/.vscode
/vendor

4
src/Kernel/AccessToken.php

@ -22,7 +22,7 @@ abstract class AccessToken implements AccessTokenInterface @@ -22,7 +22,7 @@ abstract class AccessToken implements AccessTokenInterface
use InteractsWithCache;
/**
* @var \Wechat\Kernel\ServiceContainer
* @var \Baidu\Kernel\ServiceContainer
*/
protected $app;
@ -54,7 +54,7 @@ abstract class AccessToken implements AccessTokenInterface @@ -54,7 +54,7 @@ abstract class AccessToken implements AccessTokenInterface
/**
* @var string
*/
protected $cachePrefix = 'wechat.kernel.access_token.';
protected $cachePrefix = 'baidu.kernel.access_token.';
/**
* AccessToken constructor.

2
src/Kernel/BaseClient.php

@ -24,7 +24,7 @@ class BaseClient @@ -24,7 +24,7 @@ class BaseClient
}
/**
* @var \Wechat\Kernel\ServiceContainer
* @var \Baidu\Kernel\ServiceContainer
*/
protected $app;

2
src/Kernel/Extension.php

@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
declare(strict_types=1);
namespace Wechat\Kernel;
namespace Baidu\Kernel;
use Baidu\Kernel\Contracts\EventHandlerInterface;
use Pimple\Container;

3
src/Kernel/ServerGuard.php

@ -67,7 +67,7 @@ class ServerGuard @@ -67,7 +67,7 @@ class ServerGuard
];
/**
* @var \Wechat\Kernel\ServiceContainer
* @var \Baidu\Kernel\ServiceContainer
*/
protected $app;
@ -349,6 +349,7 @@ class ServerGuard @@ -349,6 +349,7 @@ class ServerGuard
*/
protected function isSafeMode(): bool
{
var_dump($this->app['request']);die;
return !empty($this->app['request']->post('MsgSignature'));
}

2
src/MiniProgram/Server/ServiceProvider.php

@ -31,7 +31,7 @@ class ServiceProvider implements ServiceProviderInterface @@ -31,7 +31,7 @@ class ServiceProvider implements ServiceProviderInterface
};
!isset($app['server']) && $app['server'] = function ($app) {
return new Guard($app->resource);
return new Guard($app);
};
}
}

100
src/OpenPlatform/Application.php

@ -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);
}
}
}

4
src/OpenPlatform/Auth/ServiceProvider.php

@ -27,11 +27,11 @@ class ServiceProvider implements ServiceProviderInterface @@ -27,11 +27,11 @@ class ServiceProvider implements ServiceProviderInterface
public function register(Container $app)
{
$app['verify_ticket'] = function ($app) {
return new VerifyTicket($app->resource);
return new VerifyTicket($app);
};
$app['access_token'] = function ($app) {
return new AccessToken($app->resource);
return new AccessToken($app);
};
}
}

38
src/OpenPlatform/Authorizer/Auth/AccessToken.php

@ -12,11 +12,6 @@ @@ -12,11 +12,6 @@
namespace Baidu\OpenPlatform\Authorizer\Auth;
use Baidu\Kernel\AccessToken as BaseAccessToken;
use Baidu\OpenPlatform\Application;
use HttpBase\Exceptions\HttpException;
use Pimple\Container;
use Baidu\Kernel\Support\Collection;
use Baidu\Kernel\Traits\Observable;
use Baidu\Kernel\Support\Arr;
/**
@ -26,10 +21,6 @@ use Baidu\Kernel\Support\Arr; @@ -26,10 +21,6 @@ use Baidu\Kernel\Support\Arr;
*/
class AccessToken extends BaseAccessToken
{
use Observable;
const EVENT_ACCESS_TOKEN = 'access_token';
/**
* {@inheritdoc}.
*/
@ -40,19 +31,6 @@ class AccessToken extends BaseAccessToken @@ -40,19 +31,6 @@ class AccessToken extends BaseAccessToken
*/
protected $component;
/**
* AuthorizerAccessToken constructor.
*
* @param \Pimple\Container $app
* @param \Baidu\OpenPlatform\Application $component
*/
public function __construct(Container $app, Application $component)
{
parent::__construct($app);
$this->component = $component;
}
/**
* {@inheritdoc}.
*/
@ -64,22 +42,6 @@ class AccessToken extends BaseAccessToken @@ -64,22 +42,6 @@ class AccessToken extends BaseAccessToken
];
}
public function requestToken(array $credentials, $toArray = false)
{
$result = $formatted = $this->component->getAuthorizerToken($credentials);
if($formatted instanceof Collection){
$result = $formatted->toArray();
}
if (empty($result[$this->tokenKey])) {
throw new HttpException('Request access_token fail: '.json_encode($result, JSON_UNESCAPED_UNICODE));
}
$this->dispatch(static::EVENT_ACCESS_TOKEN, $result);
return $toArray ? $result : $formatted;
}
/**
* @return string
*/

38
src/OpenPlatform/Authorizer/MiniProgram/Account/Client.php

@ -27,42 +27,4 @@ class Client extends BaseClient @@ -27,42 +27,4 @@ class Client extends BaseClient
{
return $this->httpGet('rest/2.0/smartapp/app/info');
}
/**
* 修改头像.
* @param string $mediaId 头像素材mediaId
* @param float|int $left 剪裁框左上角x坐标(取值范围:[0, 1])
* @param float|int $top 剪裁框左上角y坐标(取值范围:[0, 1])
* @param float|int $right 剪裁框右下角x坐标(取值范围:[0, 1])
* @param float|int $bottom 剪裁框右下角y坐标(取值范围:[0, 1])
* @return array|\Baidu\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \HttpBase\Exceptions\InvalidConfigException
*/
public function updateAvatar(
string $mediaId,
float $left = 0,
float $top = 0,
float $right = 1,
float $bottom = 1
) {
$params = [
'head_img_media_id' => $mediaId,
'x1' => $left, 'y1' => $top, 'x2' => $right, 'y2' => $bottom,
];
return $this->httpPostJson('cgi-bin/account/modifyheadimage', $params);
}
/**
* 修改功能介绍.
*
* @param string $signature 功能介绍(简介)
*/
public function updateSignature(string $signature)
{
$params = ['signature' => $signature];
return $this->httpPostJson('cgi-bin/account/modifysignature', $params);
}
}

2
src/OpenPlatform/Authorizer/MiniProgram/Account/ServiceProvider.php

@ -19,7 +19,7 @@ class ServiceProvider implements ServiceProviderInterface @@ -19,7 +19,7 @@ class ServiceProvider implements ServiceProviderInterface
public function register(Container $app)
{
$app['account'] = function ($app) {
return new Client($app->resource);
return new Client($app);
};
}
}

2
src/OpenPlatform/Authorizer/MiniProgram/Auth/ServiceProvider.php

@ -10,7 +10,7 @@ class ServiceProvider implements ServiceProviderInterface @@ -10,7 +10,7 @@ class ServiceProvider implements ServiceProviderInterface
public function register(Container $pimple)
{
$pimple['auth'] = function ($app) {
return new Client($app->resource);
return new Client($app);
};
}
}

56
src/OpenPlatform/Authorizer/MiniProgram/Code/Client.php

@ -102,29 +102,6 @@ class Client extends BaseClient @@ -102,29 +102,6 @@ class Client extends BaseClient
return $this->httpPost('rest/2.0/smartapp/package/submitaudit', compact('package_id', 'content', 'remark'));
}
/**
* @param int $auditId
* @return array|\Baidu\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \HttpBase\Exceptions\InvalidConfigException
*/
public function getAuditStatus(int $auditId)
{
return $this->httpPostJson('wxa/get_auditstatus', [
'auditid' => $auditId,
]);
}
/**
* @return array|\Baidu\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \HttpBase\Exceptions\InvalidConfigException
*/
public function getLatestAuditStatus()
{
return $this->httpGet('wxa/get_latest_auditstatus');
}
/**
* @param string $package_id
* @return array|\Baidu\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
@ -135,37 +112,4 @@ class Client extends BaseClient @@ -135,37 +112,4 @@ class Client extends BaseClient
{
return $this->httpPost('rest/2.0/smartapp/package/release', compact('package_id'));
}
/**
* @return array|\Baidu\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \HttpBase\Exceptions\InvalidConfigException
*/
public function withdrawAudit()
{
return $this->httpGet('wxa/undocodeaudit');
}
/**
* @return array|\Baidu\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \HttpBase\Exceptions\InvalidConfigException
*/
public function rollbackRelease()
{
return $this->httpGet('wxa/revertcoderelease');
}
/**
* @param string $action
* @return array|\Baidu\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \HttpBase\Exceptions\InvalidConfigException
*/
public function changeVisitStatus(string $action)
{
return $this->httpPostJson('wxa/change_visitstatus', [
'action' => $action,
]);
}
}

2
src/OpenPlatform/Authorizer/MiniProgram/Code/ServiceProvider.php

@ -19,7 +19,7 @@ class ServiceProvider implements ServiceProviderInterface @@ -19,7 +19,7 @@ class ServiceProvider implements ServiceProviderInterface
public function register(Container $app)
{
$app['code'] = function ($app) {
return new Client($app->resource);
return new Client($app);
};
}
}

2
src/OpenPlatform/Authorizer/MiniProgram/Domain/ServiceProvider.php

@ -19,7 +19,7 @@ class ServiceProvider implements ServiceProviderInterface @@ -19,7 +19,7 @@ class ServiceProvider implements ServiceProviderInterface
public function register(Container $app)
{
$app['domain'] = function ($app) {
return new Client($app->resource);
return new Client($app);
};
}
}

32
src/OpenPlatform/Authorizer/Server/Guard.php

@ -0,0 +1,32 @@ @@ -0,0 +1,32 @@
<?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 Baidu\OpenPlatform\Authorizer\Server;
use Baidu\Kernel\ServerGuard;
/**
* Class Guard.
*
* @author mingyoung <mingyoungcheung@gmail.com>
*/
class Guard extends ServerGuard
{
/**
* Get token from OpenPlatform encryptor.
*
* @return string
*/
protected function getToken()
{
return $this->app['encryptor']->getToken();
}
}

91
src/OpenPlatform/Base/Client.php

@ -37,23 +37,6 @@ class Client extends BaseClient @@ -37,23 +37,6 @@ class Client extends BaseClient
return $this->httpGet('rest/2.0/oauth/token', $params);
}
/**
* Get authorizer info.
* @param string $appId
* @return array|\Baidu\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \HttpBase\Exceptions\InvalidConfigException
*/
public function getAuthorizer(string $appId)
{
$params = [
'component_appid' => $this->app['config']['app_id'],
'authorizer_appid' => $appId,
];
return $this->httpPostJson('cgi-bin/component/api_get_authorizer_info', $params);
}
/**
* Get authorizer access_token.
* @param array $credentials
@ -66,65 +49,6 @@ class Client extends BaseClient @@ -66,65 +49,6 @@ class Client extends BaseClient
return $this->httpGet('rest/2.0/oauth/token', $credentials);
}
/**
* Get options.
*
* @param string $appId
* @param string $name
*
* @return mixed
*/
public function getAuthorizerOption(string $appId, string $name)
{
$params = [
'component_appid' => $this->app['config']['app_id'],
'authorizer_appid' => $appId,
'option_name' => $name,
];
return $this->httpPostJson('cgi-bin/component/api_get_authorizer_option', $params);
}
/**
* Set authorizer option.
* @param string $appId
* @param string $name
* @param string $value
* @return array|\Baidu\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \HttpBase\Exceptions\InvalidConfigException
*/
public function setAuthorizerOption(string $appId, string $name, string $value)
{
$params = [
'component_appid' => $this->app['config']['app_id'],
'authorizer_appid' => $appId,
'option_name' => $name,
'option_value' => $value,
];
return $this->httpPostJson('cgi-bin/component/api_set_authorizer_option', $params);
}
/**
* Get authorizer list.
*
* @param int $offset
* @param int $count
*
* @return mixed
*/
public function getAuthorizers($offset = 0, $count = 500)
{
$params = [
'component_appid' => $this->app['config']['app_id'],
'offset' => $offset,
'count' => $count,
];
return $this->httpPostJson('cgi-bin/component/api_get_authorizer_list', $params);
}
/**
* Create pre-authorization code.
* @return array|\Baidu\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
@ -135,19 +59,4 @@ class Client extends BaseClient @@ -135,19 +59,4 @@ class Client extends BaseClient
{
return $this->httpGet('rest/2.0/smartapp/tp/createpreauthcode');
}
/**
* OpenPlatform Clear quota.
* @return array|\Baidu\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \HttpBase\Exceptions\InvalidConfigException
*/
public function clearQuota()
{
$params = [
'component_appid' => $this->app['config']['app_id'],
];
return $this->httpPostJson('cgi-bin/component/clear_quota', $params);
}
}

2
src/OpenPlatform/Base/ServiceProvider.php

@ -27,7 +27,7 @@ class ServiceProvider implements ServiceProviderInterface @@ -27,7 +27,7 @@ class ServiceProvider implements ServiceProviderInterface
public function register(Container $app)
{
$app['base'] = function ($app) {
return new Client($app->resource);
return new Client($app);
};
}
}

2
src/OpenPlatform/CodeTemplate/ServiceProvider.php

@ -19,7 +19,7 @@ class ServiceProvider implements ServiceProviderInterface @@ -19,7 +19,7 @@ class ServiceProvider implements ServiceProviderInterface
public function register(Container $app)
{
$app['code_template'] = function ($app) {
return new Client($app->resource);
return new Client($app);
};
}
}

5
src/OpenPlatform/Server/Guard.php

@ -29,7 +29,10 @@ class Guard extends ServerGuard @@ -29,7 +29,10 @@ class Guard extends ServerGuard
const EVENT_COMPONENT_VERIFY_TICKET = 'ticket';
/**
* @return \Symfony\Component\HttpFoundation\Response
* @return Response
* @throws \HttpBase\Exceptions\BadRequestException
* @throws \HttpBase\Exceptions\InvalidArgumentException
* @throws \HttpBase\Exceptions\InvalidConfigException
*/
protected function resolve(): Response
{

2
src/OpenPlatform/Server/Handlers/VerifyTicketRefreshed.php

@ -11,7 +11,7 @@ @@ -11,7 +11,7 @@
namespace Baidu\OpenPlatform\Server\Handlers;
use Baidu\Kernel\Contracts\EventHandlerInterface;
use HttpBase\Contracts\EventHandlerInterface;
use Baidu\Kernel\ServiceContainer;
/**

4
src/OpenPlatform/Server/ServiceProvider.php

@ -11,7 +11,7 @@ @@ -11,7 +11,7 @@
namespace Baidu\OpenPlatform\Server;
use Baidu\OpenPlatform\Encryptor;
use Baidu\Kernel\Encryptor;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
@ -28,7 +28,7 @@ class ServiceProvider implements ServiceProviderInterface @@ -28,7 +28,7 @@ class ServiceProvider implements ServiceProviderInterface
};
$app['server'] = function ($app) {
return new Guard($app->resource);
return new Guard($app);
};
}
}

Loading…
Cancel
Save