<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST , ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace plugin\wechat\admin\controller;
use \temmoku\controller;
use \temmoku\db;
use \plugin\wechat\lib\common;
use \plugin\wechat\admin\model\index;
use plugin\wechat\public_class\cache;
class menu extends controller{
	
	public function alllist(){
		if($_POST['step']=='post'){
			
			foreach($_POST['menu'] AS $row){
				$_row=[];
				if($row['name']=="" || !$row['name']){
					continue;
				}
				$_row['name']=$row['name'];
				$_row['type']=$row['type'];
				if($row['type']=='view' ){
					$_row['url']=$row['url'];
				}elseif($row['type']=='click'){
					$_row['key']=$row['key'];
				}elseif($row['type']=='miniprogram'){
					$_row['url']=$row['url'];
					$_row['appid']=$row['appid'];
					$_row['pagepath']=$row['pagepath'];
				}
				
				$list=$row['list'];
				unset($row['list']);
				
				foreach($list AS $row_){
					if($row_['name']==""){
						continue;
					}
					$_roww=[];
					$_roww['name']=$row_['name'];
					$_roww['type']=$row_['type'];
					if($row_['type']=='view'){
						$_roww['url']=$row_['url'];
					}elseif($row_['type']=='click'){
						$_roww['key']=$row_['key'];
					}elseif($row_['type']=='miniprogram'){
						$_roww['url']=$row_['url'];
						$_roww['appid']=$row_['appid'];
						$_roww['pagepath']=$row_['pagepath'];
					}
					$_row['list'][]=$_roww;
				}
				
				$menuDB[]=$_row;
			}
			unset($_POST['menu']);
			$_POST['wechat']['Menu']=serialize($menuDB);
			(new index)->setting();
			cache::setting_cache();
			
			//重新设置，然后将值转发到微信
			foreach($menuDB AS $row){
				$list=$row['list'];
				unset($row['list']);
				if(is_array($list)){
					foreach($list AS $_row){
						$row['sub_button'][]=$_row;
					}
				}
				
				if($row['sub_button']){
					unset($row['type']);
					unset($row['url']);
					unset($row['appid']);
					unset($row['pagepath']);
					unset($row['key']);
				}
				$wechat['button'][]=$row;
			}
			$json=json_encode($wechat,JSON_UNESCAPED_UNICODE);
			$access_token= (new Common)->Get_Token();
			//先清空微信内的菜单
			http_request("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=$access_token");
			$data = http_request("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=$access_token",$json);
			$data=json_decode($data,true);
			if($data['errcode'] != '0'){
				$errcode=C('wechat|errcode.'.$data['errcode']);
				if($errcode){
					$this->err('<p>菜单生成失败，错误如下:</p>'.$errcode);
				}else{
					$this->err('<p>菜单生成失败，以下是微信给出的错误信息:</p>'.$data['errmsg']);
				}
			}
			$this->success('菜单生成成功');
		}
		
		$_settingDB=db::select('*')->from(P_jab."set")->query();
    	foreach($_settingDB AS $row){
    		$settingDB[$row['u_key']]=$row['u_value'];
    	}
		$this->assign('settingDB',$settingDB);
		
		$this->display(tpl('menu/alllist'));
		
	}
}
?>