关于获取access_token,创建自定义菜单

首先定义一个函数存储获取access_token的方法
<?php
//获取正在使用的公众号
function getCurrentMp(){
	$mp=M('mp')->where('is_use=1')->find();
	return $mp;
}
//获取正在使用的公众号的access_token
function getAccess_token(){
	$mp=M('mp')->where('is_use=1')->find();
	if(empty($mp)) return false;
	$id=$mp['id'];//正在使用的公众号的主键

	if(empty($mp['access_token']) || $mp['expire_time']<time()){
		$appid=$mp['appid'];
		$appsecret=$mp['appsecret'];
		$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;

		include APP_PATH.'LaneWeChat/lanewechat.php';
		$arr= \LaneWeChat\Core\Curl::callWebServer($url,'','GET');
		//将获取到的access_token存入数据库
		if(isset($arr['access_token'])){
			$data['access_token']=$arr['access_token'];
			$data['expire_time']=$arr['expires_in'] + time()-200;

			M('mp')->where("id=$id")->save($data);
			return $arr['access_token'];
		}else{
			return false;
		}
	}else{
		return $mp['access_token'];
	}
}
?>

然后同步菜单

//同步菜单
	public function downmenu(){
		$mp = $this->mp;
		$mp_id = $mp['id'];

		include APP_PATH.'LaneWeChat/lanewechat.php';
		$menu=Menu::getMenu($arr);

		// 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('menu');
		$model->where("mp_id={$mp['id']}")->delete();
		foreach ($arr as $key => $value) {
			$model->add($value);	
		}
		$this->redirect('index');
		
	}

创建自定义菜单

 public function index(){
		$mp = $this->mp;
		$data = M('menu')->where("mp_id={$mp['id']}")->order('id')->select();
		// print_r($data);
		// exit;
		$data2 = $data;
		foreach ($data as $k => $v) {
			foreach ($data2 as $k2 => $v2) {
				if ($v['index'] == $v2['pindex']) {
					$data[$k]['sub'][] = $v2;
					unset($data[$k2]);
				}
			}
		}
		// print_r($data);
		// exit;
		$this->assign('mpInfo',$mp);
		$this->assign('list',$data);
		$this->display();
	}


	public function menuedit(){
		// $mp = M('mp')->where('is_use=1')->find();
		$mp = $this->mp;
		// 写入数据库
		$data1 = I('post.data');
		$arr = array();
		foreach ($data1 as $key1 => $value1){
			$row = array();
			$row['mp_id'] = $mp['id'];
			$row['index'] = $value1['index'];
			$row['pindex'] = $value1['pindex'];
			$row['type'] = $value1['type'];
			$row['name'] = $value1['name'];
			$row['content'] = $value1['content'];
			$row['sort'] = $value1['sort'];
			$arr[] = $row;
		}
		$model=M('Menu');
        $model->where("mp_id = {$mp['id']}")->delete();
        $model->addAll($arr);
     	//创建微信公众号的菜单
		include APP_PATH.'LaneWeChat/lanewechat.php';
		$ret=Menu::setMenu($arr);
		if($ret === true){
			$this->ajaxReturn(array('msg'=>"创建菜单成功"));
		}else{
			$this->ajaxReturn(array('msg'=>$ret));
		}
	}


猜你喜欢

转载自blog.csdn.net/ssh456/article/details/79924295