下面是模板页面单击领取卡券的,加了一个参数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不正确好像也可以的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
$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里面,在其他文件