下面是模板页面单击领取卡券的,加了一个参数outer_str,这样领取返回时就多了一个参数outer_str(这里返回没有下滑线了),可以作为场景的来源参照,nimei_str那个不用的也可以

{php echo register_jssdk(false);}
<script>
    function jumpCardUrl(){
        // alert("{$cardArry['openid']}");
        // alert("{$cardArry['timestamp']}");
        // alert("{$cardArry['nonceStr']}");
        // alert("{$cardArry['signature']}");
        // alert("{$cardArry['cardId']}");
     wx.addCard({

    cardList: [
         {
            cardId: '{$cardArry['cardId']}',

           cardExt: '{"code": "", "openid": "{$cardArry['openid']}", "timestamp": "{$cardArry['timestamp']}","nonce_str":"{$cardArry['nonceStr']}", "signature":"{$cardArry['signature']}","outer_str":"{$outerstr}","nimei_str":"rw_22_33"}'
           
        }

    ], // 需要添加的卡券列表
     success: function (res) {
        }

     }); 
    }

</script>

 php页面  卡券场景来源变量 就不说了根据实际情况查询   $cardinfo,$openid分别是卡券cardid和用户openid,用户openid不正确好像也可以的

$cardArry = getCard($cardinfo,$openid);
function getCard($card_id,$openid){

     global $_W,$_GPC;

     //获取access_token

        load()->classs('weixin.account');

        load()->func('communication');

        $access_token = WeAccount::token();
        $ticket=getApiTicket($access_token);
     //获得ticket后将参数拼成字符串进行sha1加密
        $now = time();

        $timestamp = $now;

        $nonceStr = createNonceStr();

        $card_id = $card_id;

        $openid = $openid;

        $arr = array($card_id,$ticket,$nonceStr,$openid,$timestamp);//组装参数

        asort($arr, SORT_STRING);

        $sortString = "";

         foreach($arr as $temp){

            $sortString = $sortString.$temp;

         }

        $signature = sha1($sortString);

     $cardArry = array(

        'code' =>"",

        'openid' => $openid,

        'timestamp' => $now,

        'signature' => $signature,

        'cardId' => $card_id,

        'ticket' => $ticket,

        'nonceStr' => $nonceStr,

     );

    return $cardArry;

        

  

  }

 function createNonceStr($length = 16) {

        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

        $str = "";

        for ($i = 0; $i < $length; $i++) {

            $str.= substr($chars, mt_rand(0, strlen($chars) - 1) , 1);

        }

        return $str;

    }

function getCardTicket($app_id){

        global $_W,$_GPC;

        //获取access_token

        load()->classs('weixin.account');

        load()->func('communication');

        $access_token = WeAccount::token();

        $api_ticket = getApiTicket($access_token);

        $card_id ="";

        $card_type = "";

        $location_id = "";

        //获得ticket后将参数拼成字符串进行sha1加密

        $now = time();

        $timestamp = $now;

        $nonceStr = createNonceStr(8);

        $arr = array($api_ticket,$location_id,$app_id,$nonceStr,$card_id,$timestamp,$card_type);//组装参数

        asort($arr, SORT_STRING);

        $sortString = "";

         foreach($arr as $temp){

            $sortString = $sortString.$temp;

         }

        $signature = sha1($sortString);

        

         $cardArry = array(

            

            'timestamp' => $now,

            'signature' => $signature,

            'nonceStr' => $nonceStr,

         );

        return $cardArry;

            

      

      }
 function getApiTicket($access_token){

        global $_W, $_GPC;

        $w = $_W['uniacid'];

        $cookiename = "wx{$w}a{$w}pi{$w}ti{$w}ck{$w}et";

        $apiticket = $_COOKIE[$cookiename];

        if (empty($apiticket)){

            $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={$access_token}&type=wx_card";

            load()->func('communication');

            $res = ihttp_get($url);

            $res = json_decode($res['content'],true);

            if (!empty($res['ticket'])){

                setcookie($cookiename,$res['ticket'],time()+$res['expires_in']);

                $apiticket = $res['ticket'];

            }else{

                message('获取api_ticket失败:'.$res['errmsg']);

            }

        }

        return $apiticket;

    }

  领取成功,核销   返回信息都是在api.php里面

有一个在卡券页面点击第一个使用的核销不是在api里面,在其他文件