微信生成带参数的二维码,以及给粉丝添加标签

(1)生成带参数的二维码

        在控制器中写一个自动验证的方法再写一个增加二维码的方法

public function _initialize(){
		$mp=getCurrentMp();
		if(empty($mp)){
			$this->error('无使用公众号',U('index'));
		}else{
			$this->mp=$mp;
		}
	}
public function qrcodeAdd(){
		if(IS_GET){
			$this->display();
		}else{
			$mp=$this->mp;
			$arr=I('post.');
			// dump($arr);
			// exit;
			$arr['mp_id']=$mp['id'];
			$id=M('weixin_qrcode')->add($arr);
			//调用创建标签
			$this->createTag($arr['scene_str']);
			
			include APP_PATH.'LaneWeChat/lanewechat.php';
			$ret=Popularize::createTicket($arr['type'],$arr['expire'],$arr['scene_str']);
			// dump($ret);
			// exit;
			if(isset($ret['ticket'])){
				//在页面上显示二维码图片
				$qrcodefile=Popularize::getQrcode($ret['ticket']);
				$ret['src']=$qrcodefile;
				$ret['create_time']=time();
				M('weixin_qrcode')->where("id=$id")->save($ret);
				$this->ajaxReturn(array('status'=>1,'msg'=>'ok','url'=>U('index')));
			}else{
				$this->ajaxReturn(array('status'=>0,'msg'=>$ret));
			}
		}
	}

    (2)给粉丝添加标签写一个创建标签的方法

public function createTag($tagname=""){
		$access_token=getAccess_token();
		$mp=$this->mp;
		$where['mp_id']=$mp['id'];
		$where['tag']=$tagname;
		$data=M('tage')->where($where)->find();
		if(empty($data)){
			$api="https://api.weixin.qq.com/cgi-bin/tags/create?access_token=$access_token";
			$arr=array();
			$arr['tag']['name']=$tagname;
			$json=json_encode($arr,JSON_UNESCAPED_UNICODE);
			// echo $json;
			// exit;
			include APP_PATH.'LaneWeChat/lanewechat.php';
			$ret=Curl::callwebServer($api,$json,'POST');
			// print_r($ret);
			// exit;
			if($ret['tag']){
				$row['mp_id']=$mp['id'];
				$row['tag_id']=$ret['tag']['id'];
				$row['tag']=$ret['tag']['name'];
				M('tage')->add($row);
			}
		}
	}

  在创建二维码方法中调用这个方法这样在生成二维码时同时也可以生成一个标签,同时把生成的标签同时写入两个表中这样就     可以在扫描二维码时可以给粉丝添加标签

$this->createTag($arr['scene_str']);

 同时在lanewechat的wechatrequest.lib.php要重写方法

public static function eventQrsceneSubscribe(&$request){
        /*
        *用户扫描带参数二维码进行自动分组
        *此处添加此代码是大多数需求是在扫描完带参数二维码之后对用户自动分组
        */
        $scene_str = str_replace("qrscene_","",$request['eventkey']);
        $ret=self::fansGroup($request['fromusername'],$scene_str);
        // echo $ret;
        // exit;
        //移动用户到相应分组中去,此处的$sceneid依赖于之前创建时带的参数
       if($ret===true){
         $content='您是新粉丝,分配到'.$scene_str;
       }else{
        $content='失败了'.json_encode($ret,JSON_UNESCAPED_UNICODE);
       }
        return ResponsePassive::text($request['fromusername'], $request['tousername'], $content);
    }
 public static function eventScan(&$request){
        if(isset($request['eventkey']) && isset($request['ticket'])){
            $scene_str=$request['eventkey'];
            $ret=self::fansGroup($request['fromusername'],$scene_str);
            if($ret===true){
                 $content='分配到'.$scene_str;
               }else{
                $content='失败了'.json_encode($ret,JSON_UNESCAPED_UNICODE);
               }
        }
        return ResponsePassive::text($request['fromusername'], $request['tousername'], $content);
    }
private static function fansGroup($openid,$tagname){
        include APP_PATH.'LaneWeChat/lanwechat.php';
        $id=$_GET['id'];

        $where['mp_id']=$id;
        $where['tag']=$tagname;
        $data=M("tage")->where($where)->find();
        if (!empty($data)) {
            $api="https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token=".getAccess_Token();
            $arr=array();
            $arr=['openid_list'=>array($openid),'tagid'=>(int)$data['tag_id']];
            $json=json_encode($arr);
            $ret=\LaneWeChat\Core\Curl::callWebServer($api,$json,'POST');
            if (isset($ret['errcode']) && $ret['errcode']==0) {
              return true;
            }else{
                return $ret;
            }
        }else{
            return '无此标签'.$tagname;
        }
    } 

猜你喜欢

转载自blog.csdn.net/qq_41860519/article/details/80098539