微信公众号 – 自定义菜单

2017-05-12   阅读:2956   类型:后端   分类:微信开发    标签: 微信开发

查询菜单列表:

public function navlist(){
        $navlist= \think\Db::name('wxmenu')->order('id desc')->select();
        // halt($navlist);
        $aa=json_encode($navlist);
        // halt($aa);
        $this->assign('navlist',$navlist);
        return $this->fetch();
    }

提交微信公众号并生成菜单:

//生成微信公众号菜单
    public function createmenu(){
        $accesstoken=$this->gettoken();
        $url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$accesstoken;
        
        $navlist= \think\Db::name('wxmenu')->where("pid=0")->order('id asc')->select();
        $menuarr=array();
        foreach($navlist as $k=>$r){
            $issubcat= \think\Db::name('wxmenu')->where("pid=".$r["id"])->order('id asc')->select();
            if($issubcat){
                $menuarr["button"][$k]["name"]=$r["name"];
                
                $subcat=array();
                foreach($issubcat as $k2=>$r2){
                    $subcat[$k2]["type"]=$r2["mtype"];
                    $subcat[$k2]["name"]=$r2["name"];
                    if($r2["mtype"]=="click"){
                        $subcat[$k2]["key"]=$r2["content"];
                    }
                    else{
                        $subcat[$k2]["url"]=$r2["content"];
                    }
                    
                }
                $menuarr["button"][$k]["sub_button"]=$subcat;
            }
            else{
                $menuarr["button"][$k]["type"]=$r["mtype"];
                $menuarr["button"][$k]["name"]=$r["name"];
                if($r["mtype"]=="click"){
                    $menuarr["button"][$k]["key"]=$r["content"];
                }
                else{
                    $menuarr["button"][$k]["url"]=$r["content"];
                }
            }
        }
        $aa=json_encode($menuarr,JSON_UNESCAPED_UNICODE);
        
        // print_r($aa);exit;
        $re=$this->https_request($url,$aa);
        //{"errcode":0,"errmsg":"ok"}
        $result=json_decode($re,true);
        if($result["errmsg"]=='ok'){
            $this->success("生成成功",'navlist');
        }
        else{
            $this->error("生成失败:".$result["errmsg"],'navlist');
        }
        // print_r($re);exit;
        //invalid charset. please check your request, if include \uxxxx will create fail! hint: [fzL3fA0681vr21] 
        //invalid button url domain hint: [ywtvaA0831vr22] 
        //生成失败:invalid button size hint: [RB3fza0975vr22] 
        
    }
    //POST提交
    public function https_request($url,$data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        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);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    //获取公众号ACCESS_TOKEN
    public function gettoken(){
        $appid="wx0287c1d30ce085ace";
        $secret="5cdfc65569383b134a3c1c2e9c987456";
        $gettoken=$this->https_request("https://api.weixin.qq.com/cgi-bin/token?
grant_type=client_credential&appid=".$appid."&secret=".$secret);
        // print_r($gettoken);exit;
        $accesstoken=json_decode($gettoken,true);
        
        return $accesstoken["access_token"];
    }

按钮:

<ul class="pagination no-margin">
     <input type="button" class="btn btn-xs btn-success" value="生成微信公众号菜单" 
     onclick="location.href='{:url('admin/Weixin/createmenu')}'">
</ul>

效果:

1494580891558571.jpg

腾讯云11.11上云拼团Go,2核2G3M云服务器 28元/年

‘简忆博客’微信公众号 扫码关注‘简忆博客’微信公众号,获取最新文章动态
转载:请说明文章出处“来源简忆博客”。http://tpxhm.com/adetail/86.html

×
觉得文章有用就打赏一下文章作者
微信扫一扫打赏 微信扫一扫打赏
支付宝扫一扫打赏 支付宝扫一扫打赏

文章评论(0)

登录
简忆博客壁纸一
简忆博客壁纸二
简忆博客壁纸三
简忆博客壁纸四
简忆博客壁纸五
简忆博客壁纸六
简忆博客壁纸七
简忆博客壁纸八
头像

简忆博客
勤于学习,乐于分享

置顶推荐

打赏本站

如果你觉得本站很棒,可以通过扫码支付打赏哦!
微信扫码:你说多少就多少~
微信扫码
支付宝扫码:你说多少就多少~
支付宝扫码
×