作者 魏强

自定义菜单

... ... @@ -17,8 +17,8 @@ return [
*/
'app_id' => 'wxb4238fbc5c39db5d', // AppID
'secret' => '9d95998ab1a23374c7235a30b53f3a3c', // AppSecret
'token' => 'your-token', // Token
'aes_key' => '', // EncodingAESKey,安全模式下请一定要填写!!!
'token' => 'bronet', // Token
'aes_key' => 'qblu4O3peCTWjLLEqKqLjQhJNNeCTi2se2EPeVARsbk', // EncodingAESKey,安全模式下请一定要填写!!!
/**
* 日志配置
*
... ...
... ... @@ -18,10 +18,7 @@ class AdminIndexController extends PluginBaseController
function _initialize()
{
parent::_initialize();
$this->options= [
'app_id' => config('wechat_config.app_id'),
'secret' => config('wechat_config.secret'),
];
$this->options= config('wechat_config');
}
/**
... ... @@ -41,17 +38,83 @@ class AdminIndexController extends PluginBaseController
public function index()
{
// $app = new Application($this->options);
// $menu = $app->menu;
// $menus = $menu->all();
// var_dump($menus);
$list=Db::name('wechat_menu')->select()->toArray();
$list=$this->arr2tree($list, 'index', 'parent_id');
$this->assign('list',$list);
return $this->fetch('/admin_index');
}
public function sync(){
$app = new Application($this->options);
$menu = $app->menu;
$menus = $menu->current();
if($menus->is_menu_open==1){
$selfmenu_info=$menus->selfmenu_info;
if(isset($selfmenu_info['button'])){
$data=[];
$i=1;
foreach ($selfmenu_info['button'] as $k=>$v){
if(isset($v['type'])&&$v['type']=='click' && isset($v['key'])){
$type='keys';
}elseif (isset($v['type'])&&$v['type']=='click' && isset($v['value'])){
$type='text';
}elseif(isset($v['type'])){
$type=$v['type'];
}else{
$type='text';
}
if(!empty($v['view'])){
$content=$v['view'];
}elseif (!empty($v['key'])){
$content=$v['key'];
}elseif (!empty($v['value'])){
$content=$v['value'];
}else{
$content='';
}
$data[]=['index'=>$i,'parent_id'=>0,'type'=>$type,'content'=>$content,'name'=>$v['name']];
if(!empty($v['sub_button']['list'])){
$j=1;
foreach ($v['sub_button']['list'] as $k1=>$v1){
if($v1['type']=='click' && isset($v1['key'])){
$type1='keys';
}elseif ($v1['type']=='click' && isset($v1['value'])){
$type1='text';
}else{
$type1=$v1['type'];
}
if(!empty($v1['url'])){
$content1=$v1['url'];
}elseif (!empty($v1['key'])){
$content1=$v1['key'];
}elseif (!empty($v1['value'])){
$content1=$v1['value'];
}else{
$content1='';
}
$data[]=['index'=>$i.$j,'parent_id'=>$i,'type'=>$type1,'content'=>$content1,'name'=>$v1['name']];
$j++;
}
}
$i++;
}
try {
Db::transaction(function () use ($data) {
Db::name('wechat_menu')->where('1=1')->delete();
Db::name('wechat_menu')->insertAll($data);
});
$this->_push();
} catch (\Exception $e) {
$this->error('微信菜单发布失败,请稍候再试!' . $e->getMessage());
}
$this->success('保存发布菜单成功!', '');
}
}
$this->error('微信菜单尚未启用!');
}
/**
* 微信菜单编辑
*/
... ... @@ -113,7 +176,7 @@ class AdminIndexController extends PluginBaseController
private function _push()
{
list($map, $field) = [['status' => '1'], 'id,index,parent_id,name,type,content'];
$result = (array)Db::name('wechat_menu')->field($field)->where($map)->order('sort ASC,id ASC')->select();
$result =Db::name('wechat_menu')->field($field)->where($map)->order('sort ASC,id ASC')->select()->toArray();
foreach ($result as &$row) {
empty($row['content']) && $row['content'] = uniqid();
if ($row['type'] === 'miniprogram') {
... ... @@ -124,35 +187,34 @@ class AdminIndexController extends PluginBaseController
} else {
$row['url'] = url($row['content'], '', true, true);
}
} elseif
($row['type'] === 'event') {
} elseif ($row['type'] === 'event') {
if (isset($this->menuType[$row['content']])) {
list($row['type'], $row['key']) = [$row['content'], "wechat_menu#id#{$row['id']}"];
}
} elseif ($row['type'] === 'media_id') {
$row['media_id'] = $row['content'];
} else {
$row['key'] = "wechat_menu#id#{$row['id']}";
$row['key'] = $row['content'];
!in_array($row['type'], $this->menuType) && $row['type'] = 'click';
}
unset($row['content']);
}
$menus = $this->arr2tree($result, 'index', 'parent_id', 'sub_button');
//去除无效的字段
foreach ($menus as &$menu) {
unset($menu['index'], $menu['parent_id'], $menu['id']);
if (empty($menu['sub_button'])) {
foreach ($menus as &$item) {
unset($item['index'], $item['parent_id'], $item['id']);
if (empty($item['sub_button'])) {
continue;
}else{
unset($item['key']);
}
foreach ($menu['sub_button'] as &$submenu) {
foreach ($item['sub_button'] as &$submenu) {
unset($submenu['index'], $submenu['parent_id'], $submenu['id']);
}
unset($menu['type']);
unset($item['type']);
}
$app = new Application($this->options);
$menu = $app->menu;
$menu->add($menus);
$app->menu->add($menus);
}
/**
... ...
... ... @@ -56,17 +56,6 @@
<div class='mobile-header'>公众号</div>
<div class='mobile-body'></div>
<ul class="mobile-footer">
<empty name="list">
<li class="parent-menu" style="width: 50%;">
<a class="active"><i class="icon-sub hide"></i> <span data-type="text">一级菜单</span></a>
<div class="sub-menu text-center">
<ul>
<li class="menu-add"><a><i class="icon-add"></i></a></li>
</ul>
<i class="arrow arrow_out"></i><i class="arrow arrow_in"></i>
</div>
</li>
<else/>
<volist name="list" id="menu">
<li class="parent-menu">
<a>
... ... @@ -91,7 +80,6 @@
</div>
</li>
</volist>
</empty>
<li class="parent-menu menu-add" style="width: 50%;">
<a><i class="icon-add"></i></a>
</li>
... ... @@ -165,6 +153,7 @@
<div class="text-center menu-submit-container">
<button class="layui-btn menu-submit">保存发布</button>
<button data-load='cancel' class="layui-btn layui-btn-danger">取消发布</button>
<button class="layui-btn layui-btn-warm" id="sync" type="button">获取微信菜单</button>
</div>
</div>
<script>
... ... @@ -342,6 +331,11 @@
};
this.listen();
};
$('#sync').click(function () {
$.form.load('sync', {}, 'post');
})
});
</script>
</body>
... ...