GET
GET = 'get'
Class Staff.
$http : \EasyWeChat\Core\Http
Http instance.
$accessToken : \EasyWeChat\Core\AccessToken
The request token.
__construct(\EasyWeChat\Core\AccessToken $accessToken)
Constructor.
\EasyWeChat\Core\AccessToken | $accessToken |
getHttp() : \EasyWeChat\Core\Http
Return the http instance.
setHttp(\EasyWeChat\Core\Http $http) : $this
Set the http instance.
\EasyWeChat\Core\Http | $http |
getAccessToken() : \EasyWeChat\Core\AccessToken
Return the current accessToken.
setAccessToken(\EasyWeChat\Core\AccessToken $accessToken) : $this
Set the request token.
\EasyWeChat\Core\AccessToken | $accessToken |
parseJSON(string $method, array $args) : \EasyWeChat\Support\Collection
Parse JSON from response and check error.
string | $method | |
array | $args |
| null
lists() : \EasyWeChat\Support\Collection
List all staffs.
onlines() : \EasyWeChat\Support\Collection
List all online staffs.
create(string $account, string $nickname) : \EasyWeChat\Support\Collection
Create a staff.
string | $account | |
string | $nickname |
update(string $account, string $nickname) : \EasyWeChat\Support\Collection
Update a staff.
string | $account | |
string | $nickname |
delete(string $account) : \EasyWeChat\Support\Collection
Delete a staff.
string | $account |
invite(string $account, string $wechatId) : \EasyWeChat\Support\Collection
Invite a staff.
string | $account | |
string | $wechatId |
avatar(string $account, string $path) : \EasyWeChat\Support\Collection
Set staff avatar.
string | $account | |
string | $path |
message(\EasyWeChat\Message\AbstractMessage|string $message) : \EasyWeChat\Staff\MessageBuilder
Get message builder.
\EasyWeChat\Message\AbstractMessage|string | $message |
send(string|array $message) : \EasyWeChat\Support\Collection
Send a message.
string|array | $message |
records(integer $startTime, integer $endTime, integer $page = 1, integer $pageSize = 10) : \EasyWeChat\Support\Collection
Get staff session history.
integer | $startTime | |
integer | $endTime | |
integer | $page | |
integer | $pageSize |
messages(integer $startTime, integer $endTime, integer $msgId = 1, integer $number = 10000) : \EasyWeChat\Support\Collection
获取聊天记录.
integer | $startTime | |
integer | $endTime | |
integer | $msgId | |
integer | $number |
<?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.
*/
/**
* Staff.php.
*
* @author overtrue <i@overtrue.me>
* @copyright 2015 overtrue <i@overtrue.me>
*
* @see https://github.com/overtrue
* @see http://overtrue.me
*/
namespace EasyWeChat\Staff;
use EasyWeChat\Core\AbstractAPI;
use EasyWeChat\Support\Collection;
/**
* Class Staff.
*/
class Staff extends AbstractAPI
{
const API_LISTS = 'https://api.weixin.qq.com/cgi-bin/customservice/getkflist';
const API_ONLINE = 'https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist';
const API_DELETE = 'https://api.weixin.qq.com/customservice/kfaccount/del';
const API_UPDATE = 'https://api.weixin.qq.com/customservice/kfaccount/update';
const API_CREATE = 'https://api.weixin.qq.com/customservice/kfaccount/add';
const API_INVITE_BIND = 'https://api.weixin.qq.com/customservice/kfaccount/inviteworker';
const API_MESSAGE_SEND = 'https://api.weixin.qq.com/cgi-bin/message/custom/send';
const API_AVATAR_UPLOAD = 'https://api.weixin.qq.com/customservice/kfaccount/uploadheadimg';
const API_RECORDS = 'https://api.weixin.qq.com/customservice/msgrecord/getrecord';
const API_MSG_LIST = 'https://api.weixin.qq.com/customservice/msgrecord/getmsglist';
/**
* List all staffs.
*
* @return \EasyWeChat\Support\Collection
*/
public function lists()
{
return $this->parseJSON('get', [self::API_LISTS]);
}
/**
* List all online staffs.
*
* @return \EasyWeChat\Support\Collection
*/
public function onlines()
{
return $this->parseJSON('get', [self::API_ONLINE]);
}
/**
* Create a staff.
*
* @param string $account
* @param string $nickname
*
* @return \EasyWeChat\Support\Collection
*/
public function create($account, $nickname)
{
$params = [
'kf_account' => $account,
'nickname' => $nickname,
];
return $this->parseJSON('json', [self::API_CREATE, $params]);
}
/**
* Update a staff.
*
* @param string $account
* @param string $nickname
*
* @return \EasyWeChat\Support\Collection
*/
public function update($account, $nickname)
{
$params = [
'kf_account' => $account,
'nickname' => $nickname,
];
return $this->parseJSON('json', [self::API_UPDATE, $params]);
}
/**
* Delete a staff.
*
* @param string $account
*
* @return \EasyWeChat\Support\Collection
*/
public function delete($account)
{
// XXX: 微信那帮搞技术的都 TM 是 SB,url上的文本居然不 TM urlencode,
// 这里客服账号因为有 @ 符,而微信不接收urlencode的账号。。
// 简直是日了...
// #222
// PS: 如果你是微信做接口的,奉劝你们,尊重技术,不会别乱搞,笨不是你们的错,你们出来坑人就是大错特错。
$accessTokenField = sprintf('%s=%s', $this->accessToken->getQueryName(), $this->accessToken->getToken());
$url = sprintf(self::API_DELETE.'?%s&kf_account=%s', $accessTokenField, $account);
$contents = $this->getHttp()->parseJSON(file_get_contents($url));
$this->checkAndThrow($contents);
return new Collection($contents);
}
/**
* Invite a staff.
*
* @param string $account
* @param string $wechatId
*
* @return \EasyWeChat\Support\Collection
*/
public function invite($account, $wechatId)
{
$params = [
'kf_account' => $account,
'invite_wx' => $wechatId,
];
return $this->parseJSON('json', [self::API_INVITE_BIND, $params]);
}
/**
* Set staff avatar.
*
* @param string $account
* @param string $path
*
* @return \EasyWeChat\Support\Collection
*/
public function avatar($account, $path)
{
return $this->parseJSON('upload', [self::API_AVATAR_UPLOAD, ['media' => $path], [], ['kf_account' => $account]]);
}
/**
* Get message builder.
*
* @param \EasyWeChat\Message\AbstractMessage|string $message
*
* @return \EasyWeChat\Staff\MessageBuilder
*
* @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
*/
public function message($message)
{
$messageBuilder = new MessageBuilder($this);
return $messageBuilder->message($message);
}
/**
* Send a message.
*
* @param string|array $message
*
* @return \EasyWeChat\Support\Collection
*/
public function send($message)
{
return $this->parseJSON('json', [self::API_MESSAGE_SEND, $message]);
}
/**
* Get staff session history.
*
* @param int $startTime
* @param int $endTime
* @param int $page
* @param int $pageSize
*
* @return \EasyWeChat\Support\Collection
*/
public function records($startTime, $endTime, $page = 1, $pageSize = 10)
{
$params = [
'starttime' => is_numeric($startTime) ? $startTime : strtotime($startTime),
'endtime' => is_numeric($endTime) ? $endTime : strtotime($endTime),
'pageindex' => $page,
'pagesize' => $pageSize,
];
return $this->parseJSON('json', [self::API_RECORDS, $params]);
}
/**
* 获取聊天记录.
*
* @param int $startTime
* @param int $endTime
* @param int $msgId
* @param int $number
*
* @return \EasyWeChat\Support\Collection
*/
public function messages($startTime, $endTime, $msgId = 1, $number = 10000)
{
$params = [
'starttime' => is_numeric($startTime) ? $startTime : strtotime($startTime),
'endtime' => is_numeric($endTime) ? $endTime : strtotime($endTime),
'msgid' => $msgId,
'number' => $number,
];
return $this->parseJSON('json', [self::API_MSG_LIST, $params]);
}
}