您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關微信公眾號如何實現會員卡領取的功能的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
1、會員卡的領取也需要js-sdk接口(可以參考獲取微信公眾號獲取用戶的地理位置信息)(借鑒網址:http://gaoboy.com/article/25.html)
2、 比獲取用戶地理位置信息多了一個是需要單獨獲取簽名包,簽名方式也和獲取用戶地理位置的不同(這里再說一下獲取簽名包的方式)
獲取js-sdk簽名包:
1、當前的url、時間戳、隨機字符串、jsapiticket進行組合
//調用js-sdk的簽名包 public function getSignPackage() { $jsapiTicket = $this->getJsApiTicket(); // 注意 URL 一定要動態獲取,不能 hardcode.(獲取當前網頁的url) $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; //時間戳 $timestamp = time(); //隨機字符串獲取 $nonceStr = $this->createNonceStr(); // 這里參數的順序要按照 key 值 ASCII 碼升序排序 $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; //生成字符串是用來簽名用的 $signature = sha1($string); $signPackage = array( "appId" => $this->appid, "nonceStr" => $nonceStr, "timestamp" => $timestamp, "url" => $url, "signature" => $signature, "rawString" => $string ); return $signPackage; }
獲取會員卡簽名包:
//使用會員卡領取的簽名包 public function getHuiYuanSignPackage() { $apiTicket = $this->getApiTicket(); // 注意 URL 一定要動態獲取,不能 hardcode.(獲取當前網頁的url) $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; //時間戳 $timestamp = time(); //隨機字符串獲取 // $nonceStr = $this->createNonceStr(); // 這里參數的順序要按照 key 值 ASCII 碼升序排序 $string = $timestamp.$apiTicket."pVYA_t3RCVF_yhNcO6QCeAmb-1UI"; //生成字符串是用來簽名用的 $signature = sha1($string); $signPackage = array( "timestamp" => $timestamp, "signature" => $signature, ); return $signPackage; }
詳細代碼說明:
HTML頁面:
//引入微信js文件 <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> <script type="text/JavaScript"> //配置信息驗證接口(填寫的js-sdk獲取的簽名包的參數) wx.config({ debug: false, appId: '<?PHP echo $signPackage["appId"];?>', timestamp: '<?php echo $signPackage["timestamp"];?>', nonceStr: '<?php echo $signPackage["nonceStr"];?>', signature: '<?php echo $signPackage["signature"];?>', jsApiList: [ // 所有要調用的 API 都要加到這個列表中 'addCard' ] }); wx.ready(function(){ //添加卡券 document.querySelector('#addCard').onclick = function () { wx.addCard({ cardList: [ { cardId: "",//微信公眾號內創建的會員卡的id cardExt: '{"timestamp":"<?php echo $huiyuanPackage['timestamp'] ?>","signature":"<?php echo $huiyuanPackage['signature'] ?>"}'//會員卡的簽名包 } ], //成功之后的回調的函數(通過回調函數該表數據庫是否領取會員卡的狀態) success: function (res) { $.ajax({ url: '__CONTROLLER__/editHuiYuan', type: 'post', dataType: 'json', data: {is_LingQu: '1',user_id:"<?php echo $user['user_id'] ?>"}, success:function(){ $("#addCard").html("我的會員卡"); } }) } }); }; }); </script>
控制器中的代碼:
類庫:http://www.jb51.net/article/115732.htm
public function index(){ $user_id = session('user_id'); if($user_id){ $jssdk = new \Home\Model\WechatModel(); $signPackage = $jssdk->GetSignPackage();//獲取js-sdk簽名包 $huiyuanPackage = $jssdk->getHuiYuanSignPackage();獲取會員卡簽名包 //獲取用戶信息 $user = M('user')->where(array('user_id' => $user_id))->find(); //產品收藏數量統計 $goods_count = M('goods_shoucang')->where(array('user_id' => $user_id))->count(); //門店收藏數量統計 $shop_count = M('shop_shoucang')->where(array('user_id' => $user_id))->count(); }else{ //判斷該用戶是否存在 $model = new \Home\Model\WechatModel(); $openid_accesstoken = $model->openId(); $rst = M('user')->where(array('user_openid' => $openid_accesstoken['openid']))->find(); if($rst){ session('openid',$openid_accesstoken['openid']); session('user_id', $rst['user_id']); $jssdk = new \Home\Model\WechatModel(); $signPackage = $jssdk->GetSignPackage(); $huiyuanPackage = $jssdk->getHuiYuanSignPackage(); //獲取用戶信息 $user = M('user')->where(array('user_id' => $rst['user_id']))->find(); //產品收藏數量統計 $goods_count = M('goods_shoucang')->where(array('user_id' => $rst['user_id']))->count(); //門店收藏數量統計 $shop_count = M('shop_shoucang')->where(array('user_id' => $rst['user_id']))->count(); }else{ $userInfo = $model->getOpenId($openid_accesstoken['openid'],$openid_accesstoken['access_token']); $data = array( 'user_img' => $userInfo['headimgurl'], 'user_openid' => $userInfo['openid'], 'user_name' => filter($userInfo['nickname']), 'user_register_time' => time(), 'city' => $userInfo['province'].'-'.$userInfo['city'], ); $id = M('user')->add($data); session('openid', $userInfo['openid']); session('user_id',$id); $jssdk = new \Home\Model\WechatModel(); $signPackage = $jssdk->GetSignPackage(); $huiyuanPackage = $jssdk->getHuiYuanSignPackage(); //獲取用戶信息 $user = M('user')->where(array('user_id' => $id))->find(); //產品收藏數量統計 $goods_count = M('goods_shoucang')->where(array('user_id' => $id))->count(); //門店收藏數量統計 $shop_count = M('shop_shoucang')->where(array('user_id' => $id))->count(); } } $this->assign('signPackage', $signPackage); $this->assign('huiyuanPackage', $huiyuanPackage); $this->assign('user', $user); $this->assign('shop_count', $shop_count); $this->assign('goods_count', $goods_count); $this->display(); }
感謝各位的閱讀!關于“微信公眾號如何實現會員卡領取的功能”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。