实现微信带参数的二维码功能
.近期项目中需要使用到将在门店注册的会员做个区分,以便每个商户统计在微商城内的业绩
在这里就需要使用到带参数的二维码。将门店参数加载二维码里面,用户在扫描二维码后,将用户唯一的openid关联到用户扫码的门店
1 ,实现步骤
① 当然是拿到用户的accesstoken这个很重要,去微信的公开接口去 获取信息,都需要携带这个参数
access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token。正常情况下access_token有效期为7200秒,
一般框架里面都封装好了获取accessToken的方法,这里不再赘述了
$account = account_fetch( $_W[\'uniacid\']); load()->classs(\'weixin.account\'); $account_api = WeAccount::create(); $token = $account_api->getAccessToken();
② 获取带参数的二维码
$post_data[\'action_name\']=\'QR_LIMIT_STR_SCENE\'; //指明需要带参数的二维码 $post_data[\'action_info\'][\'scene\'][\'scene_str\'] = \'store-\'.$id; //把店铺id放入其中 $qrcode_url=\'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=\'.$token; $return_result = ihttp_post($qrcode_url, json_encode($post_data)); $return_json=json_decode($return_result[\'content\'],true);
返回正常的二维码
msg:“获取成功!“
url:“http://weixin.qq.com/q/02INjTIKFBb6e10000007o”
③ 通过qrcode.js将二维码显示出来
qrcode.js大家可以在网上自己下载
<script src="./resource/js/lib/qrcode.js"></script>
function showCode(obj) { try { var shopname =$(\'#shopname\').html(); var uniacid = $(obj).attr("data-uniacid"); var name = $(obj).attr("data-name"); var code = $(obj).attr("data-code"); //var url = "http://drp.winu.cn/app/index.php?i=" + uniacid + "&c=entry&do=member&m=ewei_shop" + "&name=" + name + "&code=" + code; var url=$(obj).attr(\'data-url\'); if(url==\'\'){ $(".js_qrcode_btn1").show(); } $(\'#barModal .modal-body\').html(""); new QRCode($(\'#barModal .modal-body\')[0], url); $("#barModal .modal-body img").css({ width: \'300px\', height: \'300px\', margin: \'0 auto\', }); $("#barModal .modal-body").append($("<div class=\'text-center shopname\' style=\'text-align:center;font-size:18px;margin-top:15px;\'>"+shopname+"</div>")); $("#barModal").modal(\'show\'); } catch (e) { util.message(e + ""); } }
④ 处理用户微信扫码事件在和微信的接口api里面处理start(扫描)事件和booking(订阅(关注事件))
在这里我将用户openid与门店storeid放在缓存里面
if(!empty($message[\'scene\'])) { $scene_tmp = explode(\'-\', $message[\'scene\']); if ($scene_tmp[0] == \'store\') { if ($message[\'event\'] == \'subscribe\') { //关注写入关系数据 //判断是否是重复关注 $store_id =$scene_tmp[1]; load()->func(\'logging\'); logging_run($scene_tmp[1],\'normal\',"api_save_uid",date("Y-m-d")); $result = Db::name(\'core_cache\')->where(\'key\', $message[\'from\'])->value(\'value\'); if (empty($result)) { $tmp_data[\'key\'] = $message[\'from\']; $tmp_data[\'value\'] = $store_id; Db::name(\'core_cache\')->insert($tmp_data); unset($tmp_data); }else{ //如果已经存在了这个人的数据 if($store_id <> $result){ $update[\'value\'] = $store_id; Db::name(\'core_cache\')->where(\'openid\',$message[\'from\'])->update($update); } } } } }
if (!isset($setting[\'passport\']) || empty($setting[\'passport\'][\'focusreg\'])) { $storeid = Db::name(\'core_cache\')->where(\'key\',$message[\'from\'])->value(\'value\'); $storecode = \'\'; if(!empty($storeid)){ $storecode = Db::name(\'ewei_shop_store\')->where(\'uniacid\',$_W[\'uniacid\'])->where(\'id\',$storeid)->value(\'storecode\'); } $data = array( \'uniacid\' => $_W[\'uniacid\'], \'email\' => md5($message[\'from\']).\'@we7.cc\', \'salt\' => random(8), \'groupid\' => $default_groupid, \'createtime\' => TIMESTAMP, \'storecode\'=>$storecode );
至此这个工作就做完了,将用户与门店参数相关联