微信中获取access_token,同步菜单,创建自定义菜单

一:获取access_token

access_token是公众号的全局唯一接口调用凭据,公众号调用各接口时都需使用access_token。开发者需要进行妥善保存。access_token的存储至少要保留512个字符空间。access_token的有效期目前为2个小时,需定时刷新,重复获取将导致上次获取的access_token失效。

   1.首先定义一个函数用来获取正在使用的公众号

function getCurrentMp(){
	$mp=M('mp')->where('is_use=1')->find();
	return $mp;
}
2.获取正在使用的公众号的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');

		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('mp_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('mp_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=$this->mp;
       $data1=I('post.data');

       $arr=array();
       foreach ($data1 as $key => $value){
           $row=array();
           $row['mp_id']=$mp['id'];
           $row['index']=$value['index'];
           $row['pindex']=$value['pindex'];
           $row['type']=$value['type'];
           $row['name']=$value['name'];
           $row['content']=$value['code'];
           $row['sort']=$value['sort'];
           $arr[]=$row;
    
     }
      $model= M('mp_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/f__theone/article/details/79927649
今日推荐