var api = ""; var appid = "" var openid = "" function getQueryString(name) { let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); let r = window.location.search.substr(1).match(reg); if (r != null) { return decodeURIComponent(r[2]); }; return null; } function initwx(){ url = window.location.href $.ajax({ cache: true, type: "POST", type: "POST", url: api + "/game/jssdk", data: JSON.stringify({ 'url': url, }), contentType: "application/json; charset=utf-8", dataType: "json", success: function (resp) { layer.closeAll('loading'); if (resp.errcode == 0) { wx.config({ debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。 appId: "", // 必填,公众号的唯一标识 timestamp: resp.data.data.timestamp, // 必填,生成签名的时间戳 nonceStr: resp.data.data.nonceStr, // 必填,生成签名的随机串 signature: resp.data.data.signature,// 必填,签名 jsApiList: ['chooseWXPay'] // 必填,需要使用的JS接口列表 }); } else { layer.msg(data.errmsg, { time: 1000, }); return; } }, error: function () { layer.closeAll('loading'); layer.msg("连接超时", { time: 1000, }); } }); } function wxlogin(){ var url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appid+"&redirect_uri="+window.location.href+"&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect" console.log("url = ", url) var code = getQueryString("code") console.log("code val = ", code) if(!code){ window.location = url; }else{ $.ajax({ cache: true, type: "GET", url: api + "/game/h5login", data: { 'code': code, }, success: function (resp) { layer.closeAll('loading'); if (resp.errcode == 0) { console.log("resp = ", resp ) openid = resp.data.openid var order_no = getQueryString("order_no") initwx() loadorder(order_no) } else { layer.msg(data.errmsg, { time: 1000, }); return; } }, error: function () { layer.closeAll('loading'); layer.msg("连接超时", { time: 1000, }); } }); } } function loadorder(order_no) { //验证码 $.ajax({ cache: true, type: "GET", url: api + "/game/loadorder", data: { 'order_no': order_no, }, success: function (resp) { layer.closeAll('loading'); if (resp.errcode == 0) { var order = resp.data.order document.getElementById("amount").textContent = order.payment + "元" document.getElementById("item").textContent = order.description payBtn() } else { layer.msg(resp.errmsg, { time: 1000, }); } return; }, error: function () { layer.closeAll('loading'); layer.msg("连接超时", { time: 1000, }); } }); } function payBtn() { var order_no = getQueryString("order_no") if (order_no == null) { layer.msg("订单号获取失败", { time: 2000, }); return } $.ajax({ url: api + "/game/h5pay", data: { "order_no": order_no, "openid": openid, }, success: function (resp) { layer.closeAll('loading'); if (resp.errcode == 0) { wx.ready(function() { wx.chooseWXPay({ timestamp: resp.data.data.timestamp, //支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符 nonceStr: resp.data.data.nonceStr, // 支付签名随机串,不长于 32 位 package: resp.data.data.package, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=\*\*\*) signType: 'RSA', // 微信支付V3的传入RSA,微信支付V2的传入格式与V2统一下单的签名格式保持一致 paySign: resp.data.data.paySign, // 支付签名 success: function (res) { // 支付成功后的回调函数 console.log("pay success res= ", res) if (res.errMsg == "chooseWXPay:ok"){ const paycomplete_button = document.getElementById("paycomplete"); paycomplete_button.style.display = "block"; const paying_button = document.getElementById("paying"); paying_button.style.display = "none"; const wxpay_button = document.getElementById("wxpay"); wxpay_button.style.display = "none"; layer.msg("支付成功", { time: 2000, }); }else if (res.errMsg == "chooseWXPay:cancel"){ const paycomplete_button = document.getElementById("paycomplete"); paycomplete_button.style.display = "none"; const paying_button = document.getElementById("paying"); paying_button.style.display = "none"; const wxpay_button = document.getElementById("wxpay"); wxpay_button.style.display = "block"; } }, complete: function (res) { // 支付成功后的回调函数 console.log("pay complete res= ", res) if (res.errMsg == "chooseWXPay:ok"){ const paycomplete_button = document.getElementById("paycomplete"); paycomplete_button.style.display = "block"; const paying_button = document.getElementById("paying"); paying_button.style.display = "none"; const wxpay_button = document.getElementById("wxpay"); wxpay_button.style.display = "none"; layer.msg("支付完成", { time: 2000, }); }else if (res.errMsg == "chooseWXPay:cancel"){ const paycomplete_button = document.getElementById("paycomplete"); paycomplete_button.style.display = "none"; const paying_button = document.getElementById("paying"); paying_button.style.display = "none"; const wxpay_button = document.getElementById("wxpay"); wxpay_button.style.display = "block"; } }, fail: function (res) { // 支付成功后的回调函数 const paycomplete_button = document.getElementById("paycomplete"); paycomplete_button.style.display = "none"; const paying_button = document.getElementById("paying"); paying_button.style.display = "none"; const wxpay_button = document.getElementById("wxpay"); wxpay_button.style.display = "block"; layer.msg("支付失败", { time: 2000, }); } }); }) } else { layer.msg(resp.errmsg, { time: 1000, }); } }, error: function () { layer.closeAll('loading'); layer.msg("连接超时", { time: 2000, }); return false; } }) }