Skip to content

Commit

Permalink
完善小程序代码 (#566)
Browse files Browse the repository at this point in the history
* Improved base template message compatibility.

* Add json data type.

* 稍微完善下

* fix styles

* fix unused parameter
  • Loading branch information
mingyoung authored and overtrue committed Jan 10, 2017
1 parent ee5cc42 commit 718b8e9
Show file tree
Hide file tree
Showing 13 changed files with 470 additions and 37 deletions.
23 changes: 22 additions & 1 deletion src/Foundation/ServiceProviders/MiniProgramServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@

namespace EasyWeChat\Foundation\ServiceProviders;

use EasyWeChat\Encryption\Encryptor;
use EasyWeChat\MiniProgram\AccessToken;
use EasyWeChat\MiniProgram\MiniProgram;
use EasyWeChat\MiniProgram\Server\Guard;
use Pimple\Container;
use Pimple\ServiceProviderInterface;

Expand Down Expand Up @@ -52,10 +54,29 @@ public function register(Container $pimple)
);
};

$pimple['mini_program_encryptor'] = function ($pimple) {
$config = $pimple['config']->get('mini_program');

return new Encryptor(
$config['app_id'],
$config['token'],
$config['aes_key']
);
};

$pimple['mini_program'] = function ($pimple) {
$config = $pimple['config']->get('mini_program');

$server = new Guard($config['token']);

$server->debug($pimple['config']['debug']);

$server->setEncryptor($pimple['mini_program_encryptor']);

return new MiniProgram(
$server,
$pimple['mini_program_access_token'],
$pimple['config']->get('mini_program')
$config
);
};
}
Expand Down
4 changes: 4 additions & 0 deletions src/MiniProgram/AccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@
*/
class AccessToken extends CoreAccessToken
{
/**
* {@inheritdoc}.
*/
protected $prefix = 'easywechat.common.mini.program.access_token.';
}
52 changes: 52 additions & 0 deletions src/MiniProgram/Core/AbstractMiniProgram.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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.
*/

/**
* AbstractMiniProgram.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\Core;

use EasyWeChat\Core\AbstractAPI;

class AbstractMiniProgram extends AbstractAPI
{
/**
* Mini program config.
*
* @var array
*/
protected $config;

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

$this->config = $config;
}
}
36 changes: 36 additions & 0 deletions src/MiniProgram/Material/Temporary.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.
*/

/**
* Temporary.php.
*
* @author mingyoung <mingyoungcheung@gmail.com>
*
* @see https://github.com/overtrue
* @see http://overtrue.me
*/

namespace EasyWeChat\MiniProgram\Material;

use EasyWeChat\Material\Temporary as BaseTemporary;

/**
* Class Temporary.
*/
class Temporary extends BaseTemporary
{
public function __construct()
{
$accessToken = func_get_args()[0];

parent::__construct($accessToken);
}
}
79 changes: 60 additions & 19 deletions src/MiniProgram/MiniProgram.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,32 @@

namespace EasyWeChat\MiniProgram;

use EasyWeChat\Core\AbstractAPI;
use EasyWeChat\Core\Exceptions\InvalidArgumentException;
use EasyWeChat\MiniProgram\Material\Temporary;
use EasyWeChat\MiniProgram\Notice\Notice;
use EasyWeChat\MiniProgram\QRCode\QRCode;
use EasyWeChat\MiniProgram\Staff\Staff;
use EasyWeChat\MiniProgram\User\User;
use EasyWeChat\Support\Arr;

/**
* Class MiniProgram.
*
* @property \EasyWeChat\MiniProgram\Server\Guard $server
* @property \EasyWeChat\MiniProgram\User\User $user
* @property \EasyWeChat\MiniProgram\Notice\Notice $notice
* @property \EasyWeChat\MiniProgram\Staff\Staff $staff
* @property \EasyWeChat\MiniProgram\QRCode\QRCode $qrcode
* @property \EasyWeChat\MiniProgram\Material\Temporary $material_temporary
*/
class MiniProgram extends AbstractAPI
class MiniProgram
{
/**
* Api.
* Access Token.
*
* @var \EasyWeChat\MiniProgram\AccessToken
*/
const JSCODE_TO_SESSION = 'https://api.weixin.qq.com/sns/jscode2session';
protected $accessToken;

/**
* Mini program config.
Expand All @@ -45,35 +60,61 @@ class MiniProgram extends AbstractAPI
*/
protected $config;

/**
* Guard.
*
* @var \EasyWeChat\MiniProgram\Server\Guard
*/
protected $server;

/**
* Components.
*
* @var array
*/
protected $components = [
'user' => User::class,
'notice' => Notice::class,
'staff' => Staff::class,
'qrcode' => QRCode::class,
'material_temporary' => Temporary::class,
];

/**
* MiniProgram constructor.
*
* @param \EasyWeChat\MiniProgram\AccessToken $accessToken
* @param array $config
* @param \EasyWeChat\MiniProgram\Server\Guard $server
* @param \EasyWeChat\MiniProgram\AccessToken $accessToken
* @param array $config
*/
public function __construct($accessToken, $config)
public function __construct($server, $accessToken, $config)
{
parent::__construct($accessToken);
$this->server = $server;

$this->accessToken = $accessToken;

$this->config = $config;
}

/**
* JsCode 2 session key.
* Magic get access.
*
* @param $name
*
* @param string $jsCode
* @return mixed
*
* @return \EasyWeChat\Support\Collection
* @throws InvalidArgumentException
*/
public function getSessionKey($jsCode)
public function __get($name)
{
$params = [
'appid' => $this->config['app_id'],
'secret' => $this->config['secret'],
'js_code' => $jsCode,
'grant_type' => 'authorization_code',
];
if (property_exists($this, $name)) {
return $this->$name;
}

if ($class = Arr::get($this->components, $name)) {
return new $class($this->accessToken, $this->config);
}

return $this->parseJSON('GET', [self::JSCODE_TO_SESSION, $params]);
throw new InvalidArgumentException("Property or component \"$name\" does not exists.");
}
}
71 changes: 71 additions & 0 deletions src/MiniProgram/Notice/Notice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?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.
*/

/**
* Notice.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\Notice;

use EasyWeChat\Notice\Notice as BaseNotice;

class Notice extends BaseNotice
{
/**
* {@inheritdoc}.
*/
protected $message = [
'touser' => '',
'template_id' => '',
'page' => '',
'form_id' => '',
'data' => [],
'emphasis_keyword' => '',
];

/**
* {@inheritdoc}.
*/
protected $defaults = [
'touser' => '',
'template_id' => '',
'form_id' => '',
'data' => [],
];

/**
* {@inheritdoc}.
*/
protected $required = ['touser', 'template_id', 'form_id'];

public function __construct()
{
$accessToken = func_get_args()[0];

parent::__construct($accessToken);
}

/**
* Send notice message.
*/
const API_SEND_NOTICE = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send';
}
50 changes: 50 additions & 0 deletions src/MiniProgram/QRCode/QRCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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.
*/

/**
* QRCode.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\QRCode;

use EasyWeChat\MiniProgram\Core\AbstractMiniProgram;

class QRCode extends AbstractMiniProgram
{
/**
* API.
*/
const API_CREATE_QRCODE = 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode';

/**
* Create mini program qrcode.
*
* @param $path
* @param int $width
*
* @return \EasyWeChat\Support\Collection
*/
public function create($path, $width = 430)
{
return $this->parseJSON('POST', [self::API_CREATE_QRCODE, compact('path', 'width')]);
}
}
Loading

0 comments on commit 718b8e9

Please sign in to comment.