<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                [TOC] # 自定義菜單 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141013 ![](https://box.kancloud.cn/1611d5c2f7433ae43670becbf7121779_651x262.png) 微信公眾號默認沒有開通底部是沒有菜單功能,需要使用者自行來創建開通。 注意點: * 自定義菜單最多包括3個一級菜單,每個一級菜單最多包含5個二級菜單。 * 一級菜單最多4個漢字,二級菜單最多7個漢字,多出來的部分將會以“...”代替。 * 創建自定義菜單后,菜單的刷新策略是,在用戶進入公眾號會話頁或公眾號profile頁時,如果發現上一次拉取菜單的請求在5分鐘以前,就會拉取一下菜單,如果菜單有更新,就會刷新客戶端的菜單。測試時可以嘗試取消關注公眾賬號后再次關注,則可以看到創建后的效果。 5分鐘后更新,如果要實時,測試時可以嘗試取消關注公眾賬號后再次關注 # 按鈕事件 ![](https://box.kancloud.cn/b9821efee346878135c3c4f67eee2bc0_1412x403.png) ![](https://box.kancloud.cn/688e1fd70b7f60425c493e1c534b2dbe_1413x77.png) 注: * 最常用的自定義類型按鈕為:view 它相當有html中的a作用 * view想跳轉到指定的URL,需要認證后的公眾號才可以 # 自定義菜單的創建 [https://mp.weixin.qq.com/wiki?t=resource/res\_main&id=mp1421141013](https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141013) ![](https://box.kancloud.cn/754cb6969037a5a26102105f1aca02b9_1108x625.png) 創建menu.php 用到這個來創建菜單 ![](https://box.kancloud.cn/3ed4c7b4eafd4f99d94f3d26dc4acd38_562x288.png) ~~~ <?php return '{ "button":[ { "type":"click", "name":"首頁", "key":"index001" }, { "name":"最新活動", "sub_button":[ { "type":"view", "name":"搜索", "url":"http://m.baidu.com/" }, { "type":"click", "name":"客服", "key":"kefu001" },{ "type": "pic_sysphoto", "name": "系統拍照", "key": "photo001" },{ "name": "發送位置", "type": "location_select", "key": "rselfmenu_2_0" }] }, { "type":"view", "name":"個人中心", "url":"http://m.itcast.cn/" } ] }'; ~~~ 在代碼中寫 (省略curl那個方法) ~~~ <?php /** * 主動模式 */ $wx = new Wechat(); $menuList = include './menu.php'; echo $wx->createMenu($menuList); class Wechat { // appid const APPID = 'wx77c58459abaf8ad6'; // appsecret const SECRET = '8074c29bdfa71fdbe9debc19060275dc'; /** * 得到access_token access_token是全局唯一有效的 * @return [type] [description] */ public function getAccessToken(){ # 緩存的文件 $cacheFile = self::APPID.'_cache.log'; // 判斷文件是否存在,要是不存在則表示沒有緩存 // 存在判斷修改的時間是否過了有效期,如果沒有過,則不進行url網絡請求 if (is_file($cacheFile) && filemtime($cacheFile)+7000 > time()) { return file_get_contents($cacheFile); } // 第1次或緩存過期 $surl = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s'; $url = sprintf($surl,self::APPID,self::SECRET); // 發起GET請求 http_request是我們封裝的函數 $json = $this->http_request($url); // 把json轉為數組 $arr = json_decode($json,true); $access_token = $arr['access_token']; // 寫緩存 file_put_contents($cacheFile,$access_token); // 返回數據 return $access_token; } /** * 創建自定義菜單 * @param array|string $menu [description] * @return [type] [description] */ public function createMenu($menu){ if(is_array($menu)){ // 因為菜單有中文,所以一定要寫json_encode第2個參數,讓中文不亂碼 $data = json_encode($menu,JSON_UNESCAPED_UNICODE); # 256 }else{ $data = $menu; } // 創建自定義菜單URL $url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$this->getAccessToken(); // 發起請求 $json = $this->http_request($url,$data); return $json; } /** * 發起請求 * @param strin $url url地址 * @param string|array $ret 請求體 * @param string $file 上傳的文件絕對地址 * @return [type] [description] */ private function http_request($url,$ret='',$file=''){ ~~~ 我們自己執行下這個代碼 成功后會返回 ~~~ { "errcode":?0, "errmsg":?"ok" } ~~~ # 刪除自定義菜單 ![](https://box.kancloud.cn/408e4b28b3e820c27bcee8de4677fdf4_1116x344.png) ~~~ /** * 刪除自定義菜單 * @return [type] [description] */ public function delMenu(){ $url = 'https://api.weixin.qq.com/cgi-bin/menu/delete?access_token='.$this->getAccessToken(); // 發起請求 $json = $this->http_request($url); return $json; } ~~~ ~~~ $wx = new Wechat(); //$menuList = include './menu.php'; //echo $wx->createMenu($menuList); # 刪除菜單 echo $wx->delMenu(); ~~~ 會全部刪除 我們自己執行下這個代碼 成功后會返回 ~~~ { "errcode":?0, "errmsg":?"ok" } ~~~ 用處是,全部刪除,創建新的 # 點擊事件的處理 `"type":"click"` 點擊事件需要捕獲是個event ![](https://box.kancloud.cn/a8525fab354f930bb3e37b3faccb0173_888x199.png) 我們寫到另一個文件,被動回復的那邊 他會自動調用我們的方法的 `echo $ret = call_user_func([$this,$fun],$obj);` ~~~ /** * 事件處理 */ private function eventFun($obj){ // 事件的名稱 $Event = $obj->Event; switch ($Event) { case 'CLICK': // 關于點擊事件 return $this->clickFun($obj); break; } } // 按鈕的點擊事件 private function clickFun($obj){ $EventKey = $obj->EventKey; //字符串就是你定義的菜單那邊的key if ('index001' == $EventKey) { return $this->createText($obj,'你點擊首頁按鈕'); }elseif('kefu001' == $EventKey){ return $this->createText($obj,'你點擊找客服小姐姐!'); } return $this->createText($obj,'我解決不了!'); } ~~~ 我們判斷點擊事件,然后根據key來返回東西 # 自定義菜單事件推送 click和view很常用,**只有認證的公眾號使用view事件才能跳轉到任意url中**
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看