查询菜单列表:
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>
效果:
文章评论(0)