目标
本文的目标是实现微信公众号菜单的创建与删除
创建路由
1 2 3 4 5 6 7 8 9 10 11
| Route::get('/create-menus', [ 'as' => 'wechat.create.menus', 'uses' => 'WeChatServerController@createMenus', ]);
Route::get('/destroy-menus', [ 'as' => 'wechat.destroy.menus', 'uses' => 'WeChatServerController@destroyMenus', ]);
|
修改控制器
修改控制器 app/Http/Controllers/WeChat/WeChatServerController.php,新增代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| /** * 创建新的微信菜单 * @param WeChat $weChat * @return string */ public function createMenus(WeChat $weChat) { $menu = $weChat->menu; $buttons = [ [ "type" => "view", "name" => "进入好答", "url" => route('wechat.index'), ], [ "type" => "click", "name" => "听我唱歌", "key" => "K0001_LET_ME_SING_MY_SONG", ], ];
$json = $menu->add($buttons); $result = json_decode($json, true);
if ($result['errcode'] == 0 && $result['errmsg'] == 'ok') { echo "创建菜单成功"; } else { echo "创建菜单失败"; } }
/** * 删除所有微信菜单 * @param WeChat $weChat * @return string */ public function destroyMenus(WeChat $weChat) { $result = $weChat->menu->destroy();
if ($result['errcode'] == 0 && $result['errmsg'] == 'ok') { echo "删除菜单成功"; } else { echo "删除菜单失败"; } }
|