Skip to content

Commit

Permalink
添加小程序支持 (#564)
Browse files Browse the repository at this point in the history
* 添加小程序支持
使用微信一致的命名 `mini_program`;
暂实现 session_key 和 access_token 的获取。

* fix styles
  • Loading branch information
mingyoung authored and overtrue committed Jan 9, 2017
1 parent 6971e7b commit 8d77a8f
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
* @property \EasyWeChat\ShakeAround\ShakeAround $shakearound
* @property \EasyWeChat\User\MiniAppUser $mini_app_user
* @property \EasyWeChat\OpenPlatform\OpenPlatform $open_platform
* @property \EasyWeChat\MiniProgram\MiniProgram $mini_program
*/
class Application extends Container
{
Expand Down Expand Up @@ -98,6 +99,7 @@ class Application extends Container
ServiceProviders\ShakeAroundServiceProvider::class,
ServiceProviders\MiniAppUserServiceProvider::class,
ServiceProviders\OpenPlatformServiceProvider::class,
ServiceProviders\MiniProgramServiceProvider::class,
];

/**
Expand Down
62 changes: 62 additions & 0 deletions src/Foundation/ServiceProviders/MiniProgramServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?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.
*/

/**
* MiniProgramServiceProvider.php.
*
* This file is part of the wechat.
*
* (c) mingyoung <mingyoungcheung@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace EasyWeChat\Foundation\ServiceProviders;

use EasyWeChat\MiniProgram\AccessToken;
use EasyWeChat\MiniProgram\MiniProgram;
use Pimple\Container;
use Pimple\ServiceProviderInterface;

/**
* Class MiniProgramServiceProvider.
*/
class MiniProgramServiceProvider implements ServiceProviderInterface
{
/**
* Registers services on the given container.
*
* This method should only be used to configure services and parameters.
* It should not get services.
*
* @param Container $pimple A container instance
*/
public function register(Container $pimple)
{
$pimple['mini_program_access_token'] = function ($pimple) {
$config = $pimple['config']->get('mini_program');

return new AccessToken(
$config['app_id'],
$config['secret'],
$pimple['cache']
);
};

$pimple['mini_program'] = function ($pimple) {
return new MiniProgram(
$pimple['mini_program_access_token'],
$pimple['config']->get('mini_program')
);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
use Pimple\ServiceProviderInterface;

/**
* Class BroadcastServiceProvider.
* Class OpenPlatformServiceProvider.
*/
class OpenPlatformServiceProvider implements ServiceProviderInterface
{
Expand Down
36 changes: 36 additions & 0 deletions src/MiniProgram/AccessToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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.
*/

/**
* AccessToken.php.
*
* Part of Overtrue\WeChat.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author mingyoung <mingyoungcheung@gmail.com>
* @copyright 2016
*
* @see https://github.com/overtrue
* @see http://overtrue.me
*/

namespace EasyWeChat\MiniProgram;

use EasyWeChat\Core\AccessToken as CoreAccessToken;

/**
* Class AccessToken.
*/
class AccessToken extends CoreAccessToken
{
}
79 changes: 79 additions & 0 deletions src/MiniProgram/MiniProgram.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?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.
*/

/**
* MiniProgram.php.
*
* Part of Overtrue\WeChat.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author mingyoung <mingyoungcheung@gmail.com>
* @copyright 2016
*
* @see https://github.com/overtrue
* @see http://overtrue.me
*/

namespace EasyWeChat\MiniProgram;

use EasyWeChat\Core\AbstractAPI;

/**
* Class MiniProgram.
*/
class MiniProgram extends AbstractAPI
{
/**
* Api.
*/
const JSCODE_TO_SESSION = 'https://api.weixin.qq.com/sns/jscode2session';

/**
* Mini program config.
*
* @var array
*/
protected $config;

/**
* MiniProgram constructor.
*
* @param \EasyWeChat\MiniProgram\AccessToken $accessToken
* @param array $config
*/
public function __construct($accessToken, $config)
{
parent::__construct($accessToken);

$this->config = $config;
}

/**
* JsCode 2 session key.
*
* @param string $jsCode
*
* @return \EasyWeChat\Support\Collection
*/
public function getSessionKey($jsCode)
{
$params = [
'appid' => $this->config['app_id'],
'secret' => $this->config['secret'],
'js_code' => $jsCode,
'grant_type' => 'authorization_code',
];

return $this->parseJSON('GET', [self::JSCODE_TO_SESSION, $params]);
}
}

0 comments on commit 8d77a8f

Please sign in to comment.