From e147a3eb13420d567da187be3714792cee2b50d5 Mon Sep 17 00:00:00 2001 From: XueSi <1592328848@qq.com> Date: Thu, 1 Apr 2021 00:00:22 +0800 Subject: [PATCH] officialAccount: fix Menu Client --- src/OfficialAccount/Menu/Client.php | 208 ++++++++++------------------ 1 file changed, 77 insertions(+), 131 deletions(-) diff --git a/src/OfficialAccount/Menu/Client.php b/src/OfficialAccount/Menu/Client.php index 7ed9f88..59328e7 100644 --- a/src/OfficialAccount/Menu/Client.php +++ b/src/OfficialAccount/Menu/Client.php @@ -20,178 +20,124 @@ * */ class Client extends BaseClient { - const URL = [ - 'create' => '/cgi-bin/menu/create', - 'get_current_selfmenu_info' => '/cgi-bin/get_current_selfmenu_info', - 'delete' => '/cgi-bin/menu/delete', - 'addconditional' => '/cgi-bin/menu/addconditional', - 'delconditional' => '/cgi-bin/menu/delconditional', - 'trymatch' => '/cgi-bin/menu/trymatch', - 'get' => '/cgi-bin/menu/get' - ]; /** - * 创建接口 - * @param array $data 自定义标签需要的数据 - * @return array - * @throws HttpException - */ - function create(array $data) - { - $response = $this->getClient() - ->setMethod('POST') - ->setBody( - $this->jsonDataToStream($data) - ) - ->send($this->buildUrl( - self::URL['create'], - [ - 'access_token' => $this->getAccessToken() - ] - )); - - $this->checkResponse($response, $jsonData); - return $jsonData; - } - - /** - * 查询接口 - * @return array + * 获取自定义菜单配置 + * @return mixed * @throws HttpException */ - public function queryConfig() + public function list() { $response = $this->getClient() ->setMethod('GET') - ->send($this->buildUrl( - self::URL['get_current_selfmenu_info'], - [ - 'access_token' => $this->getAccessToken(), - ] - )); + ->send($this->buildUrl('/cgi-bin/menu/get', [ + 'access_token' => $this->app[ServiceProviders::AccessToken]->getToken() + ]) + ); - $this->checkResponse($response, $jsonData); - return $jsonData; + $this->checkResponse($response, $parseData); + return $parseData; } /** - * 删除接口 - * @return array + * 获取当前菜单配置 + * @return mixed * @throws HttpException */ - public function delete() + public function current() { $response = $this->getClient() ->setMethod('GET') - ->send($this->buildUrl( - self::URL['delete'], - [ - 'access_token' => $this->getAccessToken(), - ] - )); + ->send($this->buildUrl('/cgi-bin/get_current_selfmenu_info', [ + 'access_token' => $this->app[ServiceProviders::AccessToken]->getToken() + ]) + ); - $this->checkResponse($response, $jsonData); - return $jsonData; + $this->checkResponse($response, $parseData); + return $parseData; } /** - * 个性化菜单接口:创建个性化菜单 - * @param array $data - * @return array + * 创建菜单或个性化菜单 + * @param array $buttons + * @param array $matchRule + * @return bool * @throws HttpException */ - function addconditional(array $data) + public function create(array $buttons, array $matchRule = []) { + if (!empty($matchRule)) { + // 创建个性化菜单 + $response = $this->getClient() + ->setMethod('POST') + ->setBody($this->jsonDataToStream([ + 'button' => $buttons, + 'matchrule' => $matchRule, + ])) + ->send($this->buildUrl('/cgi-bin/menu/addconditional', [ + 'access_token' => $this->app[ServiceProviders::AccessToken]->getToken() + ]) + ); + return $this->checkResponse($response, $parseData); + } + + // 创建菜单 $response = $this->getClient() ->setMethod('POST') - ->setBody( - $this->jsonDataToStream($data) - ) - ->send($this->buildUrl( - self::URL['addconditional'], - [ - 'access_token' => $this->getAccessToken() - ] - )); - - $this->checkResponse($response, $jsonData); - return $jsonData; + ->setBody($this->jsonDataToStream(['button' => $buttons])) + ->send($this->buildUrl('/cgi-bin/menu/create', [ + 'access_token' => $this->app[ServiceProviders::AccessToken]->getToken() + ]) + ); + return $this->checkResponse($response, $parseData); } /** - * 个性化菜单接口:删除个性化菜单 - * @param string $menuId 菜单id,可以通过自定义菜单查询接口获取。 - * @return array + * 删除菜单或个性化菜单 + * @param int|null $menuId + * @return bool * @throws HttpException */ - function delconditional(string $menuId) + public function delete(int $menuId = null) { - $response = $this->getClient() - ->setMethod('POST') - ->setBody( - $this->jsonDataToStream([ - 'menuid' => $menuId, + if (is_null($menuId)) { + // 删除所有菜单 + $response = $this->getClient() + ->setMethod('GET') + ->send($this->buildUrl('/cgi-bin/menu/delete', [ + 'access_token' => $this->app[ServiceProviders::AccessToken]->getToken() ]) - ) - ->send($this->buildUrl( - self::URL['delconditional'], - [ - 'access_token' => $this->getAccessToken() - ] - )); + ); + return $this->checkResponse($response, $parseData); + } - $this->checkResponse($response, $jsonData); - return $jsonData; - } - - /** - * 个性化菜单接口:测试个性化菜单匹配结果 - * @param string $userId 可以是粉丝的OpenID,也可以是粉丝的微信号。 - * @return array - * @throws HttpException - */ - function match(string $userId) - { + // 删除个性化菜单 $response = $this->getClient() ->setMethod('POST') - ->setBody( - $this->jsonDataToStream([ - 'user_id' => $userId, - ]) - ) - ->send($this->buildUrl( - self::URL['trymatch'], - [ - 'access_token' => $this->getAccessToken() - ] - )); - - $this->checkResponse($response, $jsonData); - return $jsonData; + ->setBody($this->jsonDataToStream(['menuid' => $menuId])) + ->send($this->buildUrl('/cgi-bin/menu/delconditional', [ + 'access_token' => $this->app[ServiceProviders::AccessToken]->getToken() + ]) + ); + return $this->checkResponse($response, $parseData); } /** - * 获取自定义菜单配置 - * @return array + * 测试个性化菜单匹配结果 + * @param string $userId + * @return bool * @throws HttpException */ - public function query() + public function match(string $userId) { $response = $this->getClient() - ->setMethod('GET') - ->send($this->buildUrl( - self::URL['get'], - [ - 'access_token' => $this->getAccessToken(), - ] - )); - - $this->checkResponse($response, $jsonData); - return $jsonData; - } - - protected function getAccessToken() - { - return $this->app[ServiceProviders::AccessToken]->getToken(); + ->setMethod('POST') + ->setBody($this->jsonDataToStream(['user_id' => $userId])) + ->send($this->buildUrl('/cgi-bin/menu/trymatch', [ + 'access_token' => $this->app[ServiceProviders::AccessToken]->getToken() + ]) + ); + $this->checkResponse($response, $parseData); + return $parseData; } } \ No newline at end of file