微擎:带参数二维码,自定义接口回复

微擎:带参数二维码,自定义接口回复

微擎中生成带参数二维码

使用场景:生成个人带参数二维码,分享给好友,好友关注后进入指定连接,赠送分享人积分或其他操作。

生成二维码

微擎自带生成二维码函数:barCodeCreateDisposable( b a r c o d e ) 和 b a r C o d e C r e a t e F i x e d ( barcode)和barCodeCreateFixed( barcode)barCodeCreateFixed(barcode),
微擎官方文档:http://s.w7.cc/index.php?c=wiki&do=view&id=1&list=1013,这里生成临时码

	function qrcode($id){ 
		$barcode = array(
		     'expire_seconds' => 2592000,
		     'action_name' => 'QR_SCENE',
		     'action_info' => array(
		         'scene' => array(
		             'scene_id' =>$id,
		         ),
		     ),
		 );
		 $account_api = WeAccount::create();
		 //微擎函数:获得场景二维码,临时码
		 $result = $account_api->barCodeCreateDisposable($barcode);   
		 // $result = $account_api->barCodeCreateFixed($barcode);//永久码
		// 微擎官方文档:http://s.w7.cc/index.php?c=wiki&do=view&id=1&list=1013
		 return $result;

		 // 前台获取二维码图片 https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket = $qr
	}
	

这里的场景值 scene_id 可自己定,但是在微擎中我们看他是怎么生成的,在 web/source/platform/qr.ctrl.php
在 do=post 处,有一段

$qrcid = pdo_fetchcolumn("SELECT qrcid FROM ".tablename('qrcode')." WHERE acid = :acid AND model = '1' ORDER BY qrcid DESC LIMIT 1", array(':acid' => $acid));
            $barcode['action_info']['scene']['scene_id'] = !empty($qrcid) ? ($qrcid + 1) : 100001;
            $barcode['expire_seconds'] = intval($_GPC['expire-seconds']);
            $barcode['action_name'] = 'QR_SCENE';
            $result = $uniacccount->barCodeCreateDisposable($barcode);

 

微擎查询了 ims_qrcode 表,所以本例中的场景值也与微擎保持一致。查看qr.ctrl.php发现微擎后台生成二维码时如果是临时二维码他用scene_id,而生成永久二维码时用scene_str,本例使用的微擎版本是这样的,不知道其他版本微擎是否也一样,未考究。所以研究ims_qrcode表发现,‘scene_id’,和*‘scene_str’只存了其中一个。所以本例中将分享人的openid存到了scene_str*中,后根据scene值查询ims_qrcode表获取到分享人的openid。
用户扫描带参数二维码后,微信服务器根据公众号后台填写的第三方服务器接口地址,发出数据请求,也就是请求到微擎的 api.php中,进入start()方法,跳转到analyze()方法,对于扫描二维码事件,会再跳转到analyzeqr(),在里面会查询qrcode表,比较其中的qrcid或者sceneid中的值是否与微信传过来的eventkey相等,如果没有,会再次查找ticket中是否有值与微信传过来的ticket相等。所以ims_qrcode 表很重要。
所以这里要把生成的带参二维码存入ims_qrcode表。

$insert = array(
                'uniacid' => $_W['uniacid'],
                'acid' => $acid,
                'qrcid' => $barcode['action_info']['scene']['scene_id'],
                'scene_str' => $barcode['action_info']['scene']['scene_str'],
                // 'keyword' => $_GPC['keyword'],
                'keyword' => 'share',
                // 'name' => $_GPC['scene-name'],
                'name' =>$openid,
                'model' => 2,//永久
                'ticket' => $result['ticket'],
                'url' => $result['url'],
                'expire' => $result['expire_seconds'],
                'createtime' => TIMESTAMP,
                'status' => '1',
                'type' => 'scene',
            );
            pdo_insert('qrcode', $insert);

这里的 keyword 要注意:这个是在微擎后台设置的关键字,
本例是在微擎中 自动回复>自定义接口回复 中设置的关键字 share;这个必须对应,不然微信返回给微擎无法调用接口,研究一下微擎api.php文件就明白了。

接受微信返回信息

用户扫描了带参数的二维码,微信返回信息给微擎,微擎api接口会根据keywordims_qrcode表中找到关键字,调用自定义接口,将信息返回到定义的自定义接口中,自定义接口放在微擎项目的 framework/builtin/userapi/api/,在自定义接口中进行事件操作,正常情况下返回如下:

// return $this->respText(json_encode($this->message));//返回的数据
//如果已关注,扫码
/*{"tousername":"gh_a4f6d262a7b6",
"fromusername":"o0wkLxHbOn8bPmsO9tA286Z6-WWW",
"createtime":"1575880363",
"msgtype":"event",
"event":"SCAN",
"eventkey":"100012",
"ticket":"二维码ticket",
"from":"o0wkLxHbOn8bPmsO9tA286Z6-WWW",
"to":"gh_a4f6d262a7b6",
"time":"1575880363",
"type":"text",
"scene":"100012",
"redirection":true,
"source":"qr",
"content":"share"}*/

//未关注,扫码关注
 {"tousername":"gh_a4f6d262a7b6",
"fromusername":"o0wkLxN5_X3uavbAoSaSmHPIWXXX",
 "createtime":"1575880612",
 "msgtype":"event",
 "event":"subscribe",
 "eventkey":"qrscene_100012", 
 "ticket":"二维码ticket",
 "from":"o0wkLxN5_X3uavbAoSaSmHPIWXXX",
 "to":"gh_a4f6d262a7b6",
 "time":"1575880612",
 "type":"text",
  "scene":"100012",
 "redirection":true,
 "source":"qr",
 "content":"share"}


这里的event为事件,scene为场景值,content为关键字,根据返回的事件和场景值进行业务操作

  
        if($this->message['msgtype'] === 'event'){
            $data .= '消息类型为事件。';
            if($this->message['event'] === 'SCAN' || $this->message['event'] === 'subscribe' )
            {
                if($this->message['event'] === 'SCAN')
                 		$data .= '事件类型为扫描。';
                else
                 		//$data .= '事件类型为关注。';
		                $replys[] = array(
						'title' => '感谢关注',
						'picurl' => '你的图片地址',
						'description' => '你的描述',
						'url' => '要跳转的链接',
					);
					return $this->respNews ($replys);
                $qrMessage = $this->message['content'];//扫描到的二维码信息
                    
            }
        }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值