|  |  | <?php
 | 
						
						
						
							|  |  | namespace LaneWeChat\Core;
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  | /**
 | 
						
						
						
							|  |  |  * 多客服功能类,用于微信公众号的多客服系统管理。
 | 
						
						
						
							|  |  |  * 该类提供接口以添加、编辑、删除客服账号,获取客服列表,设置客服头像,以及获取客服聊天记录。
 | 
						
						
						
							|  |  |  * 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)
 | 
						
						
						
							|  |  |     {
 | 
						
						
						
							|  |  |         // 构建请求URL,包含access_token
 | 
						
						
						
							|  |  |         $queryUrl = 'https://api.weixin.qq.com/customservice/kfaccount/add?access_token=' . AccessToken::getAccessToken();
 | 
						
						
						
							|  |  |         $queryAction = 'POST';
 | 
						
						
						
							|  |  |         // 创建模板数组
 | 
						
						
						
							|  |  |         $template = array();
 | 
						
						
						
							|  |  |         $template['kf_account'] = $kfAccount;
 | 
						
						
						
							|  |  |         $template['nickname'] = $nickname;
 | 
						
						
						
							|  |  |         $template['password'] = $password;
 | 
						
						
						
							|  |  |         // 将模板数组转换为JSON格式
 | 
						
						
						
							|  |  |         $template = json_encode($template);
 | 
						
						
						
							|  |  |         // 使用Curl类发送POST请求
 | 
						
						
						
							|  |  |         return Curl::callWebServer($queryUrl, $template, $queryAction);
 | 
						
						
						
							|  |  |     }
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  |     /**
 | 
						
						
						
							|  |  |      * 修改客服账号信息。
 | 
						
						
						
							|  |  |      * 此方法用于修改已存在的客服账号信息。
 | 
						
						
						
							|  |  |      *
 | 
						
						
						
							|  |  |      * @param string $kfAccount 完整客服账号,格式为:账号前缀@公众号微信号。
 | 
						
						
						
							|  |  |      * @param string $nickname 客服昵称。
 | 
						
						
						
							|  |  |      * @param string $password 客服密码。
 | 
						
						
						
							|  |  |      * @return array 操作结果,成功返回操作结果数组,失败返回false。
 | 
						
						
						
							|  |  |      */
 | 
						
						
						
							|  |  |     public function editAccount($kfAccount, $nickname, $password)
 | 
						
						
						
							|  |  |     {
 | 
						
						
						
							|  |  |         // 构建请求URL,包含access_token
 | 
						
						
						
							|  |  |         $queryUrl = 'https://api.weixin.qq.com/customservice/kfaccount/update?access_token=' . AccessToken::getAccessToken();
 | 
						
						
						
							|  |  |         $queryAction = 'POST';
 | 
						
						
						
							|  |  |         // 创建模板数组
 | 
						
						
						
							|  |  |         $template = array();
 | 
						
						
						
							|  |  |         $template['kf_account'] = $kfAccount;
 | 
						
						
						
							|  |  |         $template['nickname'] = $nickname;
 | 
						
						
						
							|  |  |         $template['password'] = $password;
 | 
						
						
						
							|  |  |         // 将模板数组转换为JSON格式
 | 
						
						
						
							|  |  |         $template = json_encode($template);
 | 
						
						
						
							|  |  |         // 使用Curl类发送POST请求
 | 
						
						
						
							|  |  |         return Curl::callWebServer($queryUrl, $template, $queryAction);
 | 
						
						
						
							|  |  |     }
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  |     /**
 | 
						
						
						
							|  |  |      * 删除客服账号。
 | 
						
						
						
							|  |  |      * 此方法用于从微信公众号后台删除一个客服账号。
 | 
						
						
						
							|  |  |      *
 | 
						
						
						
							|  |  |      * @param string $kfAccount 完整客服账号,格式为:账号前缀@公众号微信号。
 | 
						
						
						
							|  |  |      * @return array 操作结果,成功返回操作结果数组,失败返回false。
 | 
						
						
						
							|  |  |      */
 | 
						
						
						
							|  |  |     public function delAccount($kfAccount)
 | 
						
						
						
							|  |  |     {
 | 
						
						
						
							|  |  |         // 构建请求URL,包含access_token
 | 
						
						
						
							|  |  |         $queryUrl = 'https://api.weixin.qq.com/customservice/kfaccount/del?access_token=' . AccessToken::getAccessToken();
 | 
						
						
						
							|  |  |         $queryAction = 'POST';
 | 
						
						
						
							|  |  |         // 创建模板数组
 | 
						
						
						
							|  |  |         $template = array();
 | 
						
						
						
							|  |  |         $template['kf_account'] = $kfAccount;
 | 
						
						
						
							|  |  |         // 将模板数组转换为JSON格式
 | 
						
						
						
							|  |  |         $template = json_encode($template);
 | 
						
						
						
							|  |  |         // 使用Curl类发送POST请求
 | 
						
						
						
							|  |  |         return Curl::callWebServer($queryUrl, $template, $queryAction);
 | 
						
						
						
							|  |  |     }
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  |     /**
 | 
						
						
						
							|  |  |      * 获取所有客服账号列表。
 | 
						
						
						
							|  |  |      * 此方法用于获取公众号下所有客服账号的信息。
 | 
						
						
						
							|  |  |      *
 | 
						
						
						
							|  |  |      * @return array 客服账号列表,每个客服账号包含kf_account, kf_nick, kf_id, kf_headimgurl等信息。
 | 
						
						
						
							|  |  |      */
 | 
						
						
						
							|  |  |     public function getAccountList()
 | 
						
						
						
							|  |  |     {
 | 
						
						
						
							|  |  |         // 构建请求URL,包含access_token
 | 
						
						
						
							|  |  |         $queryUrl = 'https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=' . AccessToken::getAccessToken();
 | 
						
						
						
							|  |  |         $queryAction = 'GET';
 | 
						
						
						
							|  |  |         // 使用Curl类发送GET请求
 | 
						
						
						
							|  |  |         return Curl::callWebServer($queryUrl, '', $queryAction);
 | 
						
						
						
							|  |  |     }
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  |     /**
 | 
						
						
						
							|  |  |      * 设置客服账号的头像。
 | 
						
						
						
							|  |  |      * 此方法用于上传并设置客服账号的头像。
 | 
						
						
						
							|  |  |      * 头像图片文件必须是jpg格式,推荐使用640*640大小的图片以达到最佳效果。
 | 
						
						
						
							|  |  |      *
 | 
						
						
						
							|  |  |      * @param string $kfAccount 完整客服账号,格式为:账号前缀@公众号微信号。
 | 
						
						
						
							|  |  |      * @param string $imagePath 待上传的头像文件路径。
 | 
						
						
						
							|  |  |      * @return array 操作结果,成功返回操作结果数组,失败返回false。
 | 
						
						
						
							|  |  |      */
 | 
						
						
						
							|  |  |     public function setAccountImage($kfAccount, $imagePath)
 | 
						
						
						
							|  |  |     {
 | 
						
						
						
							|  |  |         // 检查头像文件是否存在
 | 
						
						
						
							|  |  |         if (!file_exists($imagePath)) {
 | 
						
						
						
							|  |  |             return false;
 | 
						
						
						
							|  |  |         }
 | 
						
						
						
							|  |  |         // 构建请求URL,包含access_token和kf_account
 | 
						
						
						
							|  |  |         $queryUrl = 'http://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?access_token=' . AccessToken::getAccessToken() . '&kf_account=' . $kfAccount;
 | 
						
						
						
							|  |  |         // 创建数据数组
 | 
						
						
						
							|  |  |         $data = array();
 | 
						
						
						
							|  |  |         $data['media'] = '@' . $imagePath;
 | 
						
						
						
							|  |  |         // 使用Curl类发送POST请求,设置multipart为1表示发送文件
 | 
						
						
						
							|  |  |         return Curl::callWebServer($queryUrl, $data, 'POST', 1, 0);
 | 
						
						
						
							|  |  |     }
 | 
						
						
						
							|  |  | 
 | 
						
						
						
							|  |  |     /**
 | 
						
						
						
							|  |  |      * 获取客服聊天记录。
 | 
						
						
						
							|  |  |      * 此方法用于获取多客服的会话记录,包括客服和用户会话的所有消息记录和会话的创建、关闭等操作记录。
 | 
						
						
						
							|  |  |      * 利用此接口可以开发如“消息记录”、“工作监控”、“客服绩效考核”等功能。
 | 
						
						
						
							|  |  |      *
 | 
						
						
						
							|  |  |      * @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 = '')
 | 
						
						
						
							|  |  |     {
 | 
						
						
						
							|  |  |         // 构建请求URL,包含access_token
 | 
						
						
						
							|  |  |         $queryUrl = 'https://api.weixin.qq.com/cgi-bin/customservice/getrecord?access_token=' . AccessToken::getAccessToken();
 | 
						
						
						
							|  |  |         $queryAction = 'POST';
 | 
						
						
						
							|  |  |         // 创建模板数组
 | 
						
						
						
							|  |  |         $template = array();
 | 
						
						
						
							|  |  |         $template['starttime'] = $startTime;
 | 
						
						
						
							|  |  |         $template['endtime'] = $endTime;
 | 
						
						
						
							|  |  |         $template['openid'] = $openId;
 | 
						
						
						
							|  |  |         $template['pagesize'] = $pageSize;
 | 
						
						
						
							|  |  |         $template['pageindex'] = $pageIndex;
 | 
						
						
						
							|  |  |         // 将模板数组转换为JSON格式
 | 
						
						
						
							|  |  |         $template = json_encode($template);
 | 
						
						
						
							|  |  |         // 使用Curl类发送POST请求
 | 
						
						
						
							|  |  |         $result = Curl::callWebServer($queryUrl, $template, $queryAction);
 | 
						
						
						
							|  |  |         // 返回聊天记录列表
 | 
						
						
						
							|  |  |         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',
 | 
						
						
						
							|  |  |             // 其他可能的响应数据...
 | 
						
						
						
							|  |  |         );
 | 
						
						
						
							|  |  |     }
 | 
						
						
						
							|  |  | } |