根据appid appsercet 请求服务器 服务器返回用户一个accesstoken 根据微信提供的接口参数accesstoken 对公众号添加自定义菜单
<?php
namespace Admin\Controller;
use Think\Controller;
use LaneWeChat\Core\Menu;
class MenuController extends Controller{
// public function getAccessToken(){
// var_dump(getAccess_Token());
// }
public function mp(){
$mp = getCurrentMp();
return $mp;
}
public function menu(){
$mp = getCurrentMp();
if(empty($mp)){
$this->error('无使用的公众号',U('mp/index'));
}
$data = M('mp_menu')->where("mp_id={$mp['id']}")->order('id')->select();
$data2 = $data;
foreach($data as $key => $value){
foreach ($data2 as $key1 => $value1) {
if($value['index'] == $value1['pindex']){
$data[$key]['sub'][] = $value1;
unset($data[$key1]);
}
}
}
$this->assign('mpInfo',$mp);
$this->assign('list',$data);
$this->display('menu');
}
public function menuedit(){
$mp = getCurrentMp();
if(empty($mp)){
$this->ajaxReturn(array('error'=>false,'msg'=>'无使用的公众号','url'=>U('mp/index')));
exit;
}
$data1 = I('post.data');
foreach ($data1 as &$value) {
$value['mp_id'] = $mp['id'];
}
// $this->ajaxReturn(array('error'=>false,'data'=>$data1));
// exit;
$model = M('mp_menu');
$model->where("mp_id={$mp['id']}")->delete();
$model->addAll($data1);
// print_r($data1);
// exit;
include APP_PATH . 'LaneWeChat/lanewechat.php';
$ret = Menu::setMenu($data1);
// print_r($ret);
// exit;
file_put_contents('menu.txt', $ret);
// print_r($ret);
// exit;
if($ret=== true){
$this->ajaxReturn(array('error'=>true,'msg'=>'创建菜单成功','url'=>U('menu/menu')));
}else{
$this->ajaxReturn(array('error'=>false,'msg'=>$ret,'url'=>U('menu/menu')));
}
// exit;
// $this->display('menu');
}
//同步菜单
public function downmenu(){
$mp = $this->mp();
$mp_id = $mp['id'];
include APP_PATH . 'LaneWeChat/lanewechat.php';
$menu = Menu::getMenu($data);
// print_r($menu);
// // exit;
// print_r($menu['menu']['button']);
// exit;
$menu = $menu['menu']['button'];
$arr = array();
$index = 1;
foreach ($menu as $key => &$value) {
$value['mp_id']= $mp_id;
$value['index'] = $index;
$value['pindex'] = 0;
$value['sort'] = $index;
if(!empty($value['sub_button'])){
$value['type'] = '';//设置一级菜单,默认值
$value['content'] = '';//设置一级菜单,默认值
$sub_menu = $value['sub_button']; //把二级菜单入到变量$sub_menu
unset($value['sub_button']);
$arr[] = $value;
$subindex = 1;
foreach ($sub_menu as $subkey=>&$subvalue) {
$subvalue['mp_id'] = $mp_id;
$subvalue['index'] = $index . $subindex;
$subvalue['pindex'] = $index;
$subvalue['sort'] = $subindex;
if($subvalue['type']== 'click'){
$subvalue['content'] = $subvalue['key'];
unset($subvalue['key']);
}else if($subvalue['type']=='view'){
$subvalue['content'] = $subvalue['url'];
unset($subvalue['url']);
}else {
$subvalue['content'] = $subvalue['type'];
$subvalue['type'] = 'event';
unset($subvalue['key']);
}
unset($subvalue['sub_button']);
$arr[] = $subvalue;
$subindex++;
}
}else{
if($value['type']== 'click'){
$value['content'] = $value['key'];
unset($value['key']);
}else if($value['type']=='view'){
$value['content'] = $value['url'];
unset($value['url']);
}else {
$value['content'] = $value['type'];
$value['type'] = 'event';
unset($value['key']);
}
unset($value['sub_button']);
$arr[] = $value;
}
$index++;
}
// print_r($arr);
// exit;
$model = M('mp_menu');
$model->where("mp_id={$mp['id']}")->delete();
foreach ($arr as $key => $value) {
$model->add($value);
}
$this->redirect('menu');
}
public function delemenu(){
$mp = $this->mp();
$model = M('mp_menu');
$model->where("mp_id={$mp['id']}")->delete();
include APP_PATH . 'LaneWeChat/lanewechat.php';
$ret = Menu::delMenu();
if($ret=== true){
$this->success('删除菜单成功','menu/menu');
}else{
$this->error('删除失败','menu/menu');
}
}
}