作者 魏强

自定义菜单

@@ -17,8 +17,8 @@ return [ @@ -17,8 +17,8 @@ return [
17 */ 17 */
18 'app_id' => 'wxb4238fbc5c39db5d', // AppID 18 'app_id' => 'wxb4238fbc5c39db5d', // AppID
19 'secret' => '9d95998ab1a23374c7235a30b53f3a3c', // AppSecret 19 'secret' => '9d95998ab1a23374c7235a30b53f3a3c', // AppSecret
20 - 'token' => 'your-token', // Token  
21 - 'aes_key' => '', // EncodingAESKey,安全模式下请一定要填写!!! 20 + 'token' => 'bronet', // Token
  21 + 'aes_key' => 'qblu4O3peCTWjLLEqKqLjQhJNNeCTi2se2EPeVARsbk', // EncodingAESKey,安全模式下请一定要填写!!!
22 /** 22 /**
23 * 日志配置 23 * 日志配置
24 * 24 *
@@ -18,10 +18,7 @@ class AdminIndexController extends PluginBaseController @@ -18,10 +18,7 @@ class AdminIndexController extends PluginBaseController
18 function _initialize() 18 function _initialize()
19 { 19 {
20 parent::_initialize(); 20 parent::_initialize();
21 - $this->options= [  
22 - 'app_id' => config('wechat_config.app_id'),  
23 - 'secret' => config('wechat_config.secret'),  
24 - ]; 21 + $this->options= config('wechat_config');
25 } 22 }
26 23
27 /** 24 /**
@@ -41,17 +38,83 @@ class AdminIndexController extends PluginBaseController @@ -41,17 +38,83 @@ class AdminIndexController extends PluginBaseController
41 38
42 public function index() 39 public function index()
43 { 40 {
44 -// $app = new Application($this->options);  
45 -// $menu = $app->menu;  
46 -// $menus = $menu->all();  
47 -// var_dump($menus);  
48 -  
49 $list=Db::name('wechat_menu')->select()->toArray(); 41 $list=Db::name('wechat_menu')->select()->toArray();
50 $list=$this->arr2tree($list, 'index', 'parent_id'); 42 $list=$this->arr2tree($list, 'index', 'parent_id');
51 $this->assign('list',$list); 43 $this->assign('list',$list);
  44 +
52 return $this->fetch('/admin_index'); 45 return $this->fetch('/admin_index');
53 } 46 }
54 47
  48 + public function sync(){
  49 + $app = new Application($this->options);
  50 + $menu = $app->menu;
  51 + $menus = $menu->current();
  52 + if($menus->is_menu_open==1){
  53 + $selfmenu_info=$menus->selfmenu_info;
  54 + if(isset($selfmenu_info['button'])){
  55 + $data=[];
  56 + $i=1;
  57 + foreach ($selfmenu_info['button'] as $k=>$v){
  58 + if(isset($v['type'])&&$v['type']=='click' && isset($v['key'])){
  59 + $type='keys';
  60 + }elseif (isset($v['type'])&&$v['type']=='click' && isset($v['value'])){
  61 + $type='text';
  62 + }elseif(isset($v['type'])){
  63 + $type=$v['type'];
  64 + }else{
  65 + $type='text';
  66 + }
  67 + if(!empty($v['view'])){
  68 + $content=$v['view'];
  69 + }elseif (!empty($v['key'])){
  70 + $content=$v['key'];
  71 + }elseif (!empty($v['value'])){
  72 + $content=$v['value'];
  73 + }else{
  74 + $content='';
  75 + }
  76 + $data[]=['index'=>$i,'parent_id'=>0,'type'=>$type,'content'=>$content,'name'=>$v['name']];
  77 +
  78 + if(!empty($v['sub_button']['list'])){
  79 + $j=1;
  80 + foreach ($v['sub_button']['list'] as $k1=>$v1){
  81 + if($v1['type']=='click' && isset($v1['key'])){
  82 + $type1='keys';
  83 + }elseif ($v1['type']=='click' && isset($v1['value'])){
  84 + $type1='text';
  85 + }else{
  86 + $type1=$v1['type'];
  87 + }
  88 + if(!empty($v1['url'])){
  89 + $content1=$v1['url'];
  90 + }elseif (!empty($v1['key'])){
  91 + $content1=$v1['key'];
  92 + }elseif (!empty($v1['value'])){
  93 + $content1=$v1['value'];
  94 + }else{
  95 + $content1='';
  96 + }
  97 + $data[]=['index'=>$i.$j,'parent_id'=>$i,'type'=>$type1,'content'=>$content1,'name'=>$v1['name']];
  98 + $j++;
  99 + }
  100 + }
  101 + $i++;
  102 + }
  103 + try {
  104 + Db::transaction(function () use ($data) {
  105 + Db::name('wechat_menu')->where('1=1')->delete();
  106 + Db::name('wechat_menu')->insertAll($data);
  107 + });
  108 + $this->_push();
  109 + } catch (\Exception $e) {
  110 + $this->error('微信菜单发布失败,请稍候再试!' . $e->getMessage());
  111 + }
  112 + $this->success('保存发布菜单成功!', '');
  113 + }
  114 + }
  115 + $this->error('微信菜单尚未启用!');
  116 + }
  117 +
55 /** 118 /**
56 * 微信菜单编辑 119 * 微信菜单编辑
57 */ 120 */
@@ -113,7 +176,7 @@ class AdminIndexController extends PluginBaseController @@ -113,7 +176,7 @@ class AdminIndexController extends PluginBaseController
113 private function _push() 176 private function _push()
114 { 177 {
115 list($map, $field) = [['status' => '1'], 'id,index,parent_id,name,type,content']; 178 list($map, $field) = [['status' => '1'], 'id,index,parent_id,name,type,content'];
116 - $result = (array)Db::name('wechat_menu')->field($field)->where($map)->order('sort ASC,id ASC')->select(); 179 + $result =Db::name('wechat_menu')->field($field)->where($map)->order('sort ASC,id ASC')->select()->toArray();
117 foreach ($result as &$row) { 180 foreach ($result as &$row) {
118 empty($row['content']) && $row['content'] = uniqid(); 181 empty($row['content']) && $row['content'] = uniqid();
119 if ($row['type'] === 'miniprogram') { 182 if ($row['type'] === 'miniprogram') {
@@ -124,35 +187,34 @@ class AdminIndexController extends PluginBaseController @@ -124,35 +187,34 @@ class AdminIndexController extends PluginBaseController
124 } else { 187 } else {
125 $row['url'] = url($row['content'], '', true, true); 188 $row['url'] = url($row['content'], '', true, true);
126 } 189 }
127 - } elseif  
128 - ($row['type'] === 'event') { 190 + } elseif ($row['type'] === 'event') {
129 if (isset($this->menuType[$row['content']])) { 191 if (isset($this->menuType[$row['content']])) {
130 list($row['type'], $row['key']) = [$row['content'], "wechat_menu#id#{$row['id']}"]; 192 list($row['type'], $row['key']) = [$row['content'], "wechat_menu#id#{$row['id']}"];
131 } 193 }
132 } elseif ($row['type'] === 'media_id') { 194 } elseif ($row['type'] === 'media_id') {
133 $row['media_id'] = $row['content']; 195 $row['media_id'] = $row['content'];
134 } else { 196 } else {
135 - $row['key'] = "wechat_menu#id#{$row['id']}"; 197 + $row['key'] = $row['content'];
136 !in_array($row['type'], $this->menuType) && $row['type'] = 'click'; 198 !in_array($row['type'], $this->menuType) && $row['type'] = 'click';
137 } 199 }
138 unset($row['content']); 200 unset($row['content']);
139 } 201 }
140 $menus = $this->arr2tree($result, 'index', 'parent_id', 'sub_button'); 202 $menus = $this->arr2tree($result, 'index', 'parent_id', 'sub_button');
141 //去除无效的字段 203 //去除无效的字段
142 - foreach ($menus as &$menu) {  
143 - unset($menu['index'], $menu['parent_id'], $menu['id']);  
144 - if (empty($menu['sub_button'])) { 204 + foreach ($menus as &$item) {
  205 + unset($item['index'], $item['parent_id'], $item['id']);
  206 + if (empty($item['sub_button'])) {
145 continue; 207 continue;
  208 + }else{
  209 + unset($item['key']);
146 } 210 }
147 - foreach ($menu['sub_button'] as &$submenu) { 211 + foreach ($item['sub_button'] as &$submenu) {
148 unset($submenu['index'], $submenu['parent_id'], $submenu['id']); 212 unset($submenu['index'], $submenu['parent_id'], $submenu['id']);
149 } 213 }
150 - unset($menu['type']); 214 + unset($item['type']);
151 } 215 }
152 -  
153 $app = new Application($this->options); 216 $app = new Application($this->options);
154 - $menu = $app->menu;  
155 - $menu->add($menus); 217 + $app->menu->add($menus);
156 } 218 }
157 219
158 /** 220 /**
@@ -56,17 +56,6 @@ @@ -56,17 +56,6 @@
56 <div class='mobile-header'>公众号</div> 56 <div class='mobile-header'>公众号</div>
57 <div class='mobile-body'></div> 57 <div class='mobile-body'></div>
58 <ul class="mobile-footer"> 58 <ul class="mobile-footer">
59 - <empty name="list">  
60 - <li class="parent-menu" style="width: 50%;">  
61 - <a class="active"><i class="icon-sub hide"></i> <span data-type="text">一级菜单</span></a>  
62 - <div class="sub-menu text-center">  
63 - <ul>  
64 - <li class="menu-add"><a><i class="icon-add"></i></a></li>  
65 - </ul>  
66 - <i class="arrow arrow_out"></i><i class="arrow arrow_in"></i>  
67 - </div>  
68 - </li>  
69 - <else/>  
70 <volist name="list" id="menu"> 59 <volist name="list" id="menu">
71 <li class="parent-menu"> 60 <li class="parent-menu">
72 <a> 61 <a>
@@ -91,7 +80,6 @@ @@ -91,7 +80,6 @@
91 </div> 80 </div>
92 </li> 81 </li>
93 </volist> 82 </volist>
94 - </empty>  
95 <li class="parent-menu menu-add" style="width: 50%;"> 83 <li class="parent-menu menu-add" style="width: 50%;">
96 <a><i class="icon-add"></i></a> 84 <a><i class="icon-add"></i></a>
97 </li> 85 </li>
@@ -165,6 +153,7 @@ @@ -165,6 +153,7 @@
165 <div class="text-center menu-submit-container"> 153 <div class="text-center menu-submit-container">
166 <button class="layui-btn menu-submit">保存发布</button> 154 <button class="layui-btn menu-submit">保存发布</button>
167 <button data-load='cancel' class="layui-btn layui-btn-danger">取消发布</button> 155 <button data-load='cancel' class="layui-btn layui-btn-danger">取消发布</button>
  156 + <button class="layui-btn layui-btn-warm" id="sync" type="button">获取微信菜单</button>
168 </div> 157 </div>
169 </div> 158 </div>
170 <script> 159 <script>
@@ -342,6 +331,11 @@ @@ -342,6 +331,11 @@
342 }; 331 };
343 this.listen(); 332 this.listen();
344 }; 333 };
  334 +
  335 + $('#sync').click(function () {
  336 + $.form.load('sync', {}, 'post');
  337 + })
  338 +
345 }); 339 });
346 </script> 340 </script>
347 </body> 341 </body>