1.引用微信的js
2.配置
wx.config({
debug: false,
appId: data.appid, // appid信息
timestamp: data.timestamp, //时间戳
nonceStr: data.nonceStr,
signature: data.signature,
jsApiList: ['openLocation', 'scanQRCode', 'closeWindow']
});
wx.error(function (res) {
$.alert("出错了:" + res.errMsg);//这个地方的好处就是wx.config配置错误,会弹出窗口哪里错误,然后根据微信文档查询即可。
});
wx.ready(function () {
wx.checkJsApi({
jsApiList: ['scanQRCode'],
success: function (res) {
}
});
wx.scanQRCode({
needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码,默认二者都有
success: function (res) {
var result = res.resultStr; // 扫码返回的结果
........
},
error: function (res) {
alert(res)
}
});
layer.closeAll('loading');
})
3.后台代码数据(未完成)
var appid="*********";
var timestamp = ((long)(DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalSeconds).ToString();
var nonceStr = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 16);
var ticket= CommonMethod.WebRequestPostOrGet("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + BasicApi.GetTokenSession(Appid, Appsecret) + "&type=jsapi", "");
string toHash = string.Format("jsapi_ticket={0}&noncestr={1}×tamp={2}&url={3}",
ticket, nonceStr, timestamp, url);
SHA1 sha = new SHA1CryptoServiceProvider();
ASCIIEncoding enc = new ASCIIEncoding();
byte[] dataToHash = enc.GetBytes(toHash);
byte[] dataHashed = sha.ComputeHash(dataToHash);
//将运算结果转换成string
var signature = BitConverter.ToString(dataHashed).Replace("-", "").ToLower();
注:ticket生成待考究。