123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- namespace App\Services\WeChat;
- class KfAccount extends Base
- {
- /**
- * 客服接口-发消息
- * @param string $to_user
- * @param string $msg_type
- * @param array $data
- * @return mixed
- * @throws WeChatException
- */
- public function send($to_user, $msg_type, $data)
- {
- $post = [
- 'touser' => $to_user,
- 'msgtype' => $msg_type,
- $msg_type => $data,
- ];
- $access_token = $this->accessToken;
- $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$access_token}";
- $response = \Curl::to($url)
- ->withData(json_encode($post, JSON_UNESCAPED_UNICODE))
- ->asJsonResponse(true)
- ->post();
- if (isset($response['errcode']) && $response['errcode'] != 0) {
- throw new WeChatException($response['errmsg'], $response['errcode']);
- }
- return $response;
- }
- /**
- * 获取所有客服账号
- * @return mixed
- * @throws WeChatException
- */
- public function getkflist()
- {
- $access_token = $this->accessToken;
- $url = "https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token={$access_token}";
- $response = \Curl::to($url)->asJson(true)->get();
- if (isset($response['errcode']) && $response['errcode'] != 0) {
- throw new WeChatException($response['errmsg'], $response['errcode']);
- }
- return $response;
- }
- /**
- * 添加客服帐号
- * @param string $kf_account
- * @param string $nickname
- * @param string $password
- * @return mixed
- * @throws WeChatException
- */
- public function add($kf_account, $nickname, $password)
- {
- $post = [
- 'kf_account' => $kf_account,
- 'nickname' => $nickname,
- 'password' => $password,
- ];
- $access_token = $this->accessToken;
- $url = "https://api.weixin.qq.com/customservice/kfaccount/add?access_token={$access_token}";
- $response = \Curl::to($url)->withData($post)->asJson(true)->post();
- if (isset($response['errcode']) && $response['errcode'] != 0) {
- throw new WeChatException($response['errmsg'], $response['errcode']);
- }
- return $response;
- }
- /**
- * 修改客服帐号
- * @param string $kf_account
- * @param string $nickname
- * @param string $password
- * @return mixed
- * @throws WeChatException
- */
- public function update($kf_account, $nickname, $password)
- {
- $post = [
- 'kf_account' => $kf_account,
- 'nickname' => $nickname,
- 'password' => $password,
- ];
- $access_token = $this->accessToken;
- $url = "https://api.weixin.qq.com/customservice/kfaccount/update?access_token={$access_token}";
- $response = \Curl::to($url)->withData($post)->asJson(true)->post();
- if (isset($response['errcode']) && $response['errcode'] != 0) {
- throw new WeChatException($response['errmsg'], $response['errcode']);
- }
- return $response;
- }
- /**
- * 删除客服帐号
- * @param string $kf_account
- * @param string $nickname
- * @param string $password
- * @return mixed
- * @throws WeChatException
- */
- public function delete($kf_account, $nickname, $password)
- {
- $post = [
- 'kf_account' => $kf_account,
- 'nickname' => $nickname,
- 'password' => $password,
- ];
- $access_token = $this->accessToken;
- $url = "https://api.weixin.qq.com/customservice/kfaccount/del?access_token={$access_token}";
- $response = \Curl::to($url)->withData($post)->asJson(true)->post();
- if (isset($response['errcode']) && $response['errcode'] != 0) {
- throw new WeChatException($response['errmsg'], $response['errcode']);
- }
- return $response;
- }
- /**
- * 修改客服帐号头像
- * @param string $kf_account
- * @param string $image_url
- * @return mixed
- * @throws WeChatException
- */
- public function uploadHeadImg($kf_account, $image_url)
- {
- $filename = storage_path() . '/app/' . uniqid() . '.png';
- \File::put($filename, \Curl::to($image_url)->get());
- $access_token = $this->accessToken;
- $http = "http://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?kf_account={$kf_account}&access_token={$access_token}";
- $data = [
- 'media' => "@{$filename}",
- ];
- $response = \Curl::to($http)->withData($data)
- ->withFile('chat_' . uniqid(), $filename)
- ->asJson()
- ->post();
- \File::delete($filename);
- if (isset($response['errcode']) && $response['errcode'] != 0) {
- throw new WeChatException($response['errmsg'], $response['errcode']);
- }
- return $response;
- }
- }
|