public function getCardParam(){ $arr = array(); $Weixin = new \Weixin\Controller\BaseController(); $arr = array( ‘app_id‘ => C(‘WEIXIN_CONFIG.appId‘), ‘api_ticket‘ => $Weixin->jssdk->getCardTicket() , ‘timestamp‘ => time(), ‘nonceStr‘ =>createNoncestr() ,‘card_type‘ => ‘CASH‘ ); $arr1 = array_values($arr); sort( $arr1, SORT_STRING ); $arr[‘card_sign‘] = sha1( implode($arr1) ) ; echo json_encode($arr) ; // $this->assign(‘cardArr‘,$arr); } public function getCardCode(){ $Weixin = new \Weixin\Controller\BaseController(); $access_token = $Weixin->WechatAuth->getAccessToken(); $url4 = ‘https://api.weixin.qq.com/card/code/decrypt?access_token=‘.$access_token; //卡券code $s = json_encode(array(‘encrypt_code‘=>$_POST[‘encrypt_code‘])); $r = $this->http_post_data($url4,$s); echo $r[1]; } public function getCardDetail(){ // if(!IS_POST){ // $this->error(‘非法请求‘); // } $Weixin = new \Weixin\Controller\BaseController(); $access_token = $Weixin->WechatAuth->getAccessToken(); $url4 = ‘https://api.weixin.qq.com/card/get?access_token=‘.$access_token; //批量卡券详情 $s = json_encode(array(‘card_id‘=>$_POST[‘card_id‘])); $r = $this->http_post_data($url4,$s); echo $r[1]; // p(json_decode($r[1],true)); } function http_post_data($url, $data_string) { $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_HTTPHEADER, array( ‘Content-Type: application/json; charset=utf-8‘, ‘Content-Length: ‘ . strlen($data_string)) ); ob_start(); curl_exec($ch); $return_content = ob_get_contents(); ob_end_clean(); $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); return array($return_code, $return_content); } /** * 作用:产生随机字符串,不长于32位 */ function createNoncestr( $length = 32 ) { $chars = "abcdefghijklmnopqrstuvwxyz0123456789"; $str =""; for ( $i = 0; $i < $length; $i++ ) { $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1); } return $str; }
html代码
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> <div class="coupon"> <a id="chooseCard" href="javascript:void(0);"> <!--选择优惠券--><img src="__IMG__/you_01.jpg" /></a> <!-- <div id="aaaaa"><a href="javascript:location.reload()">shuaxin</a></div> --> </div> <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script> <script> var getCardParamUrl = "{:U(‘Card/getCardParm‘)}"; var getCardDetailUrl = "{:U(‘Card/getCardDetail‘)}"; var getCardCodeUrl = "{:U(‘Card/getCardCode‘)}"; 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: [ ‘chooseCard‘ ] }); var readyFunc = function onBridgeReady() { $("#chooseCard").click(function() { $.ajax({ type: ‘get‘, url: getCardParmUrl, dataType: ‘json‘, success: function(param) { // alert(JSON.stringify(param)); var cardArr = param; wx.chooseCard({ cardType: cardArr.card_type, // 卡券类型 // cardId: ‘‘, // 卡券Id timestamp: cardArr.timestamp , // 卡券签名时间戳 nonceStr: cardArr.nonceStr, // 卡券签名随机串 signType: ‘SHA1‘, // 签名方式,默认‘SHA1‘ cardSign:cardArr.card_sign , // 卡券签名,详见附录4 // ‘location_id‘ : "{$cardArr.location_id}" success : function(res){ var choose_card_info = JSON.parse(res.cardList); // alert(choose_card_info); if (choose_card_info[0].card_id) { coupon_used_id = choose_card_info[0].card_id; // alert(coupon_used_id); var coupon_encrypt = choose_card_info[0].encrypt_code; $.ajax({ type: ‘post‘, url: getCardDetailUrl, data: { card_id: choose_card_info[0].card_id }, dataType: ‘json‘, success: function(da) { // alert(JSON.stringify(da)); if (da.errmsg == ‘ok‘) { if (goods_total_money < (da.card.cash.least_cost) / 100) { alert(‘对不起,您买的商品不满‘ + (da.card.cash.least_cost) / 100 + ‘元,请选择其他优惠券‘); } else { coupon_used_name = da.card.cash.base_info.title; // alert(coupon_used_name); coupon_used_id = da.card.cash.base_info.id; coupon_total_money = (da.card.cash.reduce_cost) / 100; $("#coupon_total_money").text(coupon_total_money.toFixed(2)); getPayMoney(); } } } }); $.ajax({ //取得卡的真正唯一序列号 type: ‘post‘, url: getCardCodeUrl, data: { encrypt_code: coupon_encrypt }, dataType: ‘json‘, success: function(data1) { if (data1.errmsg == ‘ok‘) { coupon_code = data1.code; $("input[name=coupon_code]").val(coupon_code); } } }) } } }); } }) }) } if (typeof WeixinJSBridge === ‘undefined‘) { document.addEventListener(‘WeixinJSBridgeReady‘, readyFunc, false); } else { readyFunc(); } </script>