.近期項目中須要使用到將在門店註冊的會員作個區分,以便每一個商戶統計在微商城內的業績php
在這裏就須要使用到帶參數的二維碼。將門店參數加載二維碼裏面,用戶在掃描二維碼後,將用戶惟一的openid關聯到用戶掃碼的門店css
1 ,實現步驟html
① 固然是拿到用戶的accesstoken這個很重要,去微信的公開接口去 獲取信息,都須要攜帶這個參數json
access_token是公衆號的全局惟一票據,公衆號調用各接口時都需使用access_token。正常狀況下access_token有效期爲7200秒,api
通常框架裏面都封裝好了獲取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);
返回正常的二維碼app
msg:"獲取成功!"框架
url:"http://weixin.qq.com/q/02INjTIKFBb6e10000007o" dom
③ 經過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 );
至此這個工做就作完了,將用戶與門店參數相關聯