微信公众号自定义菜单添加小程序链接 thinkphp5开
作者:广州小程序开发 时间:2018-07-07 18:08
微信公众号自定义菜单添加小程序链接 thinkphp5开发
微信自定义菜单里面添加小程序方法 getInstanceWchatMenu为读取数据库里的菜单
public function updateInstanceMenuToWeixin($instance_id)
{
$menu = array();
$menu_list = $this->getInstanceWchatMenu($instance_id);
if (! empty($menu_list)) {
foreach ($menu_list as $k => $v) {
if (! empty($v)) {
$menu_item = array(
'name' => ''
);
$menu_item['name'] = $v['menu_name'];
// $menu_item['sub_menu'] = array();
if (! empty($v['child'])) {
foreach ($v['child'] as $k_child => $v_child) {
if (! empty($v_child)) {
$sub_menu = array();
$sub_menu['name'] = $v_child['menu_name'];
// $sub_menu['sub_menu'] = array();
if ($v_child['menu_event_type'] == 1) {
$sub_menu['type'] = 'view';
$sub_menu['url'] = $v_child['menu_event_url'];
}elseif($v_child['menu_event_type'] == 4){
// 小程序
$sub_menu['type'] = 'miniprogram';
$sub_menu['appid'] = $v_child['appid'];
$sub_menu['pagepath'] = $v_child['pagepath'];
$sub_menu['url'] = $v_child['menu_event_url'];
} else {
$sub_menu['type'] = 'click';
$sub_menu['key'] = $v_child['menu_id'];
}
$menu_item['sub_button'][] = $sub_menu;
}
}
} else {
if ($v['menu_event_type'] == 1) {
$menu_item['type'] = 'view';
$menu_item['url'] = $v['menu_event_url'];
}elseif($v['menu_event_type'] == 4){
// 小程序
$menu_item['type'] = 'miniprogram';
$menu_item['appid'] = $v['appid'];
$menu_item['pagepath'] = $v['pagepath'];
$menu_item['url'] = $v['menu_event_url'];
} else {
$menu_item['type'] = 'click';
$menu_item['key'] = $v['menu_id'];
}
}
$menu[] = $menu_item;
}
}
}
$menu_array = array();
$menu_array['button'] = array();
foreach ($menu as $k => $v) {
$menu_array['button'][] = $v;
}
// 汉字不编码
$menu_array = json_encode($menu_array);
// 链接不转义
$menu_array = preg_replace_callback("/\\\u([0-9a-f]{4})/i", create_function('$matches', 'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'), $menu_array);
return $menu_array;
}
生成菜单 $jsonmenu这个是上面方法生成菜单的数组
public function menu_create($jsonmenu)
{
$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=%s";
$result = $this->get_url_return($url, $jsonmenu);
return $result;
}
private function get_url_return($url, $data = '', $needToken = false)
{
// 第一次为空,则从文件中读取
if (empty($this->token)) {
$this->token = cache::get('token-' . $this->author_appid);
}
// 为空则重新取值
if (empty($this->token) or $needToken) {
$this->get_access_token();
$this->token = cache::get('token-' . $this->author_appid);
}
$newurl = sprintf($url, $this->token);
$curl = curl_init(); // 创建一个新url资源
curl_setopt($curl, CURLOPT_URL, $newurl);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (! empty($data)) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$AjaxReturn = curl_exec($curl);
// curl_close();
$strjson = json_decode($AjaxReturn);
if (! empty($strjson->errcode)) {
switch ($strjson->errcode) {
case 40001:
return $this->get_url_return($url, $data, true); // 获取access_token时AppSecret错误,或者access_token无效
break;
case 40014:
return $this->get_url_return($url, $data, true); // 不合法的access_token
break;
case 42001:
return $this->get_url_return($url, $data, true); // access_token超时
break;
case 45009:
return json_encode(array(
"errcode" => - 45009,
"errmsg" => "接口调用超过限制:" . $strjson->errmsg
));
break;
case 41001:
return json_encode(array(
"errcode" => - 41001,
"errmsg" => "缺少access_token参数:" . $strjson->errmsg
));
break;
default:
return json_encode(array(
"errcode" => - 41000,
"errmsg" => $strjson->errmsg
)); // 其他错误,抛出
break;
}
} else {
return $AjaxReturn;
}
}
微信自定义菜单里面添加小程序方法 getInstanceWchatMenu为读取数据库里的菜单
public function updateInstanceMenuToWeixin($instance_id)
{
$menu = array();
$menu_list = $this->getInstanceWchatMenu($instance_id);
if (! empty($menu_list)) {
foreach ($menu_list as $k => $v) {
if (! empty($v)) {
$menu_item = array(
'name' => ''
);
$menu_item['name'] = $v['menu_name'];
// $menu_item['sub_menu'] = array();
if (! empty($v['child'])) {
foreach ($v['child'] as $k_child => $v_child) {
if (! empty($v_child)) {
$sub_menu = array();
$sub_menu['name'] = $v_child['menu_name'];
// $sub_menu['sub_menu'] = array();
if ($v_child['menu_event_type'] == 1) {
$sub_menu['type'] = 'view';
$sub_menu['url'] = $v_child['menu_event_url'];
}elseif($v_child['menu_event_type'] == 4){
// 小程序
$sub_menu['type'] = 'miniprogram';
$sub_menu['appid'] = $v_child['appid'];
$sub_menu['pagepath'] = $v_child['pagepath'];
$sub_menu['url'] = $v_child['menu_event_url'];
} else {
$sub_menu['type'] = 'click';
$sub_menu['key'] = $v_child['menu_id'];
}
$menu_item['sub_button'][] = $sub_menu;
}
}
} else {
if ($v['menu_event_type'] == 1) {
$menu_item['type'] = 'view';
$menu_item['url'] = $v['menu_event_url'];
}elseif($v['menu_event_type'] == 4){
// 小程序
$menu_item['type'] = 'miniprogram';
$menu_item['appid'] = $v['appid'];
$menu_item['pagepath'] = $v['pagepath'];
$menu_item['url'] = $v['menu_event_url'];
} else {
$menu_item['type'] = 'click';
$menu_item['key'] = $v['menu_id'];
}
}
$menu[] = $menu_item;
}
}
}
$menu_array = array();
$menu_array['button'] = array();
foreach ($menu as $k => $v) {
$menu_array['button'][] = $v;
}
// 汉字不编码
$menu_array = json_encode($menu_array);
// 链接不转义
$menu_array = preg_replace_callback("/\\\u([0-9a-f]{4})/i", create_function('$matches', 'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'), $menu_array);
return $menu_array;
}
生成菜单 $jsonmenu这个是上面方法生成菜单的数组
public function menu_create($jsonmenu)
{
$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=%s";
$result = $this->get_url_return($url, $jsonmenu);
return $result;
}
private function get_url_return($url, $data = '', $needToken = false)
{
// 第一次为空,则从文件中读取
if (empty($this->token)) {
$this->token = cache::get('token-' . $this->author_appid);
}
// 为空则重新取值
if (empty($this->token) or $needToken) {
$this->get_access_token();
$this->token = cache::get('token-' . $this->author_appid);
}
$newurl = sprintf($url, $this->token);
$curl = curl_init(); // 创建一个新url资源
curl_setopt($curl, CURLOPT_URL, $newurl);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (! empty($data)) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$AjaxReturn = curl_exec($curl);
// curl_close();
$strjson = json_decode($AjaxReturn);
if (! empty($strjson->errcode)) {
switch ($strjson->errcode) {
case 40001:
return $this->get_url_return($url, $data, true); // 获取access_token时AppSecret错误,或者access_token无效
break;
case 40014:
return $this->get_url_return($url, $data, true); // 不合法的access_token
break;
case 42001:
return $this->get_url_return($url, $data, true); // access_token超时
break;
case 45009:
return json_encode(array(
"errcode" => - 45009,
"errmsg" => "接口调用超过限制:" . $strjson->errmsg
));
break;
case 41001:
return json_encode(array(
"errcode" => - 41001,
"errmsg" => "缺少access_token参数:" . $strjson->errmsg
));
break;
default:
return json_encode(array(
"errcode" => - 41000,
"errmsg" => $strjson->errmsg
)); // 其他错误,抛出
break;
}
} else {
return $AjaxReturn;
}
}