微信卡券H5投放开发

来源:互联网 发布:雨人软件 滕德润 编辑:程序博客网 时间:2024/05/06 18:26

配置

微信卡券H5投放开发需要使用微信JS-SDK,相关配置已在“公众号网页发开教程”中说过了,不同之处就是在wx.config中添加“批量添加卡券接口(addCard) “

wx.config({     debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。    appId: 'xxxxxxxxx', // 必填,公众号的唯一标识    timestamp: 1495164833, // 必填,生成签名的时间戳    nonceStr: '1c604bf7-b176-4d3c-96df-7ce30285fa0f', // 必填,生成签名的随机串    signature: 'xxxxxxxxxxxxxx',// 必填,签名,见附录1    jsApiList: ['addCard'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2})

批量添加卡券接口

wx.addCard({    cardList: [{        cardId: '',        cardExt: ''    }], // 需要添加的卡券列表    success: function (res) {        var cardList = res.cardList; // 添加的卡券列表信息    }});

注意: cardExt本身是一个JSON字符串,重要事情说三遍:字符串字符串字符串!所以必须用字符串给它赋值,如下所示,使用其他格式赋值将报错:“签名错误”。

var cardExt = "{'timestamp':'1495180999','nonce_str':'bdcaeedfe7624ff989b0ff7771360e49','signature':'xxxxxxxxxxxxxx'}";

Java生成sha1签名

字典排序

List<String> arl = new ArrayList<String>();arl.add("nonceStr");arl.add("timesTamp");arl.add("apiTicket");arl.add("cardId");Collections.sort(arl);

sha1签名

public static String getSha1(String str){   if (null == str || 0 == str.length()){       return null;   }   char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',            'a', 'b', 'c', 'd', 'e', 'f'};   try {       MessageDigest mdTemp = MessageDigest.getInstance("SHA1");       mdTemp.update(str.getBytes("UTF-8"));       byte[] md = mdTemp.digest();       int j = md.length;       char[] buf = new char[j * 2];       int k = 0;       for (int i = 0; i < j; i++) {           byte byte0 = md[i];           buf[k++] = hexDigits[byte0 >>> 4 & 0xf];           buf[k++] = hexDigits[byte0 & 0xf];       }       return new String(buf);   } catch (Exception e) {    e.printStackTrace();    return null;   } }

微信卡券JSAPI签名校验工具

示例

<!DOCTYPE html><html>    <head>        <meta charset="utf-8">    </head>    <body>        <!--        省略页面        -->        <!--引入微信JS-->        <script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>        <script type="text/javascript">            wx.config({                 debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。                appId: 'xxxxxxxx', // 必填,公众号的唯一标识                timestamp: 1495164833, // 必填,生成签名的时间戳                nonceStr: '1c604bf7b1764d3c96df7ce30285fa0f', // 必填,生成签名的随机串                signature: 'xxxxxxx',// 必填,签名,见附录1                jsApiList: ['addCard'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2            })            wx.ready(function(){                $("#btn_coupon").click(function(){                    wx.addCard({                        cardList: [{                            cardId: "xxxxxxxxxxxxxxxx",                            cardExt: "{'timestamp':'1495180999','nonce_str':'bdcaeedfe7624ff989b0ff7771360e49','signature':'xxxxxxxxxxxxxx'}"                        }], // 需要添加的卡券列表                        success: function (res) {                            console.log(res);                        }                    });                })            })        </script>    </body></html>