2)坑一:接入準(zhǔn)備 1,、需要在微信公眾號(hào)平臺(tái)里配置“JS接口安全域名”,位置:公眾號(hào)設(shè)置-》功能設(shè)置-》JS接口安全域名,; 2,、配置“網(wǎng)頁(yè)授權(quán)域名”(獲取openid時(shí)需要用到,怎么獲取這里就不說(shuō)了),,位置同上:公眾號(hào)設(shè)置-》功能設(shè)置-》網(wǎng)頁(yè)授權(quán)域名
3)坑二:api_ticket,、signature 1、添加卡券需要的api_ticket和signature(簽名),,和默認(rèn)的 jsapi_ticket,、signature 都不同,最坑的是,,官方的jssdk里還沒(méi)相關(guān)的demo,,所以要自己寫,獲取方法如下:
整合進(jìn)jssdk.php,,大概是這樣子 public function getWxCardApiTicket(){ // api_ticket 應(yīng)該全局存儲(chǔ)與更新,,以下代碼以寫入到文件中做示例 $data = json_decode($this->get_php_file("wxcard_api_ticket.php")); if ($data->expire_time < time()) { $accessToken = $this->getAccessToken(); // 如果是企業(yè)號(hào)用以下 URL 獲取 ticket $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={$accessToken}&type=wx_card"; $res = json_decode($this->httpGet($url)); $ticket = $res->ticket; if ($ticket) { $data->expire_time = time() + 7000; $data->api_ticket = $ticket; $this->set_php_file("wxcard_api_ticket.php", json_encode($data)); } } else { $ticket = $data->api_ticket; } return $ticket; }
2、卡券簽名大概是這樣子,,根據(jù)自己項(xiàng)目的邏輯異步請(qǐng)求計(jì)算簽名(假設(shè)是xxx.php) $jssdk = new JSSDK("appId", "appSecret"); $api_ticket = $jssdk->getWxCardApiTicket(); 簽名驗(yàn)證,、對(duì)比工具:http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=cardsign
4)坑四:js配置 1、js的config配置(jsApiList)需要添加 'addCard'; 2,、addCard方法里的cardExt里需要 'nonce_str' 參數(shù)(官方的demo.js里并沒(méi)有nonce_str參數(shù),,但親測(cè)沒(méi)這個(gè)參數(shù)會(huì)報(bào)簽名錯(cuò)誤!),; 3,、cardExt里參數(shù)和要后臺(tái)計(jì)算簽名的參數(shù)保持一致(時(shí)間戳也要保持一致);
使用card_id異步計(jì)算簽名并調(diào)用添加卡券方法的示例: function addCard(card_id){ var data={'opendid':'<{$smarty.session.openid}>','card_id':card_id,'timestamp':'<?php echo $signPackage["timestamp"];?>','nonceStr':'<?php echo $signPackage["nonceStr"];?>'}; $.get('xxxx.php',data,function(res){ wx.addCard({ cardList: [ { cardId: card_id, cardExt: '{"code":"", "openid": "<{$smarty.session.openid}>", "nonce_str":"<?php echo $signPackage["nonceStr"];?>","timestamp": "<?php echo $signPackage["timestamp"]; ?>", "signature":"'+res.signature+'"}' } ], success: function (res) { alert('已添加卡券:' + JSON.stringify(res.cardList)); }, cancel: function (res) { alert(JSON.stringify(res)) } }); },'json'); }
|
|