wxpay.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. function onBridgeReady(params) {
  2. console.log("onBridgeReady+"+new Date().getTime())
  3. uni.showLoading({
  4. mask:true,title:'加载中...'
  5. })
  6. //记录要回跳的url
  7. window.WeixinJSBridge.invoke(
  8. 'getBrandWCPayRequest', {
  9. 'appId': params.appId, // 公众号名称,由商户传入
  10. 'timeStamp': params.timeStamp, // 时间戳,自1970年以来的秒数
  11. 'nonceStr': params.nonceStr, // 随机串
  12. 'package': params.package,
  13. 'signType': params.signType, // 微信签名方式:
  14. 'paySign': params.paySign // 微信签名
  15. },
  16. (res) => {
  17. //alert(JSON.stringify(res)); // 支付取消会执行 支付成功不会
  18. if (res.err_msg == 'get_brand_wcpay_request:ok') {
  19. //alert("支付成功") // 不会执行
  20. //window.location.href = params.url
  21. } else if (res.err_msg == "get_brand_wcpay_request:cancel") {
  22. //alert("支付取消") // 会执行
  23. uni.hideLoading();
  24. } else {
  25. uni.hideLoading();
  26. }
  27. }
  28. )
  29. }
  30. export const wxPayJs = (params) => {
  31. console.log("wxPayJs+"+new Date().getTime())
  32. if (typeof window.WeixinJSBridge === 'undefined') {
  33. if (document.addEventListener) {
  34. document.addEventListener('WeixinJSBridgeReady', function() {
  35. onBridgeReady(params)
  36. }, false)
  37. } else if (document.attachEvent) {
  38. document.attachEvent('WeixinJSBridgeReady', function() {
  39. onBridgeReady(params)
  40. })
  41. document.attachEvent('onWeixinJSBridgeReady', function() {
  42. onBridgeReady(params)
  43. })
  44. }
  45. } else {
  46. onBridgeReady(params)
  47. }
  48. }