Home >php教程 >php手册 >【slackck】tp3.2 数据库调用显示微信自定义菜单

【slackck】tp3.2 数据库调用显示微信自定义菜单

WBOY
WBOYOriginal
2016-06-07 11:36:172099browse

测试过了 可以用,虽然只做了一个view的类型,其他类型大家可以自己加个判断,这边就是做个参考案例,所以还有些判断流程没时间加进去

数据表上传了,只供学习,后面更新的正式完整版会放在【slackck】后台系统中,整站源码可进QQ群:457295993 群文件中下载
【slackck】tp3.2 数据库调用显示微信自定义菜单

代码如下:


/*
* 生成自定义菜单
*/

public function we_menu_make(){
$we=M('sys')->where(array('sys_id'=>1))->find();//读取微信配置参数
$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$we["wesys_appid"]."&secret=".$we["wesys_appsecret"]."";
$ch=curl_init();//初始化
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$output=curl_exec($ch);
curl_close($ch);
$jsoninfo=json_decode($output,true);
$access_token=$jsoninfo['access_token'];
/*
* 菜单处理开始
* 只取3条leftid=0的数据
*/
$we_menu=M('we_menu')->where(array('we_menu_leftid'=>0,'we_menu_open'=>1))->order('we_menu_order')->limit(3)->select();
/*
* 菜单数据重组
* 重组结构参考微信公共平台开发文档
* name 菜单名称
* type 菜单类型
* url 链接地址:针对view类型
*/
$data = '{"button":[';//菜单头
foreach($we_menu as $v){
$data.='{"name":"'.$v['we_menu_name'].'",';//菜单名称

$count=M('we_menu')->where(array('we_menu_leftid'=>$v['we_menu_id'],'we_menu_open'=>1))->limit(5)->order('we_menu_order')->count();//判断是否有子栏目
if($count){//二级栏目
$data.='"sub_button":[';
$we_twomenu=M('we_menu')->where(array('we_menu_leftid'=>$v['we_menu_id'],'we_menu_open'=>1))->order('we_menu_order')->limit(5)->select();
$k=0;
foreach($we_twomenu as $t){
$k=$k+1;
$data.='{"name":"'.$t['we_menu_name'].'",';
$data.='"type":"view",';
$data.='"url":"http://www.baidu.com"';
if ($k==$count){
$data.= '}';
}else{
$data.= '},';
}
}
$data.= ']},';
}else{
$data.='"type":"view",';
$data.='"url":"http://www.baidu.com"';
}
}
$data.= '},]';
$data.= '}';

$url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=$access_token";
$ch=curl_init();//初始化
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
$this->success('菜单生成成功',U('we_menu'),1);
curl_exec($ch);
curl_close($ch);
return $access_token;
}

附件 mr_we_menu.rar ( 911 B 下载:66 次 )

AD:真正免费,域名+虚机+企业邮箱=0元

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn