Skip to content

Commit

Permalink
Merge pull request #2 from overtrue/2.1
Browse files Browse the repository at this point in the history
2.1
  • Loading branch information
tianyong90 committed Dec 30, 2015
2 parents 354d471 + cac451f commit e3e9002
Show file tree
Hide file tree
Showing 24 changed files with 315 additions and 1,134 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

可能是目前最优雅的微信公众平台 SDK 了。[Laravel 5 拓展包: overtrue/laravel-wechat](https://github.com/overtrue/laravel-wechat)

> 3.0 开发中,目前大部分模块已经完成,希望您能帮忙测试为谢![3.0](https://github.com/overtrue/wechat/tree/3.0)
SDK QQ群:`319502940`

微信开发者交流群:`9179779`
微信开发者交流群:`9179779` (这不是微信群,是名称叫“微信开发者交流群” 的QQ群,微信上聊技术?你逗我?)

[![Build Status](https://travis-ci.org/overtrue/wechat.svg?branch=master)](https://travis-ci.org/overtrue/wechat)
[![Latest Stable Version](https://poser.pugx.org/overtrue/wechat/v/stable.svg)](https://packagist.org/packages/overtrue/wechat)
Expand Down Expand Up @@ -74,6 +76,8 @@ echo $server->serve();
```
更多请参考文档。

:mega: 现在我们已经把用 2.0 API 写的一个基本例子开源了:[微易](https://github.com/vieasehub/viease)

## 文档

[Wiki](https://github.com/overtrue/wechat/wiki)
Expand All @@ -87,7 +91,7 @@ echo $server->serve();
- [x] [基本消息类型](https://github.com/overtrue/wechat/wiki/消息的使用)
- [x] [图文消息](https://github.com/overtrue/wechat/wiki/消息的使用)
- [x] [模板消息](https://github.com/overtrue/wechat/wiki/模板消息)
- [ ] <del>群发消息(微信限制太多,基本算不能用)</del>
- [x] [群发消息](https://github.com/overtrue/wechat/wiki/群发消息) `2.1.28+`
- [x] [用户与用户组](https://github.com/overtrue/wechat/wiki/用户与用户组管理)
- [x] [客服与消息发送](https://github.com/overtrue/wechat/wiki/客服管理与发送消息)
- [x] [多客服与消息转发](https://github.com/overtrue/wechat/wiki/多客服与消息转发)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"autoload-dev": {
"psr-4": {
"Overtrue\\Wechat\\Test\\": "test/"
"Overtrue\\Wechat\\Tests\\": "tests/"
}
}
}
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<phpunit bootstrap="./vendor/autoload.php" color="true">
<testsuites>
<testsuite name="testShop">
<directory>test/Shop</directory>
<directory>tests/Shop</directory>
</testsuite>
</testsuites>
</phpunit>
</phpunit>
134 changes: 134 additions & 0 deletions src/Wechat/Broadcast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php
/**
* Broadcast.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 overtrue <i@overtrue.me>
* @copyright 2015 overtrue <i@overtrue.me>
* @link https://github.com/overtrue
* @link http://overtrue.me
*/

namespace Overtrue\Wechat;

use Overtrue\Wechat\Messages\BaseMessage;

class Broadcast
{
const API_SEND_BY_GROUP = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall';
const API_SEND_BY_OPENID = 'https://api.weixin.qq.com/cgi-bin/message/mass/send';
const API_DELETE = 'https://api.weixin.qq.com/cgi-bin/message/mass/delete';
const API_PREVIEW = 'https://api.weixin.qq.com/cgi-bin/message/mass/preview';
const API_GET = 'http://api.weixin.qq.com/cgi-bin/message/mass/get';

const PREVIEW_BY_OPENID = 'touser';
const PREVIEW_BY_WXH = 'towxname';

/**
* Http对象
*
* @var Http
*/
protected $http;

/**
* 消息
*
* @var \Overtrue\Wechat\Messages\BaseMessage;
*/
protected $message;

/**
* constructor
*
* @param string $appId
* @param string $appSecret
*/
public function __construct($appId, $appSecret)
{
$this->http = new Http(new AccessToken($appId, $appSecret));
}

/**
* 准备消息
*
* @param \Overtrue\Wechat\Messages\BaseMessage $message
*
* @return Broadcast
*/
public function send($message)
{
is_string($message) && $message = Message::make('text')->with('content', $message);

if (!$message instanceof BaseMessage) {
throw new \Exception("消息必须继承自 'Overtrue\\Wechat\\BaseMessage'");
}

$this->message = $message;

return $this;
}

/**
* 发送消息
*
* @param string $group 组或oppenid
*
* @return bool
*/
public function to($group = null)
{
if (empty($this->message)) {
throw new Exception('未设置要发送的消息');
}

$this->message->to_group = $group;

$apiSend = is_array($group) ? self::API_SEND_BY_OPENID : self::API_SEND_BY_GROUP;

return $this->http->jsonPost($apiSend, $this->message->buildForBroadcast());
}

/**
* 删除群发
*
* @param string $msgId 发出去的消息ID
*
* @return bool
*/
public function delete($msgId)
{
return $this->http->jsonPost(self::API_DELETE, array('msg_id' => $msgId));
}

/**
* 预览
*
* @param string $openId 接收消息用户对应该公众号的openid
* @param string $type 接收消息用户的类型
*
* @return bool
*/
public function preview($openId, $type = self::PREVIEW_BY_OPENID)
{
$this->message->to = $openId;

return $this->http->jsonPost(self::API_PREVIEW, $this->message->buildForBroadcastPreview($type));
}

/**
* 查询群发消息发送状态
*
* @param string $msgId 全消息ID
*
* @return array
*/
public function status($msgId)
{
return $this->http->jsonPost(self::API_GET, array('msg_id' => $msgId));
}
}
72 changes: 58 additions & 14 deletions src/Wechat/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@
*/
class Menu
{
const API_CREATE = 'https://api.weixin.qq.com/cgi-bin/menu/create';
const API_GET = 'https://api.weixin.qq.com/cgi-bin/menu/get';
const API_DELETE = 'https://api.weixin.qq.com/cgi-bin/menu/delete';
const API_QUERY = 'https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info';
const API_CREATE = 'https://api.weixin.qq.com/cgi-bin/menu/create';
const API_GET = 'https://api.weixin.qq.com/cgi-bin/menu/get';
const API_DELETE = 'https://api.weixin.qq.com/cgi-bin/menu/delete';
const API_QUERY = 'https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info';
const API_CONDITIONAL_CREATE = 'https://api.weixin.qq.com/cgi-bin/menu/addconditional';
const API_CONDITIONAL_DELETE = 'https://api.weixin.qq.com/cgi-bin/menu/delconditional';
const API_CONDITIONAL_TEST = 'https://api.weixin.qq.com/cgi-bin/menu/trymatch';

/**
* Http对象
Expand All @@ -54,14 +57,6 @@ public function __construct($appId, $appSecret)
*/
public function set($menus)
{
if ($menus instanceof Closure) {
$menus = $menus($this);
}

if (!is_array($menus)) {
throw new Exception('子菜单必须是数组或者匿名函数返回数组', 1);
}

$menus = $this->extractMenus($menus);

$this->http->jsonPost(self::API_CREATE, array('button' => $menus));
Expand Down Expand Up @@ -104,15 +99,64 @@ public function current()
return empty($menus) ? array() : $menus;
}

/**
* 添加个性化的菜单
*
* @param mixed $menus
* @param array $condition
*/
public function addConditional($menus, array $condition)
{
$menus = $this->extractMenus($menus);

$this->http->jsonPost(self::API_CONDITIONAL_CREATE, array('button' => $menus, 'matchrule' => $condition));

return true;
}

/**
* 测试菜单
*
* @param string $userId
*
* @return boolean
*/
public function test($userId)
{
return $this->http->post(self::API_CONDITIONAL_TEST, array('user_id' => $userId));
}

/**
* 按菜单ID删除菜单
*
* @param int $menuId
*
* @return boolean
*/
public function deleteById($menuId)
{
$this->http->post(self::API_CONDITIONAL_DELETE, array('menuid' => $menuId));

return true;
}

/**
* 转menu为数组
*
* @param array $menus
* @param mixed $menus
*
* @return array
*/
protected function extractMenus(array $menus)
protected function extractMenus($menus)
{
if ($menus instanceof Closure) {
$menus = $menus($this);
}

if (!is_array($menus)) {
throw new Exception('子菜单必须是数组或者匿名函数返回数组', 1);
}

foreach ($menus as $key => $menu) {
$menus[$key] = $menu->toArray();

Expand Down
3 changes: 2 additions & 1 deletion src/Wechat/Message.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ class Message
const IMAGE = 'image';
const VOICE = 'voice';
const VIDEO = 'video';
const MP_VIDEO = 'mpvideo';
const MUSIC = 'music';
const NEWS = 'news';
const TRANSFER = 'transfer';
const NEWS_ITEM = 'news_item';
const MP_NEWS = 'mp_news';
const CARD = 'wxcard';
const WXCARD = 'wxcard';

/**
* 创建消息实例
Expand Down
54 changes: 50 additions & 4 deletions src/Wechat/Messages/BaseMessage.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,63 @@ public function buildForReply()
}

/**
* 生成群发的数据
* 生成通过群发的数据
*
* @return array
*/
public function buildForBroadcast()
{
if (!method_exists($this, 'toBroadcast')) {
throw new \Exception(__CLASS__.'未实现此方法:toBroadcast()');
if (!method_exists($this, 'toStaff')) {
throw new \Exception(__CLASS__.'未实现此方法:toStaff()');
}

if (is_null($this->to_group)) {
$group = array(
'filter' => array(
'is_to_all' => true,
),
);
} elseif (is_array($this->to_group)) {
$group = array(
'touser' => $this->to_group,
);
} else {
$group = array(
'filter' => array(
'is_to_all' => false,
'group_id' => $this->to_group,
),
);
}

$base = array(
'msgtype' => $this->getDefaultMessageType(),
);

return array_merge($group, $this->toStaff(), $base);
}

/**
* 生成通过群发预览的数据
*
* @param $type
*
* @return array
*
* @throws \Exception
*/
public function buildForBroadcastPreview($type)
{
if (!method_exists($this, 'toStaff')) {
throw new \Exception(__CLASS__.'未实现此方法:toStaff()');
}

//TODO
$base = array(
$type => $this->to,
'msgtype' => $this->getDefaultMessageType(),
);

return array_merge($base, $this->toStaff());
}

/**
Expand Down
Loading

0 comments on commit e3e9002

Please sign in to comment.