Skip to content

Commit

Permalink
officialAccount: fix Menu Client
Browse files Browse the repository at this point in the history
  • Loading branch information
XueSiLf authored and RunsTp committed Apr 1, 2021
1 parent 014cc86 commit e147a3e
Showing 1 changed file with 77 additions and 131 deletions.
208 changes: 77 additions & 131 deletions src/OfficialAccount/Menu/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit e147a3e

Please sign in to comment.