<?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 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';
public function lists()
{
return $this->parseJSON('get', [self::API_LISTS]);
}
public function onlines()
{
return $this->parseJSON('get', [self::API_ONLINE]);
}
public function create($account, $nickname)
{
$params = [
'kf_account' => $account,
'nickname' => $nickname,
];
return $this->parseJSON('json', [self::API_CREATE, $params]);
}
public function update($account, $nickname)
{
$params = [
'kf_account' => $account,
'nickname' => $nickname,
];
return $this->parseJSON('json', [self::API_UPDATE, $params]);
}
public function delete($account)
{
$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);
}
public function invite($account, $wechatId)
{
$params = [
'kf_account' => $account,
'invite_wx' => $wechatId,
];
return $this->parseJSON('json', [self::API_INVITE_BIND, $params]);
}
public function avatar($account, $path)
{
return $this->parseJSON('upload', [self::API_AVATAR_UPLOAD, ['media' => $path], [], ['kf_account' => $account]]);
}
public function message($message)
{
$messageBuilder = new MessageBuilder($this);
return $messageBuilder->message($message);
}
public function send($message)
{
return $this->parseJSON('json', [self::API_MESSAGE_SEND, $message]);
}
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]);
}
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]);
}
}