<?php
namespace Wechat;
use Wechat\Lib\Common;
use Wechat\Lib\Tools;
/**
* 微信菜单操作SDK
*
* @author Anyon <zoujingli@qq.com>
* @date 2016/06/28 11:52
*/
class WechatMenu extends Common
{
const MENU_ADD_URL = '/menu/create?';
const MENU_GET_URL = '/menu/get?';
const MENU_DEL_URL = '/menu/delete?';
const COND_MENU_ADD_URL = '/menu/addconditional?';
const COND_MENU_DEL_URL = '/menu/delconditional?';
const COND_MENU_TRY_URL = '/menu/trymatch?';
public function createMenu($data)
{
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_URL_PREFIX . self::MENU_ADD_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (empty($json) || !empty($json['errcode'])) {
$this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
$this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return true;
}
return false;
}
public function getMenu()
{
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpGet(self::API_URL_PREFIX . self::MENU_GET_URL . "access_token={$this->access_token}");
if ($result) {
$json = json_decode($result, true);
if (empty($json) || !empty($json['errcode'])) {
$this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
$this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
public function deleteMenu()
{
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpGet(self::API_URL_PREFIX . self::MENU_DEL_URL . "access_token={$this->access_token}");
if ($result) {
$json = json_decode($result, true);
if (empty($json) || !empty($json['errcode'])) {
$this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
$this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return true;
}
return false;
}
public function createCondMenu($data)
{
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$result = Tools::httpPost(self::API_URL_PREFIX . self::COND_MENU_ADD_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (empty($json) || !empty($json['errcode'])) {
$this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
$this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json['menuid'];
}
return false;
}
public function deleteCondMenu($menuid)
{
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('menuid' => $menuid);
$result = Tools::httpPost(self::API_URL_PREFIX . self::COND_MENU_DEL_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (empty($json) || !empty($json['errcode'])) {
$this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
$this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return true;
}
return false;
}
public function tryCondMenu($openid)
{
if (!$this->access_token && !$this->getAccessToken()) {
return false;
}
$data = array('user_id' => $openid);
$result = Tools::httpPost(self::API_URL_PREFIX . self::COND_MENU_TRY_URL . "access_token={$this->access_token}", Tools::json_encode($data));
if ($result) {
$json = json_decode($result, true);
if (empty($json) || !empty($json['errcode'])) {
$this->errCode = isset($json['errcode']) ? $json['errcode'] : '505';
$this->errMsg = isset($json['errmsg']) ? $json['errmsg'] : '无法解析接口返回内容!';
return $this->checkRetry(__FUNCTION__, func_get_args());
}
return $json;
}
return false;
}
}