Skip to content

Commit

Permalink
优化接口调用,标准化请求4e0e9d7
Browse files Browse the repository at this point in the history
  • Loading branch information
axguowen committed May 6, 2024
1 parent 11045fb commit 9ed3a04
Show file tree
Hide file tree
Showing 15 changed files with 319 additions and 483 deletions.
298 changes: 128 additions & 170 deletions src/wechat/services/official/Card.php

Large diffs are not rendered by default.

74 changes: 27 additions & 47 deletions src/wechat/services/official/CustomService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,52 +27,46 @@ class CustomService extends WeChat
*/
public function addAccount($kf_account, $nickname)
{
$data = ['kf_account' => $kf_account, 'nickname' => $nickname];
$url = "https://api.weixin.qq.com/customservice/kfaccount/add?access_token=ACCESS_TOKEN";
$this->registerApi($url, __FUNCTION__, func_get_args());
return $this->httpPostForJson($url, $data);
return $this->callPostApi($url, ['kf_account' => $kf_account, 'nickname' => $nickname]);
}

/**
* 修改客服帐号
* @access public
* @param string $kf_account 客服账号
* @param string $kfAccount 客服账号
* @param string $nickname 客服昵称
* @return array
*/
public function updateAccount($kf_account, $nickname)
public function updateAccount($kfAccount, $nickname)
{
$data = ['kf_account' => $kf_account, 'nickname' => $nickname];
$url = "https://api.weixin.qq.com/customservice/kfaccount/update?access_token=ACCESS_TOKEN";
$this->registerApi($url, __FUNCTION__, func_get_args());
return $this->httpPostForJson($url, $data);
return $this->callPostApi($url, ['kf_account' => $kfAccount, 'nickname' => $nickname]);
}

/**
* 删除客服帐号
* @access public
* @param string $kf_account 客服账号
* @param string $kfAccount 客服账号
* @return array
*/
public function deleteAccount($kf_account)
public function deleteAccount($kfAccount)
{
$data = ['kf_account' => $kf_account];
$url = "https://api.weixin.qq.com/customservice/kfaccount/del?access_token=ACCESS_TOKEN";
$this->registerApi($url, __FUNCTION__, func_get_args());
return $this->httpPostForJson($url, $data);
return $this->callPostApi($url, ['kf_account' => $kfAccount]);
}

/**
* 邀请绑定客服帐号
* @access public
* @param string $kf_account 完整客服帐号,格式为:帐号前缀@公众号微信号
* @param string $invite_wx 接收绑定邀请的客服微信号
* @param string $kfAccount 完整客服帐号,格式为:帐号前缀@公众号微信号
* @param string $inviteWx 接收绑定邀请的客服微信号
* @return array
*/
public function inviteWorker($kf_account, $invite_wx)
public function inviteWorker($kfAccount, $inviteWx)
{
$url = 'https://api.weixin.qq.com/customservice/kfaccount/inviteworker?access_token=ACCESS_TOKEN';
return $this->callPostApi($url, ['kf_account' => $kf_account, 'invite_wx' => $invite_wx]);
return $this->callPostApi($url, ['kf_account' => $kfAccount, 'invite_wx' => $inviteWx]);
}

/**
Expand All @@ -83,8 +77,7 @@ public function inviteWorker($kf_account, $invite_wx)
public function getAccountList()
{
$url = "https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=ACCESS_TOKEN";
$this->registerApi($url, __FUNCTION__, func_get_args());
return $this->httpGetForJson($url);
return $this->callGetApi($url);
}

/**
Expand All @@ -95,8 +88,7 @@ public function getAccountList()
public function getOnlineAccountList()
{
$url = "https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist?access_token=ACCESS_TOKEN";
$this->registerApi($url, __FUNCTION__, func_get_args());
return $this->httpGetForJson($url);
return $this->callGetApi($url);
}

/**
Expand All @@ -108,9 +100,8 @@ public function getOnlineAccountList()
*/
public function uploadHeadimg($kf_account, $image)
{
$url = "http://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?access_token=ACCESS_TOKEN&kf_account={$kf_account}";
$this->registerApi($url, __FUNCTION__, func_get_args());
return $this->httpPostForJson($url, ['media' => Tools::createCurlFile($image)]);
$url = "https://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?access_token=ACCESS_TOKEN&kf_account={$kf_account}";
return $this->callPostApi($url, ['media' => Tools::createCurlFile($image)], false);
}

/**
Expand All @@ -122,8 +113,7 @@ public function uploadHeadimg($kf_account, $image)
public function send(array $data)
{
$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN";
$this->registerApi($url, __FUNCTION__, func_get_args());
return $this->httpPostForJson($url, $data);
return $this->callPostApi($url, $data);
}

/**
Expand All @@ -136,8 +126,7 @@ public function send(array $data)
public function typing($openid, $command = 'Typing')
{
$url = "https://api.weixin.qq.com/cgi-bin/message/custom/typing?access_token=ACCESS_TOKEN";
$this->registerApi($url, __FUNCTION__, func_get_args());
return $this->httpPostForJson($url, ['touser' => $openid, 'command' => $command]);
return $this->callPostApi($url, ['touser' => $openid, 'command' => $command]);
}

/**
Expand All @@ -149,8 +138,7 @@ public function typing($openid, $command = 'Typing')
public function massSendAll(array $data)
{
$url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=ACCESS_TOKEN";
$this->registerApi($url, __FUNCTION__, func_get_args());
return $this->httpPostForJson($url, $data);
return $this->callPostApi($url, $data);
}

/**
Expand All @@ -162,8 +150,7 @@ public function massSendAll(array $data)
public function massSend(array $data)
{
$url = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=ACCESS_TOKEN";
$this->registerApi($url, __FUNCTION__, func_get_args());
return $this->httpPostForJson($url, $data);
return $this->callPostApi($url, $data);
}

/**
Expand All @@ -175,11 +162,10 @@ public function massSend(array $data)
*/
public function massDelete($msg_id, $article_idx = null)
{
$url = "https://api.weixin.qq.com/cgi-bin/message/mass/delete?access_token=ACCESS_TOKEN";
$data = ['msg_id' => $msg_id];
is_null($article_idx) || $data['article_idx'] = $article_idx;
$url = "https://api.weixin.qq.com/cgi-bin/message/mass/delete?access_token=ACCESS_TOKEN";
$this->registerApi($url, __FUNCTION__, func_get_args());
return $this->httpPostForJson($url, $data);
return $this->callPostApi($url, $data);
}

/**
Expand All @@ -191,21 +177,19 @@ public function massDelete($msg_id, $article_idx = null)
public function massPreview(array $data)
{
$url = "https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=ACCESS_TOKEN";
$this->registerApi($url, __FUNCTION__, func_get_args());
return $this->httpPostForJson($url, $data);
return $this->callPostApi($url, $data);
}

/**
* 查询群发消息发送状态【订阅号与服务号认证后均可用】
* @access public
* @param integer $msg_id 群发消息后返回的消息id
* @param integer $msgId 群发消息后返回的消息id
* @return array
*/
public function massGet($msg_id)
public function massGet($msgId)
{
$url = "https://api.weixin.qq.com/cgi-bin/message/mass/get?access_token=ACCESS_TOKEN";
$this->registerApi($url, __FUNCTION__, func_get_args());
return $this->httpPostForJson($url, ['msg_id' => $msg_id]);
return $this->callPostApi($url, ['msg_id' => $msgId]);
}

/**
Expand All @@ -216,8 +200,7 @@ public function massGet($msg_id)
public function massGetSeed()
{
$url = "https://api.weixin.qq.com/cgi-bin/message/mass/speed/get?access_token=ACCESS_TOKEN";
$this->registerApi($url, __FUNCTION__, func_get_args());
return $this->httpPostForJson($url, []);
return $this->callPostApi($url, []);
}

/**
Expand All @@ -229,9 +212,6 @@ public function massGetSeed()
public function massSetSeed($speed)
{
$url = "https://api.weixin.qq.com/cgi-bin/message/mass/speed/set?access_token=ACCESS_TOKEN";
$this->registerApi($url, __FUNCTION__, func_get_args());
return $this->httpPostForJson($url, ['speed' => $speed]);
return $this->callPostApi($url, ['speed' => $speed]);
}


}
39 changes: 16 additions & 23 deletions src/wechat/services/official/Draft.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,33 @@ class Draft extends WeChat
public function add($articles)
{
$url = "https://api.weixin.qq.com/cgi-bin/draft/add?access_token=ACCESS_TOKEN";
$this->registerApi($url, __FUNCTION__, func_get_args());
return $this->httpPostForJson($url, ['articles' => $articles]);
return $this->callPostApi($url, ['articles' => $articles]);
}

/**
* 获取草稿
* @access public
* @param string $media_id
* @param string $mediaId
* @param string $outType 返回处理函数
* @return array
*/
public function get($media_id, $outType = null)
public function get($mediaId, $outType = null)
{
$url = "https://api.weixin.qq.com/cgi-bin/draft/get?access_token=ACCESS_TOKEN";
$this->registerApi($url, __FUNCTION__, func_get_args());
return $this->httpPostForJson($url, ['media_id' => $media_id]);
return $this->callPostApi($url, ['media_id' => $mediaId]);
}


/**
* 删除草稿
* @access public
* @param string $media_id
* @param string $mediaId
* @return array
*/
public function delete($media_id)
public function delete($mediaId)
{
$url = "https://api.weixin.qq.com/cgi-bin/draft/delete?access_token=ACCESS_TOKEN";
$this->registerApi($url, __FUNCTION__, func_get_args());
return $this->httpPostForJson($url, ['media_id' => $media_id]);
return $this->callPostApi($url, ['media_id' => $mediaId]);
}

/**
Expand All @@ -68,24 +65,22 @@ public function delete($media_id)
public function addNews($data)
{
$url = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=ACCESS_TOKEN";
$this->registerApi($url, __FUNCTION__, func_get_args());
return $this->httpPostForJson($url, $data);
return $this->callPostApi($url, $data);
}

/**
* 修改草稿
* @access public
* @param string $media_id 要修改的图文消息的id
* @param string $mediaId 要修改的图文消息的id
* @param int $index 要更新的文章在图文消息中的位置(多图文消息时,此字段才有意义),第一篇为0
* @param $articles
* @return array
*/
public function update($media_id, $index, $articles)
public function update($mediaId, $index, $articles)
{
$data = ['media_id' => $media_id, 'index' => $index, 'articles' => $articles];
$url = "https://api.weixin.qq.com/cgi-bin/draft/update?access_token=ACCESS_TOKEN";
$this->registerApi($url, __FUNCTION__, func_get_args());
return $this->httpPostForJson($url, $data);
$data = ['media_id' => $mediaId, 'index' => $index, 'articles' => $articles];
return $this->callPostApi($url, $data);
}

/**
Expand All @@ -96,23 +91,21 @@ public function update($media_id, $index, $articles)
public function getCount()
{
$url = "https://api.weixin.qq.com/cgi-bin/draft/count?access_token=ACCESS_TOKEN";
$this->registerApi($url, __FUNCTION__, func_get_args());
return $this->httpGetForJson($url);
return $this->callGetApi($url);
}

/**
* 获取草稿列表
* @access public
* @param int $offset 从全部素材的该偏移位置开始返回,0表示从第一个素材返回
* @param int $count 返回素材的数量,取值在1到20之间
* @param int $no_content 1 表示不返回 content 字段,0 表示正常返回,默认为 0
* @param int $noContent 1 表示不返回 content 字段,0 表示正常返回,默认为 0
* @return array
*/
public function batchGet($offset = 0, $count = 20, $no_content = 0)
public function batchGet($offset = 0, $count = 20, $noContent = 0)
{
$url = "https://api.weixin.qq.com/cgi-bin/draft/batchget?access_token=ACCESS_TOKEN";
$this->registerApi($url, __FUNCTION__, func_get_args());
return $this->httpPostForJson($url, ['no_content' => $no_content, 'offset' => $offset, 'count' => $count]);
return $this->callPostApi($url, ['no_content' => $noContent, 'offset' => $offset, 'count' => $count]);
}

}
35 changes: 15 additions & 20 deletions src/wechat/services/official/FreePublish.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,69 +22,64 @@ class FreePublish extends WeChat
* 发布接口
* 开发者需要先将图文素材以草稿的形式保存(见“草稿箱/新建草稿”,如需从已保存的草稿中选择,见“草稿箱/获取草稿列表”)
* @access public
* @param mixed $media_id 选择要发布的草稿的media_id
* @param mixed $mediaId 选择要发布的草稿的media_id
* @return array
*/
public function submit($media_id)
public function submit($mediaId)
{
$url = "https://api.weixin.qq.com/cgi-bin/freepublish/submit?access_token=ACCESS_TOKEN";
$this->registerApi($url, __FUNCTION__, func_get_args());
return $this->httpPostForJson($url, ['media_id' => $media_id]);
return $this->callPostApi($url, ['media_id' => $mediaId]);
}

/**
* 发布状态轮询接口
* @access public
* @param mixed $publish_id
* @param mixed $publishId
* @return array
*/
public function get($publish_id)
public function get($publishId)
{
$url = "https://api.weixin.qq.com/cgi-bin/freepublish/get?access_token=ACCESS_TOKEN";
$this->registerApi($url, __FUNCTION__, func_get_args());
return $this->httpPostForJson($url, ['publish_id' => $publish_id]);
return $this->callPostApi($url, ['publish_id' => $publishId]);
}

/**
* 删除发布
* 发布成功之后,随时可以通过该接口删除。此操作不可逆,请谨慎操作。
* @access public
* @param mixed $article_id 成功发布时返回的 article_id
* @param mixed $articleId 成功发布时返回的 article_id
* @param int $index 要删除的文章在图文消息中的位置,第一篇编号为1,该字段不填或填0会删除全部文章
* @return array
*/
public function delete($article_id, $index = 0)
public function delete($articleId, $index = 0)
{
$url = "https://api.weixin.qq.com/cgi-bin/freepublish/delete?access_token=ACCESS_TOKEN";
$this->registerApi($url, __FUNCTION__, func_get_args());
return $this->httpPostForJson($url, ['article_id' => $article_id, 'index' => $index]);
return $this->callPostApi($url, ['article_id' => $articleId, 'index' => $index]);
}

/**
* 通过 article_id 获取已发布文章
* @access public
* @param mixed $article_id 要获取的草稿的article_id
* @param mixed $articleId 要获取的草稿的article_id
* @return array
*/
public function getArticle($article_id)
public function getArticle($articleId)
{
$url = "https://api.weixin.qq.com/cgi-bin/freepublish/getarticle?access_token=ACCESS_TOKEN";
$this->registerApi($url, __FUNCTION__, func_get_args());
return $this->httpPostForJson($url, ['article_id' => $article_id]);
return $this->callPostApi($url, ['article_id' => $articleId]);
}

/**
* 获取成功发布列表
* @access public
* @param int $offset 从全部素材的该偏移位置开始返回,0表示从第一个素材返回
* @param int $count 返回素材的数量,取值在1到20之间
* @param int $no_content 1 表示不返回 content 字段,0 表示正常返回,默认为 0
* @param int $noContent 1 表示不返回 content 字段,0 表示正常返回,默认为 0
* @return array
*/
public function batchGet($offset = 0, $count = 20, $no_content = 0)
public function batchGet($offset = 0, $count = 20, $noContent = 0)
{
$url = "https://api.weixin.qq.com/cgi-bin/freepublish/batchget?access_token=ACCESS_TOKEN";
$this->registerApi($url, __FUNCTION__, func_get_args());
return $this->httpPostForJson($url, ['no_content' => $no_content, 'offset' => $offset, 'count' => $count]);
return $this->callPostApi($url, ['no_content' => $noContent, 'offset' => $offset, 'count' => $count]);
}
}
3 changes: 1 addition & 2 deletions src/wechat/services/official/Limit.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public function ping($action = 'all', $operator = 'DEFAULT')
public function getCallbackIp()
{
$url = 'https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=ACCESS_TOKEN';
$this->registerApi($url, __FUNCTION__, func_get_args());
return $this->httpGetForJson($url);
return $this->callGetApi($url);
}
}
Loading

0 comments on commit 9ed3a04

Please sign in to comment.