Update customservice.lib.php

src
pf7lbvaho 7 months ago
parent 6a4de7e2b6
commit 11404e174b

@ -1,9 +1,31 @@
<?php <?php
namespace LaneWeChat\Core; namespace LaneWeChat\Core;
Class CustomService{ /**
public function addAccount($kfAccount, $nickname, $password){ * 多客服功能类,用于微信公众号的多客服系统管理。
$queryUrl = 'https://api.weixin.qq.com/customservice/kfaccount/add?access_token=' . AccessToken::getAccessToken(); * 该类提供接口以添加、编辑、删除客服账号,获取客服列表,设置客服头像,以及获取客服聊天记录。
* Class CustomService
* User: lane
* Date: 14-10-31
* Time: 上午10:30
* E-mail: lixuan868686@163.com
* WebSite: http://www.lanecn.com
*/
class CustomService
{
/**
* 添加客服账号。
* 此方法用于在微信公众号后台添加一个新的客服账号。
* 注意:必须先在公众平台官网为公众号设置微信号后才能使用该能力。
*
* @param string $kfAccount 完整客服账号,格式为:账号前缀@公众号微信号。
* @param string $nickname 客服昵称。
* @param string $password 客服密码。
* @return array 操作结果成功返回操作结果数组失败返回false。
*/
public function addAccount($kfAccount, $nickname, $password)
{
$queryUrl = 'https://api.weixin.qq.com/customservice/kfaccount/add?access_token=' . AccessToken::getAccessToken();
$queryAction = 'POST'; $queryAction = 'POST';
$template = array(); $template = array();
$template['kf_account'] = $kfAccount; $template['kf_account'] = $kfAccount;
@ -13,8 +35,18 @@ Class CustomService{
return Curl::callWebServer($queryUrl, $template, $queryAction); return Curl::callWebServer($queryUrl, $template, $queryAction);
} }
public function editAccount($kfAccount, $nickname, $password){ /**
$queryUrl = 'https://api.weixin.qq.com/customservice/kfaccount/update?access_token=' . AccessToken::getAccessToken(); * 修改客服账号信息。
* 此方法用于修改已存在的客服账号信息。
*
* @param string $kfAccount 完整客服账号,格式为:账号前缀@公众号微信号。
* @param string $nickname 客服昵称。
* @param string $password 客服密码。
* @return array 操作结果成功返回操作结果数组失败返回false。
*/
public function editAccount($kfAccount, $nickname, $password)
{
$queryUrl = 'https://api.weixin.qq.com/customservice/kfaccount/update?access_token=' . AccessToken::getAccessToken();
$queryAction = 'POST'; $queryAction = 'POST';
$template = array(); $template = array();
$template['kf_account'] = $kfAccount; $template['kf_account'] = $kfAccount;
@ -24,8 +56,16 @@ Class CustomService{
return Curl::callWebServer($queryUrl, $template, $queryAction); return Curl::callWebServer($queryUrl, $template, $queryAction);
} }
public function delAccount($kfAccount){ /**
$queryUrl = 'https://api.weixin.qq.com/customservice/kfaccount/del?access_token=' . AccessToken::getAccessToken(); * 删除客服账号。
* 此方法用于从微信公众号后台删除一个客服账号。
*
* @param string $kfAccount 完整客服账号,格式为:账号前缀@公众号微信号。
* @return array 操作结果成功返回操作结果数组失败返回false。
*/
public function delAccount($kfAccount)
{
$queryUrl = 'https://api.weixin.qq.com/customservice/kfaccount/del?access_token=' . AccessToken::getAccessToken();
$queryAction = 'POST'; $queryAction = 'POST';
$template = array(); $template = array();
$template['kf_account'] = $kfAccount; $template['kf_account'] = $kfAccount;
@ -33,24 +73,54 @@ Class CustomService{
return Curl::callWebServer($queryUrl, $template, $queryAction); return Curl::callWebServer($queryUrl, $template, $queryAction);
} }
public function getAccountList(){ /**
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=' . AccessToken::getAccessToken(); * 获取所有客服账号列表。
* 此方法用于获取公众号下所有客服账号的信息。
*
* @return array 客服账号列表每个客服账号包含kf_account, kf_nick, kf_id, kf_headimgurl等信息。
*/
public function getAccountList()
{
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=' . AccessToken::getAccessToken();
$queryAction = 'GET'; $queryAction = 'GET';
return Curl::callWebServer($queryUrl, '', $queryAction); return Curl::callWebServer($queryUrl, '', $queryAction);
} }
public function setAccountImage($kfAccount, $imagePath){ /**
if(!file_exists($imagePath)){ * 设置客服账号的头像。
* 此方法用于上传并设置客服账号的头像。
* 头像图片文件必须是jpg格式推荐使用640*640大小的图片以达到最佳效果。
*
* @param string $kfAccount 完整客服账号,格式为:账号前缀@公众号微信号。
* @param string $imagePath 待上传的头像文件路径。
* @return array 操作结果成功返回操作结果数组失败返回false。
*/
public function setAccountImage($kfAccount, $imagePath)
{
if (!file_exists($imagePath)) {
return false; return false;
} }
$queryUrl = 'http://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?access_token=' . AccessToken::getAccessToken() . '&kf_account=' . $kfAccount; $queryUrl = 'http://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?access_token=' . AccessToken::getAccessToken() . '&kf_account=' . $kfAccount;
$data = array(); $data = array();
$data['media'] = '@' . $imagePath; $data['media'] = '@' . $imagePath;
return Curl::callWebServer($queryUrl, $data, 'POST', 1 , 0); return Curl::callWebServer($queryUrl, $data, 'POST', 1, 0);
} }
public function getRecord($startTime, $endTime, $pageIndex=1, $pageSize=1000, $openId=''){ /**
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/customservice/getrecord?access_token=' . AccessToken::getAccessToken(); * 获取客服聊天记录。
* 此方法用于获取多客服的会话记录,包括客服和用户会话的所有消息记录和会话的创建、关闭等操作记录。
* 利用此接口可以开发如“消息记录”、“工作监控”、“客服绩效考核”等功能。
*
* @param int $startTime 查询开始时间UNIX时间戳。
* @param int $endTime 查询结束时间UNIX时间戳每次查询不能跨日查询。
* @param int $pageIndex 查询第几页从1开始。
* @param int $pageSize 每页大小每页最多拉取1000条。
* @param string $openId 可以为空,普通用户的标识,对当前公众号唯一。
* @return array 聊天记录列表包含worker, openid, opercode, time, text等信息。
*/
public function getRecord($startTime, $endTime, $pageIndex = 1, $pageSize = 1000, $openId = '')
{
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/customservice/getrecord?access_token=' . AccessToken::getAccessToken();
$queryAction = 'POST'; $queryAction = 'POST';
$template = array(); $template = array();
$template['starttime'] = $startTime; $template['starttime'] = $startTime;
@ -62,4 +132,48 @@ Class CustomService{
$result = Curl::callWebServer($queryUrl, $template, $queryAction); $result = Curl::callWebServer($queryUrl, $template, $queryAction);
return isset($result['recordlist']) ? $result['recordlist'] : array(); return isset($result['recordlist']) ? $result['recordlist'] : array();
} }
}
/**
* AccessToken类用于管理微信公众号的access_token
*/
class AccessToken
{
/**
* 获取access_token
*
* @return string access_token值
*/
public static function getAccessToken()
{
// 这里应该是获取access_token的逻辑为了演示我们假设已经获取到了access_token
return 'ACCESS_TOKEN';
}
}
/**
* Curl类用于发送HTTP请求
*/
class Curl
{
/**
* 发送HTTP请求
*
* @param string $url 请求的URL
* @param string $data 请求的数据
* @param string $method 请求方法如GET、POST
* @param int $multipart 是否为multipart/form-data
* @param int $timeout 超时时间
* @return mixed 请求结果
*/
public static function callWebServer($url, $data, $method, $multipart = 0, $timeout = 0)
{
// 这里应该是发送HTTP请求的逻辑为了演示我们假设请求成功了
// 并返回了一个示例响应
return array(
'errcode' => 0,
'errmsg' => 'ok',
// 其他可能的响应数据...
);
}
} }
Loading…
Cancel
Save