這篇文章主要介紹了微信公眾號實現會員卡領取功能的相關資料,需要的朋友可以參考下
1、會員卡的領取也需要js-sdk接口(可以參考獲取微信公眾號取得使用者的地理位置資訊)(借鏡網址:http://gaoboy.com/article/25.html)
2、 比取得使用者地理位置資訊多了一個是需要單獨取得簽章包,簽章方式也和取得使用者地理位置的不同(這裡再說一次取得簽章包的方式)
取得js-sdk簽章包:
# 1、目前的url、時間戳記、隨機字串、jsapiticket進行組合
#1212212>
##
//调用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; }詳細程式碼說明: HTML頁面:
#
//使用会员卡领取的签名包 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; }1控制器中的程式碼:
//引入微信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(); }
以上是分享微信公眾號實現會員卡領取的功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!