Skip to content

Commit

Permalink
添加模板消息
Browse files Browse the repository at this point in the history
  • Loading branch information
thenbsp committed Oct 31, 2015
1 parent 0cb29c4 commit 1f957b1
Show file tree
Hide file tree
Showing 6 changed files with 333 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ composer require thenbsp/wechat
- 消息管理
- [消息实体](/documentation/message-entity.md)
- [消息事件](/documentation/message-event.md)
- [模板消息](/documentation/message-template.md)

_其它功能开发中,表着急 ..._
61 changes: 61 additions & 0 deletions documentation/message-template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# 模板消息

定义模板参数:

```php
use Thenbsp\Wechat\Message\Template\TemplateOption;

$templateOption = new TemplateOption();
$templateOption->add('参数名称', '参数值', '参数颜色');
```

发送模板消息(需要注入全局 AccessToken):

```php
use Thenbsp\Wechat\Message\Template\Template;
use Thenbsp\Wechat\Message\Template\TemplateOption;

$templateOption = new TemplateOption();
$templateOption->add('name', '张三', '#ff0000');
$templateOption->add('remark', '李四', '#0000ff');

// 注入 AccessToken
$template = new Template($accessToken);
// 注入参数
$template->setOptions($templateOption);
// 用户 Openid
$template->setTouser('接收者用户 OpenID');
// 模板 ID
$template->setTemplateId('模板 ID');
// 消息跳转链接
$template->setUrl('消息链接');
// 发送
$template->send();
```

如果发送失败将抛出 \Exception 异常,成功将返回 msgid

```php
try {
$msgid = $template->send();
} catch (\Exception $e) {
exit($e->getMessage());
}
var_dump($msgid);
```

TemplateOption::toArray 方法可查看参数 Array:

```php
var_dump($templateOption->toArray());
```

TemplateOption::toJSON 方法可查看参数 JSON:

```php
var_dump($templateOption->toJSON());
```

示例:

[/example/message-template.php](/example/message-template.php)
24 changes: 24 additions & 0 deletions example/message-template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

require './example.php';

use Thenbsp\Wechat\Message\Template\Template;
use Thenbsp\Wechat\Message\Template\TemplateOption;

$templateOption = new TemplateOption();
$templateOption->add('name', '张三', '#ff0000');
$templateOption->add('remark', '李四', 'green');

$template = new Template($accessToken);
$template->setTouser('oWY-5jjLjo7pYUK86JPpwvcnF2Js');
$template->setTemplateId('K2M_za8UDzSMfvKMupZmgOBTpO1yJHXYdYDDY3aQl_8');
$template->setUrl('http://cn.bing.com/');
$template->setOptions($templateOption);

try {
$msgid = $template->send();
} catch (\Exception $e) {
exit($e->getMessage());
}

var_dump("发送成功:#{$msgid}");
147 changes: 147 additions & 0 deletions src/Message/Template/Template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

namespace Thenbsp\Wechat\Message\Template;

use Thenbsp\Wechat\Util\Http;
use Thenbsp\Wechat\Util\Serialize;
use Thenbsp\Wechat\AccessToken;
use Thenbsp\Wechat\Message\Template\TemplateOption;

class Template
{
/**
* 发送接口 URL
*/
const SEND_URL = 'https://api.weixin.qq.com/cgi-bin/message/template/send';

/**
* 全局 AccessToken
*/
protected $accessToken;

/**
* 用户 ID
*/
protected $touser;

/**
* 模板 ID
*/
protected $templateId;

/**
* 跳转 URL
*/
protected $url;

/**
* 选项参数
* Thenbsp\Wechat\Message\TemplateOption
*/
protected $options;

/**
* 构造方法
*/
public function __construct(AccessToken $accessToken)
{
$this->accessToken = $accessToken;
}

/**
* 设置用户 ID
*/
public function setTouser($touser)
{
$this->touser = $touser;
}

/**
* 获取用户 ID
*/
public function getTouser()
{
return $this->touser;
}

/**
* 设置模板 ID
*/
public function setTemplateId($templateId)
{
$this->templateId = $templateId;
}

/**
* 获取模板 ID
*/
public function getTemplateId()
{
return $this->templateId;
}

/**
* 设置跳转 URL
*/
public function setUrl($url)
{
$this->url = $url;
}

/**
* 获取跳转 URL
*/
public function getUrl()
{
return $this->url;
}

/**
* 设置选项参数
*/
public function setOptions(TemplateOption $options)
{
$this->options = $options;
}

/**
* 获取选项参数
*/
public function getOptions()
{
return $this->options;
}

/**
* 发送模板消息
*/
public function send()
{
if( empty($this->options) || $this->options->isEmpty() ) {
throw new \Exception('Options is requried');
}

$body = array(
'touser' => $this->touser,
'template_id' => $this->templateId,
'url' => $this->url,
'data' => $this->options->toArray()
);

$request = Http::post(self::SEND_URL, array(
'body' => Serialize::encode($body, 'json'),
'query' => array(
'access_token' => $this->accessToken->getAccessToken()
)
));

$response = $request->json();

if( array_key_exists('errcode', $response) &&
($response['errcode'] != 0) ) {
throw new \Exception($response['errcode'].': '.$response['errmsg']);
}

return $response['msgid'];
}
}
69 changes: 69 additions & 0 deletions src/Message/Template/TemplateOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Thenbsp\Wechat\Message\Template;

use Thenbsp\Wechat\Util\Serialize;

class TemplateOption implements TemplateOptionInterface
{
/**
* 选项容器
*/
protected $container = array();

/**
* 检测是否存在指定选项
*/
public function has($data)
{
return array_key_exists($data, $this->container);
}

/**
* 添加选项
*/
public function add($data, $value, $color = null)
{
$array = array('value' => $value);

if( !is_null($color) ) {
$array['color'] = $color;
}

$this->container[$data] = $array;
}

/**
* 移除指定选项
*/
public function remove($data)
{
if( $this->has($data) ) {
unset($this->container[$data]);
}
}

/**
* 转为 Array
*/
public function toArray()
{
return $this->container;
}

/**
* 转为 JSON
*/
public function toJSON()
{
return Serialize::encode($this->toArray(), 'json');
}

/**
* 检测选项上否为空
*/
public function isEmpty()
{
return empty($this->container);
}
}
31 changes: 31 additions & 0 deletions src/Message/Template/TemplateOptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Thenbsp\Wechat\Message\Template;

interface TemplateOptionInterface
{
/**
* 检测是否存在指定选项
*/
public function has($data);

/**
* 添加选项
*/
public function add($data, $value, $color);

/**
* 移除指定选项
*/
public function remove($data);

/**
* 转为 Array
*/
public function toArray();

/**
* 转为 JSON
*/
public function toJSON();
}

0 comments on commit 1f957b1

Please sign in to comment.