forked from thenbsp/wechat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
333 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |