Skip to content

Commit

Permalink
新增图文消息留言管理接口 (#716)
Browse files Browse the repository at this point in the history
* Can manage mp comment.

* protected method.

* rename method.

* comment module.
  • Loading branch information
mingyoung authored and overtrue committed May 27, 2017
1 parent 5b2b1b7 commit aff1846
Show file tree
Hide file tree
Showing 4 changed files with 367 additions and 0 deletions.
203 changes: 203 additions & 0 deletions src/Comment/Comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
<?php

/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

/**
* Comment.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 <[email protected]>
* @copyright 2017
*
* @see https://github.com/overtrue
* @see http://overtrue.me
*/

namespace EasyWeChat\Comment;

use EasyWeChat\Core\AbstractAPI;

class Comment extends AbstractAPI
{
const API_OPEN_COMMENT = 'https://api.weixin.qq.com/cgi-bin/comment/open';
const API_CLOSE_COMMENT = 'https://api.weixin.qq.com/cgi-bin/comment/close';
const API_LIST_COMMENT = 'https://api.weixin.qq.com/cgi-bin/comment/list';
const API_MARK_ELECT = 'https://api.weixin.qq.com/cgi-bin/comment/markelect';
const API_UNMARK_ELECT = 'https://api.weixin.qq.com/cgi-bin/comment/unmarkelect';
const API_DELETE_COMMENT = 'https://api.weixin.qq.com/cgi-bin/comment/delete';
const API_REPLY_COMMENT = 'https://api.weixin.qq.com/cgi-bin/comment/reply/add';
const API_DELETE_REPLY = 'https://api.weixin.qq.com/cgi-bin/comment/reply/delete';

/**
* Open article comment.
*
* @param int $dataId
* @param int $index
*
* @return \EasyWeChat\Support\Collection
*/
public function open($dataId, $index = null)
{
$params = [
'msg_data_id' => $dataId,
'index' => $index,
];

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

/**
* Close comment.
*
* @param int $dataId
* @param int $index
*
* @return \EasyWeChat\Support\Collection
*/
public function close($dataId, $index = null)
{
$params = [
'msg_data_id' => $dataId,
'index' => $index,
];

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

/**
* Get article comments.
*
* @param int $dataId
* @param int $index
* @param int $begin
* @param int $count
* @param int $type
*
* @return \EasyWeChat\Support\Collection
*/
public function lists($dataId, $index, $begin, $count, $type = 0)
{
$params = [
'msg_data_id' => $dataId,
'index' => $index,
'begin' => $begin,
'count' => $count,
'type' => $type,
];

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

/**
* Mark elect comment.
*
* @param int $dataId
* @param int $index
* @param int $commentId
*
* @return \EasyWeChat\Support\Collection
*/
public function markElect($dataId, $index, $commentId)
{
$params = [
'msg_data_id' => $dataId,
'index' => $index,
'user_comment_id' => $commentId,
];

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

/**
* Unmark elect comment.
*
* @param int $dataId
* @param int $index
* @param int $commentId
*
* @return \EasyWeChat\Support\Collection
*/
public function unmarkElect($dataId, $index, $commentId)
{
$params = [
'msg_data_id' => $dataId,
'index' => $index,
'user_comment_id' => $commentId,
];

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

/**
* Delete comment.
*
* @param int $dataId
* @param int $index
* @param int $commentId
*
* @return \EasyWeChat\Support\Collection
*/
public function delete($dataId, $index, $commentId)
{
$params = [
'msg_data_id' => $dataId,
'index' => $index,
'user_comment_id' => $commentId,
];

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

/**
* Reply to a comment.
*
* @param int $dataId
* @param int $index
* @param int $commentId
* @param string $content
*
* @return \EasyWeChat\Support\Collection
*/
public function reply($dataId, $index, $commentId, $content)
{
$params = [
'msg_data_id' => $dataId,
'index' => $index,
'user_comment_id' => $commentId,
'content' => $content,
];

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

/**
* Delete a reply.
*
* @param int $dataId
* @param int $index
* @param int $commentId
*
* @return \EasyWeChat\Support\Collection
*/
public function deleteReply($dataId, $index, $commentId)
{
$params = [
'msg_data_id' => $dataId,
'index' => $index,
'user_comment_id' => $commentId,
];

return $this->parseJSON('post', [self::API_DELETE_REPLY, $params]);
}
}
2 changes: 2 additions & 0 deletions src/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
* @property \EasyWeChat\Broadcast\Broadcast $broadcast
* @property \EasyWeChat\Card\Card $card
* @property \EasyWeChat\Device\Device $device
* @property \EasyWeChat\Comment\Comment $comment
* @property \EasyWeChat\ShakeAround\ShakeAround $shakearound
* @property \EasyWeChat\OpenPlatform\OpenPlatform $open_platform
* @property \EasyWeChat\MiniProgram\MiniProgram $mini_program
Expand Down Expand Up @@ -103,6 +104,7 @@ class Application extends Container
ServiceProviders\ShakeAroundServiceProvider::class,
ServiceProviders\OpenPlatformServiceProvider::class,
ServiceProviders\MiniProgramServiceProvider::class,
ServiceProviders\CommentServiceProvider::class,
];

/**
Expand Down
40 changes: 40 additions & 0 deletions src/Foundation/ServiceProviders/CommentServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

/**
* CommentServiceProvider.php.
*
* This file is part of the wechat.
*
* (c) overtrue <[email protected]>
*
* 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\Comment\Comment;
use Pimple\Container;
use Pimple\ServiceProviderInterface;

class CommentServiceProvider implements ServiceProviderInterface
{
/**
* {@inheritdoc}.
*/
public function register(Container $pimple)
{
$pimple['comment'] = function ($pimple) {
return new Comment($pimple['access_token']);
};
}
}
122 changes: 122 additions & 0 deletions tests/Comment/CommentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace EasyWeChat\Tests\Comment;

use EasyWeChat\Tests\TestCase;

class CommentTest extends TestCase
{
public function testOpenComment()
{
$result = $this->getComment()->open('xxx123', 0);

$this->assertEquals('https://api.weixin.qq.com/cgi-bin/comment/open', $result['api']);
$this->assertSame(['msg_data_id' => 'xxx123', 'index' => 0], $result['params']);
}

public function testCloseComment()
{
$result = $this->getComment()->close('xxx123', 0);

$this->assertEquals('https://api.weixin.qq.com/cgi-bin/comment/close', $result['api']);
$this->assertSame(['msg_data_id' => 'xxx123', 'index' => 0], $result['params']);
}

public function testListComment()
{
$result = $this->getComment()->lists('xxx123', 0, 10, 20, 0);

$this->assertEquals('https://api.weixin.qq.com/cgi-bin/comment/list', $result['api']);
$this->assertSame([
'msg_data_id' => 'xxx123',
'index' => 0,
'begin' => 10,
'count' => 20,
'type' => 0,
], $result['params']);
}

public function testMarkElect()
{
$result = $this->getComment()->markElect('xxx123', 0, 'comment-id');

$this->assertEquals('https://api.weixin.qq.com/cgi-bin/comment/markelect', $result['api']);
$this->assertSame([
'msg_data_id' => 'xxx123',
'index' => 0,
'user_comment_id' => 'comment-id',
], $result['params']);
}

public function testUnmarkElect()
{
$result = $this->getComment()->unmarkElect('xxx123', 0, 'comment-id');

$this->assertEquals('https://api.weixin.qq.com/cgi-bin/comment/unmarkelect', $result['api']);
$this->assertSame([
'msg_data_id' => 'xxx123',
'index' => 0,
'user_comment_id' => 'comment-id',
], $result['params']);
}

public function testDeleteComment()
{
$result = $this->getComment()->delete('xxx123', 0, 'comment-id');

$this->assertEquals('https://api.weixin.qq.com/cgi-bin/comment/delete', $result['api']);
$this->assertSame([
'msg_data_id' => 'xxx123',
'index' => 0,
'user_comment_id' => 'comment-id',
], $result['params']);
}

public function testAddReply()
{
$result = $this->getComment()->reply('xxx123', 0, 'comment-id', 'content...');

$this->assertEquals('https://api.weixin.qq.com/cgi-bin/comment/reply/add', $result['api']);
$this->assertSame([
'msg_data_id' => 'xxx123',
'index' => 0,
'user_comment_id' => 'comment-id',
'content' => 'content...',
], $result['params']);
}

public function testDeleteReply()
{
$result = $this->getComment()->deleteReply('xxx123', 0, 'comment-id');

$this->assertEquals('https://api.weixin.qq.com/cgi-bin/comment/reply/delete', $result['api']);
$this->assertSame([
'msg_data_id' => 'xxx123',
'index' => 0,
'user_comment_id' => 'comment-id',
], $result['params']);
}

private function getComment()
{
$press = \Mockery::mock('EasyWeChat\Comment\Comment[parseJSON]', [\Mockery::mock('EasyWeChat\Core\AccessToken')]);
$press->shouldReceive('parseJSON')->andReturnUsing(function ($method, $params) {
return [
'api' => $params[0],
'params' => empty($params[1]) ? null : $params[1],
'quires' => empty($params[3]) ? null : $params[3],
];
});

return $press;
}
}

0 comments on commit aff1846

Please sign in to comment.